-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
313 lines (296 loc) · 7.39 KB
/
binary_search_tree.cpp
File metadata and controls
313 lines (296 loc) · 7.39 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// BINARY SEARCH TREE IMPLEMENTATION IN C++
#include<bits/stdc++.h>
using namespace std;
// BST Class
template<class T>
class BST{
// Node Class
class Node{
public:
T data;//data of Node
Node* left; //left pointer
Node* right;// right pointer
Node() // Default Constructer
{
left=NULL;
right=NULL;
}
Node(T x) // Parameterized Constructor
{
data=x;
left=NULL;
right=NULL;
}
~Node() // Destructor
{
cout << "NODE DESTROYED!!!\n";
}
};
// PRIVATE
// Member Functions
Node* insertion(Node*,T);
Node* deletion(Node*,T);
void preorder(Node*);
void inorder(Node*);
void postorder(Node*);
Node* findMin(Node*);
Node* findMax(Node*);
bool isEmpty();
// Data Members
unordered_map<T,T>m;
Node* head=NULL;
public:
// PUBLIC
BST() // Default Constructor
{
cout << "\n\n..............Welcome to BST Implementation..............\n\n";
}
~BST() // Destructor
{
cout << "\n\n..............THE END!!!!..............\n\n";
}
// Member Functions
void insert();
void deleteNode();
void Traversals();
void Top();
};
// Wrapper Function for Insertion Opertaion..
template<typename T>
void BST<T>::insert()
{
cout << "Insert--> ";
T val;
cin >> val;
// Checks for Duplicate Element..
if(m.find(val)==m.end())
{
head=insertion(head,val);
// Insert into the Map.
m[val]=1;
return;
}
cout << "Element Already Exist in BST!!!!!\n";
}
// Insert Function to Insert Node in the BST..
template<typename T>
typename BST<T>::Node* BST<T>::insertion(Node* root,T val)
{
if(root==NULL)
root=new Node(val);
else if(root->data<val)
root->right=insertion(root->right,val);
else
root->left=insertion(root->left,val);
return root;
}
// Wrapper Function for Deletion Opertaion..
template<typename T>
void BST<T>:: deleteNode()
{
if(!isEmpty())
{
cout << "Delete--> ";
T val;
cin >> val;
// Checks if Node exist in BST
if(m.find(val)==m.end())
cout << "Node doesn't EXIST!!!\n";
else
{
head=deletion(head,val);
// Then erase it from the Map as well.
m.erase(val);
}
}
}
// Delete Function to Delete node from the BST..
template<typename T>
typename BST<T>::Node* BST<T>::deletion(Node* root,T val)
{
if(root->data>val) // if root->data is greater than value then goto left
root->left=deletion(root->left,val);
else if(root->data<val) // if root->data is lesser than value then goto right
root->right=deletion(root->right,val);
else
{
// Case-1 (0- Child)
if(root->left==NULL && root->right==NULL)
{
delete(root);
root=NULL;
return root;
}
// Case-1 (1- Child(RIGHT))
else if (root->left==NULL)
{
Node* temp=root;
root=root->right;
delete(temp);
return root;
}
// Case-1 (1- Child(LEFT))
else if (root->right==NULL)
{
Node* temp=root;
root=root->left;
delete(temp);
return root;
}
// Case-1 (2- Child)
else
{
cout << "DELETE Node according to--> \n";
cout << "1- INORDER PREDECESSOR\n";
cout << "2- INORDER SUCCESSOR\n";
int x;
cout << "Enter--> ";
cin >> x;
while(x<1 || x>2)
{
cout << "Enter a Valid Input...\n";
cout << "Enter--> ";
cin >> x;
}
Node* temp=NULL;
if(x==1) // Using INORDER PREDECESSOR
{
temp=findMax(root->left);
root->data=temp->data;
root->left=deletion(root->left,temp->data);
}
else if(x==2) // Using INORDER SUCCESSOR
{
temp=findMin(root->right);
root->data=temp->data;
root->right=deletion(root->right,temp->data);
}
}
}
return root;
}
// Finds the Minimum from the SubTree
template<typename T>
typename BST<T>::Node* BST<T>::findMin(Node* root)
{
while(root->left!=NULL)
root=root->left;
return root;
}
// Finds the Maximum from the SubTree
template<typename T>
typename BST<T>::Node* BST<T>::findMax(Node* root)
{
while(root->right!=NULL)
root=root->right;
return root;
}
// Traversals in the BST
template<typename T>
void BST<T>::Traversals()
{
if(!isEmpty())
{
cout << "1-PREORDER TRAVERSAL\n";
cout << "2-INORDER TRAVERSAL\n";
cout << "3-POSTORDER TRAVERSAL\n";
cout << "Select Option--> ";
int opt;
cin >> opt;
switch (opt)
{
case 1:
cout << "\nPre-Order--> ";
preorder(head);
break;
case 2:
cout << "\nIn-Order--> ";
inorder(head);
break;
case 3:
cout << "\nPost-Order--> ";
postorder(head);
break;
default:
cout << "\nInvalid Option...";
break;
}
cout << endl << endl;
}
}
// Implements PREORDER Traversal
template<typename T>
void BST<T>::preorder(Node* root)
{
if(root==NULL)
return;
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
// Implements INORDER Traversal
template<typename T>
void BST<T>::inorder(Node* root)
{
if(root==NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// Implements POSTORDER Traversal
template<typename T>
void BST<T>::postorder(Node* root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
// Finds the Root/Top Node from the BST.
template<typename T>
void BST<T>::Top()
{
cout << "Top Element--> " << head->data << endl;
}
// Checks if the BST is Empty or NOT..
template<typename T>
bool BST<T>::isEmpty()
{
if(head==NULL)
{
cout << "\nBST is Empty...\n\n";
return true;
}
return false;
}
// Driver Code
int main()
{
BST<int>obj; // Object of BST Class.
cout << "# INSTRUCTIONS--> \n";
cout << "1- Insert\n";
cout << "2- Delete\n";
cout << "3- Traversals\n";
cout << "4- Top\n";
cout << "Any Character to EXIT!\n\n";
int n;
cout << "Enter--> ";
while(cin >> n)
{
if(n==1)
obj.insert(); // Calling Insert Function
else if(n==2)
obj.deleteNode(); // Calling Delete Function
else if(n==3)
obj.Traversals(); // Calling Traversal Function
else if(n==4)
obj.Top(); // Calling Top Function
else
cout << "Choose VALID OPTION!!!\n";
cout << "Enter--> ";
}
// delete(obj);
return 0;
}