-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.cpp
239 lines (209 loc) · 6.22 KB
/
BST.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 <iostream>
#include <queue>
#include <vector>
class Node {
public:
int key;
Node* left;
Node* right;
Node(int value) : key(value), left(nullptr), right(nullptr) {}
};
class BinarySearchTree {
private:
Node* root;
Node* insertRec(Node* node, int key) {
if (node == nullptr) {
return new Node(key);
}
if (key < node->key) {
node->left = insertRec(node->left, key);
} else if (key > node->key) {
node->right = insertRec(node->right, key);
}
return node;
}
Node* deleteRec(Node* node, int key) {
if (node == nullptr) return node;
if (key < node->key) {
node->left = deleteRec(node->left, key);
} else if (key > node->key) {
node->right = deleteRec(node->right, key);
} else {
// Node with only one child or no child
if (node->left == nullptr) {
Node* temp = node->right;
delete node;
return temp;
} else if (node->right == nullptr) {
Node* temp = node->left;
delete node;
return temp;
}
// Node with two children: Get the inorder successor
Node* temp = minValueNode(node->right);
node->key = temp->key;
node->right = deleteRec(node->right, temp->key);
}
return node;
}
Node* minValueNode(Node* node) {
Node* current = node;
while (current && current->left != nullptr) {
current = current->left;
}
return current;
}
Node* searchRec(Node* node, int key) {
if (node == nullptr || node->key == key) return node;
if (key < node->key) return searchRec(node->left, key);
return searchRec(node->right, key);
}
void inOrderRec(Node* node) {
if (node != nullptr) {
inOrderRec(node->left);
std::cout << node->key << " ";
inOrderRec(node->right);
}
}
void preOrderRec(Node* node) {
if (node != nullptr) {
std::cout << node->key << " ";
preOrderRec(node->left);
preOrderRec(node->right);
}
}
void postOrderRec(Node* node) {
if (node != nullptr) {
postOrderRec(node->left);
postOrderRec(node->right);
std::cout << node->key << " ";
}
}
void dfsRec(Node* node, std::vector<int>& result) {
if (node != nullptr) {
result.push_back(node->key);
dfsRec(node->left, result);
dfsRec(node->right, result);
}
}
public:
BinarySearchTree() : root(nullptr) {}
void insert(int key) {
root = insertRec(root, key);
}
void deleteNode(int key) {
root = deleteRec(root, key);
}
Node* search(int key) {
return searchRec(root, key);
}
void inOrder() {
std::cout << "In-order Traversal: ";
inOrderRec(root);
std::cout << std::endl;
}
void preOrder() {
std::cout << "Pre-order Traversal: ";
preOrderRec(root);
std::cout << std::endl;
}
void postOrder() {
std::cout << "Post-order Traversal: ";
postOrderRec(root);
std::cout << std::endl;
}
std::vector<int> dfs() {
std::vector<int> result;
dfsRec(root, result);
return result;
}
std::vector<int> bfs() {
std::vector<int> result;
if (root == nullptr) return result;
std::queue<Node*> queue;
queue.push(root);
while (!queue.empty()) {
Node* current = queue.front();
queue.pop();
result.push_back(current->key);
if (current->left != nullptr) queue.push(current->left);
if (current->right != nullptr) queue.push(current->right);
}
return result;
}
};
void menu() {
std::cout << "\nMenu:\n";
std::cout << "1. Insert\n";
std::cout << "2. Delete\n";
std::cout << "3. Search\n";
std::cout << "4. In-order Traversal\n";
std::cout << "5. Pre-order Traversal\n";
std::cout << "6. Post-order Traversal\n";
std::cout << "7. DFS Traversal\n";
std::cout << "8. BFS Traversal\n";
std::cout << "9. Exit\n";
}
int main() {
BinarySearchTree bst;
int choice, value;
while (true) {
menu();
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter value to insert: ";
std::cin >> value;
bst.insert(value);
break;
case 2:
std::cout << "Enter value to delete: ";
std::cin >> value;
bst.deleteNode(value);
break;
case 3:
std::cout << "Enter value to search: ";
std::cin >> value;
if (bst.search(value)) {
std::cout << "Value " << value << " found in the tree.\n";
} else {
std::cout << "Value " << value << " not found in the tree.\n";
}
break;
case 4:
bst.inOrder();
break;
case 5:
bst.preOrder();
break;
case 6:
bst.postOrder();
break;
case 7: {
std::cout << "DFS Traversal: ";
std::vector<int> dfsResult = bst.dfs();
for (int key : dfsResult) {
std::cout << key << " ";
}
std::cout << std::endl;
break;
}
case 8: {
std::cout << "BFS Traversal: ";
std::vector<int> bfsResult = bst.bfs();
for (int key : bfsResult) {
std::cout << key << " ";
}
std::cout << std::endl;
break;
}
case 9:
std::cout << "Exiting...\n";
return 0;
default:
std::cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}