-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree.cpp
288 lines (266 loc) · 6.82 KB
/
Binary Search Tree.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <stdlib.h>
using namespace std;
struct bst_node{
int data;
struct bst_node* left;
struct bst_node* right;
};
bst_node* GetNode(int item)
{
bst_node* new_node=(struct bst_node* )malloc(sizeof(struct bst_node));
new_node->data=item;
new_node->left=0;
new_node->right=0;
return new_node;
}
bst_node* Insert(bst_node* root, int val)
{
if (root==0) //empty
root=GetNode(val);
else if (val<=root->data)
root->left=Insert(root->left,val);
else
root->right=Insert(root->right,val);
return root;
}
bool Search(bst_node* root,int val)
{
if (root==0)
return false;
else if (root->data==val) //if key is equal to root
return true;
else if (val<=root->data) // if key is less than root
return Search(root->left,val);
else // if key is greater than root
return Search(root->right,val);
}
void preorder(struct bst_node* root)
{
if (root==0)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
void Inorder(struct bst_node* root)
{
if (root==0)
return;
Inorder(root->left);
cout<<root->data<<" ";
Inorder(root->right);
}
void Postorder(struct bst_node* root)
{
if (root==0)
return;
Postorder(root->left);
Postorder(root->right);
cout<<root->data<<" ";
}
int Smallest(struct bst_node* root)
{
struct bst_node* current=root;
while(current->left!=0)
current=current->left;
return (current->data);
}
int largest(struct bst_node* root)
{
struct bst_node* current=root;
while(current->right!=0)
current=current->right;
return (current->data);
}
bst_node* Smallest_Delete(struct bst_node* root)
{
struct bst_node* current=root;
while(current->left!=0)
current=current->left;
return (current);
}
bst_node* Delete_node(struct bst_node* root,int key)
{
if (root==0)
return root;
if(key<root->data)
root->left=Delete_node(root->left,key);
else if (key>root->data)
root->right=Delete_node(root->right,key);
else //node with at most one child
{
if(root->left==0)
{
struct bst_node* temp=root->right;
free(root);
return temp;
}
else if (root->right==0)
{
struct bst_node* temp=root->left;
free(root);
return temp;
}
//node with two children
struct bst_node* temp;
temp=Smallest_Delete(root->right);
root->data=temp->data;
root->right=Delete_node(root->right,temp->data);
}
return root;
}
int count(bst_node* node)
{
int c=1;
if(node==0)
return 0;
else
{
c+=count(node->left);
c+=count(node->right);
return c;
}
}
int findMax(int a, int b)
{
if(a>=b)
return a;
else
return b;
}
int Height(bst_node* root)
{
if(root==0)
return 0;
return findMax(Height(root->left),Height(root->right))+1;
}
int count_internal(bst_node* root)
{
int c=1;
if (root!=0)
{
count_internal(root->left);
if(root->left!=0 || root->right!=0)
c++;
count_internal(root->right);
}
return c;
}
int count_external(struct bst_node* root)
{
if(root==0)
return 0;
if(root->left==0 && root->right==0)
return 1;
else
return (count_external(root->left)+count_external(root->right));
}
int main ()
{
bst_node* root;
root=0;
int value[20];
int i,s;
int choice,del;
do{
cout<<endl<<"Enter 1 for Insert element."<<endl;
cout<<"Enter 2 for Preorder Traversal."<<endl;
cout<<"Enter 3 for Inorder Traversal."<<endl;
cout<<"Enter 4 for Postorder Traversal."<<endl;
cout<<"Enter 5 for Smallest Element."<<endl;
cout<<"Enter 6 for Largest Element."<<endl;
cout<<"Enter 7 for Delete an item."<<endl;
cout<<"Enter 8 for count total number of nodes."<<endl;
cout<<"Enter 9 for count number of External nodes."<<endl;
cout<<"Enter 10 for count number of Internal nodes."<<endl;
cout<<"Enter 11 for height of tree."<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the values: ";
cin>>i;
root=Insert(root,i);
break;
case 2:
if (root==0)
cout<<"Empty Tree."<<endl;
else
{
cout<<"Preorder Reversal: ";
preorder(root);
cout<<endl;
}
break;
case 3:
if (root==0)
cout<<"Empty Tree."<<endl;
else
{
cout<<"Inorder Traversal: ";
Inorder(root);
cout<<endl;
}
break;
case 4:
if (root==0)
cout<<"Empty Tree."<<endl;
else
{
cout<<"Postorder Reversal: ";
Postorder(root);
cout<<endl;
}
break;
case 5:
if (root==0)
cout<<"Empty Tree."<<endl;
else
cout<<"Smallest element: "<<Smallest(root)<<endl;
break;
case 6:
if (root==0)
cout<<"Empty Tree."<<endl;
else
cout<<"Largest element: "<<largest(root)<<endl;
break;
case 7:
if (root==0)
cout<<"Empty Tree."<<endl;
else
{
cout<<"Enter the value to delete: ";
cin>>del;
root=Delete_node(root,del);
cout<<endl;
}
break;
case 8:
cout<<"Total number of nodes: "<<count(root)<<endl;
break;
case 9:
if (root==0)
cout<<"Empty Tree."<<endl;
else
cout<<"External Nodes: "<<count_external(root)<<endl;
break;
case 10:
if (root==0)
cout<<"Empty Tree."<<endl;
else
cout<<"Internal Nodes: "<<count_internal(root)<<endl;
break;
case 11:
if (root==0)
cout<<"Empty Tree."<<endl;
else
cout<<"Height of the tree: "<<Height(root)<<endl;
break;
default:
return 0;
}
}while (choice<12);
return 0;
}