-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary search tree.cpp
269 lines (268 loc) · 4.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
#include<iostream>
using namespace std;
template<class T>
class BSTnode
{
public:
T info;
BSTnode<T> *left, *right;
BSTnode()
{
left = right = NULL;
}
BSTnode(const T& e, BSTnode<T> *l = 0, BSTnode *r = 0)
{
info = e;
left = l;
right = r;
}
};
template<class T>
class BST
{
public:
BSTnode<T> *root;
//T* Bsearch(BSTnode<T>* p, const T& el);
//void breadthFirst();
void inorder(BSTnode<T> *p);
void preorder(BSTnode<T> *p);
void postorder(BSTnode<T>* p);
void display(BSTnode<T>*, T);
void insert(BSTnode<T>*, BSTnode<T>*);
T maxHeight(BSTnode<T>* );
void printGivenLevel(BSTnode<T>* , T);
//void deleteNode(BSTnode<T>*, T);
BST()
{
root = NULL;
}
};
template<class T>
void BST<T>::display(BSTnode<T> *ptr, T level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level + 1);
cout << endl;
if (ptr == root)
cout << "Root->: ";
else
{
for (i = 0;i < level;i++)
cout << " ";
}
cout << ptr->info;
display(ptr->left, level + 1);
}
}
template<class T>
void BST<T>::printGivenLevel(BSTnode<T>* root, T level)
{
if (root == NULL)
return;
if (level == 1)
cout << root->info << " ";
else if (level > 1)
{
printGivenLevel(root->left, level - 1);
printGivenLevel(root->right, level - 1);
}
}
template <class T>
T BST<T>::maxHeight(BSTnode<T>* node)
{
if (node == NULL)
return 0;
else
{
int lHeight = maxHeight(node->left);
int rHeight = maxHeight(node->right);
if (lHeight > rHeight)
return(lHeight + 1);
else return(rHeight + 1);
}
}
template<class T>
void BST<T>::insert(BSTnode<T> *tree, BSTnode<T> *newnode)
{
if (root == NULL)
{
root = new BSTnode<T>;
root->info = newnode-> info;
root->left = NULL;
root->right = NULL;
cout << " \n Root node is added";
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout << "\n Node added to left";
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout << " \n Node added to right";
return;
}
}
}
/*template<class T>
T* BST<T>::Bsearch(BSTnode<T>* p, const T& el) const
{
while (p !=0)
if(el==p->el)
return &p->el;
else if(el <p->el)
p=p->left;
else
p=p->right;
return 0;
}
template<class T>
void BST<T>::breadthFirst()
{
Queue<BSTnode<T>* queue;
BSTnode<T> *p=root;
if(p !=0)
{
queue.enqueue(p);
while(!queue.empty())
{
p=queue.dqueue();
visit(p);
if(p->left !=0)
queue.enqueue(p->left);
if(p->right !=0)
queue.enqueue(p->right);
}
}
}
*/
template<class T>
void BST<T>::inorder(BSTnode<T> *p)
{
if (root == NULL)
{
cout << "Tree is empty" << endl;
return;
}
if (p != NULL)
{
inorder(p->left);
cout << p->info << " ";
inorder(p->right);
}
}
template<class T>
void BST<T>::preorder(BSTnode<T> *p)
{
if (root == NULL)
{
cout << "Tree is empty" << endl;
return;
}
if (p != NULL)
{
cout << p->info << " ";
preorder(p->left);
preorder(p->right);
}
}
template<class T>
void BST<T>::postorder(BSTnode<T>* p)
{
if (root == NULL)
{
cout << "Tree is empty" << endl;
return;
}
if (p != NULL)
{
postorder(p->left);
postorder(p->right);
cout << p->info << " ";
}
}
int main()
{
int choice, num, h;
char ch;
BST<int> bst;
BSTnode<int> *temp, *m;
while (1)
{
cout << " \n ------MENU------";
cout << " \n 1. Insert element";
cout << " \n 2. Inorder Traversal";
cout << " \n 3. Preorder Traversal";
cout << " \n 4. Postorder Traversal";
cout << " \n 5. Display";
cout << " \n 6. Height";
cout << " \n 7. Print given level";
cout << " \n 8. Exit";
cout << "\n Enter choice";
cin >> choice;
switch (choice)
{
case 1:
temp = new BSTnode<int>;
cout << " \n Enter the number to be insert";
cin >> temp->info;
bst.insert(bst.root, temp);
break;
case 2:
cout << " \n Inorder Traversal of BST ";
bst.inorder(bst.root);
cout << endl;
break;
case 3:
cout << " \n Preorder Traversal of BST";
bst.preorder(bst.root);
cout << endl;
break;
case 4:
cout << " \n Postorder Traversal of BST";
bst.postorder(bst.root);
cout << endl;
break;
case 5:
cout << " \n Display BST";
bst.display(bst.root, 1);
cout << endl;
break;
case 6:
h = bst.maxHeight(bst.root);
cout << "\nHeight=" << h;
break;
case 7:
cout << "Enter level :";
cin >> h;
bst.printGivenLevel(bst.root, h );
break;
case 8 :
exit(1);
default:
cout << " Wrong choice entered";
}
}
}