#include<stdio.h>
#include<stdlib.h>
#define MAX 10
typedef struct node {
char data;
struct node* left;
struct node* right;
}Node;
Node* arr[MAX];
int top;
Node* pop() {
Node* node = NULL;
if (top == -1) {
return node;
}
node = arr[top];
top--;
return node;
}
void push(Node* p) {
// Increase the top by 1 and store data in the box indicated by the top
if (top == MAX - 1) {
return;
}
arr[++top] = p;
}
//potential
void preorder(Node* node) {
Node* tmp;
top = -1;
tmp = node;
while (1) {
while (tmp != NULL) {
printf("%c ", tmp->data);
push(tmp);
tmp = tmp->left;
}
tmp = pop();
if (tmp == NULL) break;
tmp = tmp->right;
}
}
Lieutenant //
void inorder(Node* node) {
Node* tmp;
top = -1;
tmp = node;
while (1) {
While (tmp! = null) {// priority.
push(tmp);
tmp = tmp->left;
}
tmp = pop();
if (tmp == NULL) break;
printf("%c ", tmp->data);
tmp = tmp->right;
}
}
//Back
void postorder(Node* node) {
Node* tmp;
top = -1;
tmp = node;
while (1) {
do {
if (tmp->data != NULL && tmp != NULL) {
push(tmp);
tmp = tmp->left;
}
else break;
} } while (tmp != NULL);
tmp = pop();
if (!tmp) break;
if (tmp->right != NULL) {
if (tmp->right->data == NULL) {
printf("%c ", tmp->data);
tmp->data = NULL;
}
else {
push(tmp);
tmp = tmp->right;
}
}
else {
printf("%c ", tmp->data);
tmp->data = NULL;
}
}
}
Node* queue[MAX];
int front = -1;
int rear = -1;
void enqueue(Node* node) {
if (rear == MAX - 1) {
return;
}
if (front == -1) {
front++;
}
rear++;
queue[rear] = node;
}
Node* dequeue() {
Node* node = NULL;
if (front == rear) {
if (front != -1) {
node = queue[front];
front = rear = -1;
}
return node;
}
node = queue[front];
front++;
return node;
}
//Level
void levelOrder(Node* root) {
Node* tmp;
enqueue(root);
while (1) {
tmp = dequeue();
if (tmp == NULL) {
break;
}
printf("%c ", tmp->data);
if (tmp->left) {
enqueue(tmp->left);
}
if (tmp->right) {
enqueue(tmp->right);
}
}
}
//Create Binary Tree
Node* makeBinaryTree(char data, Node* left, Node* right) {
Node* tmp = (Node*)malloc(sizeof(Node));
tmp->data = data;
tmp->left = left;
tmp->right = right;
return tmp;
}
void main() {
Node* f11 = makeBinaryTree('K', NULL, NULL);
Node* f10 = makeBinaryTree('J', NULL, NULL);
Node* f9 = makeBinaryTree('I', NULL, NULL);
Node* f8 = makeBinaryTree('H', NULL, NULL);
Node* f7 = makeBinaryTree('G', NULL, f11);
Node* f6 = makeBinaryTree('F', NULL, NULL);
Node* f5 = makeBinaryTree('E', f9, f10);
Node* f4 = makeBinaryTree('D', f8, NULL);
Node* f3 = makeBinaryTree('C', f6, f7);
Node* f2 = makeBinaryTree('B', f4, f5);
Node* f1 = makeBinaryTree('A', f2, f3);
printf(" preorder : ");
preorder(f1);
printf("\n\n inorder : ");
inorder(f1);
printf("\n\n postorder : ");
postorder(f1);
printf("\n\n levelorder : ");
levelOrder(f1);
}
Above is the code for the four traverses of the binary tree (potential, median, posterior, and level traversal). Among these, only level tours are not available, so I'm really curious. I think it's a problem with the enqueue and the dequeue functions.
c tree
This is because the original tree was changed in the postorder function.
© 2024 OneMinuteCode. All rights reserved.