-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
185 lines (172 loc) · 2.73 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
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int data;
struct tree *left,*right;
};
typedef struct tree *TREE;
TREE root=NULL;
void attach_node(TREE node, TREE prev, TREE cur);
void create()
{
TREE node,prev,cur;
node=(TREE)malloc(sizeof(struct tree));
if(node==NULL)
{
printf("\nNOT ENOUGH SPACE!!!");
}
else
{
printf("\nEnter the data:");
scanf("%d",&node->data);
node->left=NULL;
node->right=NULL;
attach_node(node,prev,cur);
}
}
void attach_node(TREE node,TREE prev,TREE cur)
{
if(root==NULL)
{
root=node;
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(node->data < cur->data)
{
cur=cur->left;
}
else
{
cur=cur->right;
}
}
if(node->data < prev->data)
{
prev->left=node;
}
else
{
prev->right=node;
}
}
}
//void display()
//{
// TREE temp;
// temp=root;
// if(temp==NULL)
// {
// printf("\nBinary search tree is empty");
// }
// else
// {
// while(temp!=NULL)
// {
// printf("\n%d",temp->data);
// }
// free(temp);
// }
//}
void preorder_traversal(TREE root)
{
if(root==NULL)
return;
else
{
printf("\nThe preorder traversal is: %d\n",root->data);
preorder_traversal(root->left);
preorder_traversal(root->right);
}
}
void inorder_traversal(TREE root)
{
if(root==NULL)
return;
else
{
inorder_traversal(root->left);
printf("\n%d",root->data);
inorder_traversal(root->right);
}
}
void postorder_traversal(TREE root)
{
if(root==NULL)
return;
else
{
postorder_traversal(root->left);
postorder_traversal(root->right);
printf("\n%d",root->data);
}
}
void search_bst()
{
TREE temp;
temp=root;
int key,found=0;
printf("\nEnter the key or number to search:");
scanf("%d",&key);
if(root==NULL)
{
printf("\nThe list is empty!!!");
return;
}
else
{
while(temp!= NULL)
{
if(key<temp->data)
{
temp=temp->left;
}
else if (key > temp->data)
{
temp=temp->right;
}
else
{
found=1;
printf("\n%d is present!!!", key);
return;
}
}
printf("Not found");
}
}
int main()
{
int choice;
while(1)
{
printf("\nBINARY TREE OPERATIONS\n------------------------------\n");
printf("\n1.Create Binary search tree\n2.preorder traversal\n3.inorder traversal\n4.postorder traversal\n5.Search BST \n6.Diplay BST\n7.Exit\n");
printf("\nEnter your choice:");
scanf("\n%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: preorder_traversal(root);
break;
case 3: inorder_traversal(root);
break;
case 4: postorder_traversal(root);
break;
case 5: search_bst();
break;
case 6: //display();
break;
case 7: return(0);
default: printf("\nInvalid choice!!!");
}
}
return(0);
}