forked from satojkovic/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
326 lines (291 loc) · 9.76 KB
/
Copy pathbinary_search_tree.cpp
File metadata and controls
326 lines (291 loc) · 9.76 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
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <iostream>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode() {
data = 0;
left = nullptr;
right = nullptr;
}
TreeNode(int elem) {
data = elem;
left = nullptr;
right = nullptr;
}
};
class BinarySearchTree {
TreeNode* root;
int num_nodes;
public:
BinarySearchTree(int elem) {
root = new TreeNode(elem);
num_nodes = 1;
}
TreeNode* getRoot() {
return root;
}
bool remove_leaf(TreeNode* parent, TreeNode* node) {
// Delete the leaf node by making the leaf node's parent's left or right child is nullptr
if (root->data == node->data) {
delete root;
root = nullptr;
return true;
} else if (node->data < parent->data) {
delete parent->left;
parent->left = nullptr;
return true;
} else {
delete parent->right;
parent->right = nullptr;
return true;
}
}
bool remove_node_with_left_only(TreeNode* parent, TreeNode* node) {
// Delete the node by making the left child of that node the node's parent's right or left child
if (root->data == node->data) {
delete root;
root = node->left;
return true;
} else if (node->data < parent->data) {
delete parent->left;
parent->left = node->left;
return true;
} else {
delete parent->right;
parent->right = node->left;
return true;
}
}
bool remove_node_with_right_only(TreeNode* parent, TreeNode* node) {
// Similar to the above left only case
if (root->data == node->data) {
delete root;
root = node->right;
return true;
} else if (node->data < parent->data) {
delete parent->left;
parent->left = node->right;
return true;
} else {
delete parent->right;
parent->right = node->right;
return true;
}
}
bool remove_node_with_two_children(TreeNode* parent, TreeNode* node) {
// Find the smallest value node in the right subtree of the current node
TreeNode* least_node = findLeastNode(node);
int data = least_node->data;
// Swap the current value with the samllest value
bool is_deleted = remove(root, data);
node->data = data;
return is_deleted;
}
TreeNode* findLeastNode(TreeNode* node) {
TreeNode* current = node;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
bool remove(TreeNode* node, int elem) {
if (root == nullptr) {
return false;
}
// Search the elem
TreeNode* parent;
while (node != nullptr && node->data != elem) {
parent = node;
if (elem < node->data) {
node = node->left;
} else {
node = node->right;
}
}
if (node == nullptr) {
// Return false when the elem is not found
return false;
} else if (node->left == nullptr and node->right == nullptr) {
// Deleting a leaf node
return remove_leaf(parent, node);
} else if (node->right == nullptr) {
// Deleting a node with a left child only
return remove_node_with_left_only(parent, node);
} else if (node->left == nullptr) {
// Deleting a node with a right child only
return remove_node_with_right_only(parent, node);
} else {
// Deleting a node with two children
return remove_node_with_two_children(parent, node);
}
}
TreeNode* search(int elem) {
TreeNode* current_node = root;
while (current_node && current_node->data != elem) {
if (elem < current_node->data) {
current_node = current_node->left;
} else {
current_node = current_node->right;
}
}
// after the loop, we'll have either the searched value
// or nullptr in case the value was not found
return current_node;
}
TreeNode* search(TreeNode* node, int elem) {
if (node == nullptr) {
return nullptr;
} else if (node->data == elem) {
return node;
} else if (elem < node->data) {
return search(node->left, elem);
} else {
return search(node->right, elem);
}
}
bool contains(int elem) {
return contains_r(root, elem);
}
bool contains_r(TreeNode* node, int elem) {
if (node == nullptr) {
return false;
}
if (elem < node->data) {
return contains_r(node->left, elem);
} else if (elem > node->data)
{
return contains_r(node->right, elem);
} else {
return true;
}
}
bool addNode(int elem) {
if (contains(elem)) {
return false;
} else {
root = addNode_r(root, elem);
num_nodes++;
return true;
}
}
TreeNode* addNode_r(TreeNode* node, int elem) {
// base case
// create new node and return it to the previous leaf(null) node.
if (node == nullptr) {
node = new TreeNode(elem);
} else if (elem < node->data) {
node->left = addNode_r(node->left, elem);
} else {
node->right = addNode_r(node->right, elem);
}
return node;
}
bool addNode_i(int elem) {
if (contains(elem)) {
return false;
}
if (getRoot() == nullptr) {
root = new TreeNode(elem);
num_nodes++;
return true;
}
// Starting from the root
TreeNode* node = root;
TreeNode* parent;
while (node) {
parent = node;
if (elem < node->data) {
node = node->left;
} else {
node = node->right;
}
}
// When the current node becomes nullptr, it inserts the value to its parent.
if (elem < parent->data) {
parent->left = new TreeNode(elem);
} else {
parent->right = new TreeNode(elem);
}
num_nodes++;
return true;
}
void inOrderPrint(TreeNode* node) {
if (node != nullptr) {
inOrderPrint(node->left);
std::cout << node->data << std::endl;
inOrderPrint(node->right);
}
}
void preOrderPrint(TreeNode* node) {
if (node != nullptr) {
std::cout << node->data << std::endl;
preOrderPrint(node->left);
preOrderPrint(node->right);
}
}
void postOrderPrint(TreeNode* node) {
if (node != nullptr) {
postOrderPrint(node->left);
postOrderPrint(node->right);
std::cout << node->data << std::endl;
}
}
};
int main() {
BinarySearchTree bst(6);
std::cout << "root value : " << bst.getRoot()->data << std::endl;
bst.addNode(4);
bst.addNode(9);
bst.addNode(5);
bst.addNode(2);
bst.addNode(8);
bst.addNode(12);
std::cout << "inorder print" << std::endl;
bst.inOrderPrint(bst.getRoot());
std::cout << "preorder print" << std::endl;
bst.preOrderPrint(bst.getRoot());
std::cout << "postorder print" << std::endl;
bst.postOrderPrint(bst.getRoot());
BinarySearchTree bst2(6);
std::cout << "root value : " << bst2.getRoot()->data << std::endl;
bst2.addNode(4);
bst2.addNode(9);
bst2.addNode(5);
bst2.addNode(2);
bst2.addNode(8);
bst2.addNode(12);
std::cout << "inorder print" << std::endl;
bst2.inOrderPrint(bst2.getRoot());
std::cout << "preorder print" << std::endl;
bst2.preOrderPrint(bst2.getRoot());
std::cout << "post order print" << std::endl;
bst2.postOrderPrint(bst2.getRoot());
int target = 12;
if (bst.search(target)) {
printf("Recursive search %d -> Found\n", target);
} else {
printf("Recursive search %d -> Not found\n", target);
}
if (bst.search(bst.getRoot(), target)) {
printf("Iterative search %d -> Found\n", target);
} else {
printf("Iterative search %d -> Not found\n", target);
}
int delete_target = 5;
if (bst.remove(bst.getRoot(), delete_target)) {
printf("Delete %d\n", delete_target);
bst.inOrderPrint(bst.getRoot());
} else {
printf("Not found %d\n", delete_target);
}
delete_target = -1;
if (bst.remove(bst.getRoot(), delete_target)) {
printf("Delete %d\n", delete_target);
bst.inOrderPrint(bst.getRoot());
} else {
printf("Not found %d\n", delete_target);
}
return 0;
}