-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathtree_traversal.c
149 lines (131 loc) · 2.73 KB
/
tree_traversal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} Node;
typedef struct tree
{
Node *root;
} Tree;
void tree_initialize(Tree *tree);
void tree_insert(Tree *tree, int data);
Node* create_Node(int data);
void inorder(Tree *tree);
void preorder(Tree *tree);
void postorder(Tree *tree);
Node* rec_Insert_Node(Node *root, int data);
void tree_destroy(Tree *tree);
int main()
{
int choice, loop = 1;
Tree my_tree;
tree_initialize(&my_tree);
while (loop)
{
scanf("%d", &choice);
switch (choice)
{
int number_of_elements, element, index;
int data;
case 1:
/* Insert elements */
scanf("%d", &element);
tree_insert(&my_tree, element);
break;
case 2:
/* Print elements in the inorder fashion */
preorder(&my_tree);
break;
case 3:
/* Print elements in the preorder fashion */
inorder(&my_tree);
break;
case 4:
/* Print elements in the postorder fashion */
postorder(&my_tree);
break;
default:
tree_destroy(&my_tree);
loop = 0;
break;
}
}
return 0;
}
void tree_initialize(Tree *tree)
{
tree->root=NULL;
}
Node* create_Node(int data){
Node *new = (Node *)malloc(sizeof(Node));
new->data = data;
new->left = new->right = NULL;
return new;
}
Node* rec_Insert_Node(Node *root, int data){
Node *new = create_Node(data);
if (root == NULL){
root = new;
}
else if (root->data > new->data){
root->left = rec_Insert_Node(root->left, data);
}
else if (root->data < new->data){
root->right = rec_Insert_Node(root->right, data);
}
return root;
}
void tree_insert(Tree *tree, int data){
tree->root=rec_Insert_Node(tree->root, data);
}
void tree_inorder(Node *r)
{
if(r!=NULL){
tree_inorder(r->left);
printf("%d ",r->data);
tree_inorder(r->right);
}
}
void tree_preorder(Node *r)
{
if (r != NULL){
printf("%d ", r->data);
tree_preorder(r->left);
tree_preorder(r->right);
}
}
void tree_postorder(Node *r)
{
if (r != NULL){
tree_postorder(r->left);
tree_postorder(r->right);
printf("%d ", r->data);
}
}
void postorder(Tree *t)
{
tree_postorder(t->root);
printf("\n");
}
void preorder(Tree *t)
{
tree_preorder(t->root);
printf("\n");
}
void inorder(Tree *t)
{
tree_inorder(t->root);
printf("\n");
}
void destroy(Node *r)
{
r=NULL;
}
void tree_destroy(Tree *t)
{
destroy(t->root);
}