#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
void preOrderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}
void inOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}
void postOrderTraversal(struct Node* root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
}
void freeTree(struct Node* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 5);
insert(root, 3);
insert(root, 7);
insert(root, 2);
insert(root, 4);
insert(root, 6);
insert(root, 8);
printf("전위 순회 결과: ");
preOrderTraversal(root);
printf("\n");
printf("중위 순회 결과: ");
inOrderTraversal(root);
printf("\n");
printf("후위 순회 결과: ");
postOrderTraversal(root);
printf("\n");
freeTree(root);
return 0;
}

스택을 이용한 이진 트리 구현하기
스택을 사용하면 이진 트리를 순회하는 데 유용하며, 특히 비재귀적인 방법으로 순회할 때 편리하다.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct StackNode {
struct Node* treeNode;
struct StackNode* next;
};
void push(struct StackNode** top, struct Node* treeNode) {
struct StackNode* newStackNode = (struct StackNode*)malloc(sizeof(struct StackNode));
newStackNode->treeNode = treeNode;
newStackNode->next = *top;
*top = newStackNode;
}
struct Node* pop(struct StackNode** top) {
if (*top == NULL) return NULL;
struct StackNode* temp = *top;
struct Node* treeNode = temp->treeNode;
*top = (*top)->next;
free(temp);
return treeNode;
}
int isEmpty(struct StackNode* top) {
return top == NULL;
}
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}
void inOrderTraversal(struct Node* root) {
struct StackNode* stack = NULL;
struct Node* current = root;
while (current != NULL || !isEmpty(stack)) {
while (current != NULL) {
push(&stack, current);
current = current->left;
}
current = pop(&stack);
printf("%d ", current->data);
current = current->right;
}
}
void preOrderTraversal(struct Node* root) {
if (root == NULL) return;
struct StackNode* stack = NULL;
push(&stack, root);
while (!isEmpty(stack)) {
struct Node* current = pop(&stack);
printf("%d ", current->data);
if (current->right != NULL) {
push(&stack, current->right);
}
if (current->left != NULL) {
push(&stack, current->left);
}
}
}
void postOrderTraversal(struct Node* root) {
if (root == NULL) return;
struct StackNode* stack1 = NULL;
struct StackNode* stack2 = NULL;
push(&stack1, root);
while (!isEmpty(stack1)) {
struct Node* current = pop(&stack1);
push(&stack2, current);
if (current->left != NULL) {
push(&stack1, current->left);
}
if (current->right != NULL) {
push(&stack1, current->right);
}
}
while (!isEmpty(stack2)) {
struct Node* current = pop(&stack2);
printf("%d ", current->data);
}
}
void freeTree(struct Node* root) {
if (root != NULL) {
freeTree(root->left);
freeTree(root->right);
free(root);
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 5);
insert(root, 3);
insert(root, 7);
insert(root, 2);
insert(root, 4);
insert(root, 6);
insert(root, 8);
printf("중위 순회 결과: ");
inOrderTraversal(root);
printf("\n");
printf("전위 순회 결과: ");
preOrderTraversal(root);
printf("\n");
printf("후위 순회 결과: ");
postOrderTraversal(root);
printf("\n");
freeTree(root);
return 0;
}'CS > C' 카테고리의 다른 글
| C언어로 이진 탐색 트리 구현하기 (0) | 2025.04.15 |
|---|---|
| C언어로 스택/큐 구현하기 (0) | 2025.04.12 |
| GPT야, 3.9와 3.11 중에 무엇이 더 큰 수야? (0) | 2025.02.07 |
| C언어로 BFS 구현하기 (0) | 2025.02.07 |
| C언어로 DFS 구현하기 (0) | 2025.02.07 |