-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearchTree.cpp
239 lines (223 loc) · 5.2 KB
/
binarySearchTree.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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left = nullptr;
node *right = nullptr;
};
node *st[100];
int TOS = -1;
void mainMenu()
{
cout << "\n ===========================Main Menu===========================" << endl;
cout << " 1. Insert Node" << endl;
cout << " 2. Delete Node" << endl;
cout << " 3. Search Node" << endl;
cout << " 4. PreOrder Travesal" << endl;
cout << " 5. InOrder Travesal" << endl;
cout << " 6. PostOrder Travesal" << endl;
cout << " 10. Exit" << endl;
cout << "Enter your choice: " << endl;
}
node *Insert(node *root, int key)
{
if (root == nullptr)
{
node *ptr = new node();
ptr->data = key;
return ptr;
}
if (root->data > key)
root->left = Insert(root->left, key);
if (root->data <= key)
root->right = Insert(root->right, key);
return root;
}
node *Delete(node *root, int key)
{
node *temp = root;
node *parent = nullptr;
while (temp != nullptr && temp->data != key)
{
parent = temp;
if (key < temp->data)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
if (root == nullptr)
{
return root;
}
// Case 1: Leaf node
if (temp->left == nullptr && temp->right == nullptr)
{
if (temp == root)
{
delete temp;
root = nullptr;
}
else if (parent->left == temp)
{
parent->left = nullptr;
delete temp;
}
else
{
parent->right = nullptr;
delete temp;
}
}
// Case 2: node with only left subtree
else if (temp->right == nullptr)
{
if (temp == root)
{
root = temp->left;
}
else if (parent->left == temp)
{
parent->left = temp->left;
}
else
{
parent->right = temp->left;
}
delete temp;
}
// Case 3: node with only right subtree
else if (temp->left == nullptr)
{
// node to be deleted is root node
if (temp == root)
{
root = temp->right;
}
else if (parent->left == temp)
{
parent->left = temp->right;
}
else
{
parent->right = temp->right;
}
delete temp;
}
// Case 4: node with both the subtrees
else
{
node *replacement = temp->right;
node *RightParent = temp;
// Finding minimum node in the right subtree of the node to be deleted
while (replacement->left != nullptr)
{
RightParent = replacement;
replacement = replacement->left;
}
temp->data = replacement->data;
if (RightParent->left == replacement)
{
RightParent->left = replacement->right;
}
else
{
RightParent->right = replacement->right;
}
delete replacement;
}
return root;
}
void PreOrderTraversal(node *root)
{ // node->left->right
if (root == nullptr)
return;
cout << root->data << " ";
PreOrderTraversal(root->left);
PreOrderTraversal(root->right);
}
void InOrderTraversal(node *root)
{ // left->node->right
if (root == nullptr)
return;
InOrderTraversal(root->left);
cout << root->data << " ";
InOrderTraversal(root->right);
}
void PostOrderTraversal(node *root)
{ // left->right->node
if (root == nullptr)
return;
PostOrderTraversal(root->left);
PostOrderTraversal(root->right);
cout << root->data << " ";
}
node *searchElement(node *root, int key)
{
if (root == nullptr)
return nullptr;
if (root->data == key)
return root;
if (root->data < key)
return searchElement(root->right, key);
if (root->data > key)
return searchElement(root->left, key);
return nullptr;
}
int main()
{
node *root = nullptr;
int userChoice = 0;
while (userChoice != 10)
{
mainMenu();
cin >> userChoice;
switch (userChoice)
{
case 1:
int data;
cout << "Enter the data to insert: " << endl;
cin >> data;
root = Insert(root, data);
InOrderTraversal(root);
break;
case 2:
{
int key;
cout << "Enter the element to Delete: ";
cin >> key;
Delete(root, key);
PreOrderTraversal(root);
break;
}
case 3:
{
int key;
cout << "Enter the element to search: ";
cin >> key;
searchElement(root, key) ? cout << "Element found"
: cout << "Element not found.";
}
case 4:
PreOrderTraversal(root);
break;
case 5:
InOrderTraversal(root);
break;
case 6:
PostOrderTraversal(root);
break;
case 10:
cout << "Exiting...";
userChoice = 10;
break;
default:
break;
}
}
return 0;
}