-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTree.c
186 lines (157 loc) · 3.6 KB
/
Tree.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include<stdlib.h>
#include<stdio.h>
//Tree node
struct bin_tree
{
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree)) //Creation of the tree node
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data) //If the incoming node val is less than the tree data insert in Left
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data) //If the incoming node val is greater than the tree data insert in right
{
insert(&(*tree)->right, val);
}
}
//<root><left><right>
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\t",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
//<left><root><right>
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\t",tree->data);
print_inorder(tree->right);
}
}
//<left><right><root>
void print_postorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
print_inorder(tree->right);
printf("%d\t",tree->data);
}
}
//<right><root><left>
void print_descend(node * tree)
{
if (tree)
{
print_descend(tree->right);
printf("%d\t",tree->data);
print_descend(tree->left);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
//if the val less than root val search in the left subtree
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
//if the val greater than root val search in the right subtree
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
//val equal to the root return the val
else if(val == (*tree)->data)
{
return *tree;
}
}
void getMin(node* tree)
{
//Traversing to the left end to get the minimum element
while(tree->left != NULL)
{
tree = tree->left;
}
printf("Minimum element: %d\n",tree->data);
}
void getMax(node* tree)
{
//Traversing to the right end to get the maximum element
while(tree->right != NULL)
{
tree = tree->right;
}
printf("Maximum element: %d\n",tree->data);
}
void main()
{
node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);
//Inorder traversal gives the ascending order
printf("\nIn Order Display\n");
print_inorder(root);
printf("\nPost Order Display\n");
print_postorder(root);
printf("\nDescending Order Display\n");
print_descend(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("\nSearched node=%d\n", tmp->data);
}
else
{
printf("\nData Not found in tree.\n");
}
getMin(root);
getMax(root);
/* Deleting all nodes of tree */
deltree(root);
}