-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearchtree.h
262 lines (225 loc) · 6.89 KB
/
binarysearchtree.h
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
#ifndef BINARY_SEARCH_TREE
#define BINARY_SEARCH_TREE
#include <functional>
#include <iostream>
#include "stack.h"
// Simple BST with recursively implemented basic operations
template <typename T>
class BinarySearchTree {
// Tree Node Template Class
template <typename X>
class BinarySearchTreeNode {
public:
BinarySearchTreeNode() : left(nullptr), right(nullptr) {}
BinarySearchTreeNode(T val) : left(nullptr), right(nullptr), data(val) {}
BinarySearchTreeNode(const BinarySearchTreeNode<T>& node) {
data = node.data;
left = node.left;
right = node.right;
}
BinarySearchTreeNode* left;
BinarySearchTreeNode* right;
X data;
};
// root node of the tree
BinarySearchTreeNode<T>* root;
// number of nodes found in the tree
unsigned int n;
void insertItem(BinarySearchTreeNode<T>*& node, T value);
// iterative insert func.
void insertItem(T value);
BinarySearchTreeNode<T>* searchItem(BinarySearchTreeNode<T>* node, T value);
void deleteTree(BinarySearchTreeNode<T>* node) {
if (node) {
// delete children first then the node itself
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
root = nullptr;
}
void deleteNode(BinarySearchTreeNode<T>*& node, T val);
public:
BinarySearchTree() : root(nullptr), n(0) {}
~BinarySearchTree() {
deleteTree(root);
n = 0;
}
void insert(T value) { insertItem(value); /*insertItem(root, value);*/ }
void deleteItem(T value) { deleteNode(root, value); }
BinarySearchTreeNode<T>* search(T value) { return searchItem(root, value); }
BinarySearchTreeNode<T>* findMin();
BinarySearchTreeNode<T>* findMax();
bool isEmpty() const { return size() == 0; }
BinarySearchTreeNode<T>* getRoot() const { return root; }
const unsigned int size() const { return n; }
// Traversals
void inOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc);
// iterative inorderwalk
void inOrderWalk(std::function<void(T val)> nodefunc);
void preOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc);
void postOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc);
};
/**
* @brief Function to delete a node by searching for its key.
* 3 possible options to check
* a) if the node is a leaf node, then simply delete it
* b) if the node has a left/right child, replace its key with the
* child's c) if the node has both left and right child, replace its key with
* its successor.
* @param node The node
* @param[in] val The key of the node to be deleted
*/
template <typename T>
void BinarySearchTree<T>::deleteNode(BinarySearchTreeNode<T>*& node, T val) {
// if there is no node nothing to delete
if (node == nullptr) {
return;
}
if (val < node->data) {
deleteNode(node->left, val);
} else if (val > node->data) {
deleteNode(node->right, val);
} else if (node->left != nullptr && node->right != nullptr) {
// find the successor of the right tree
BinarySearchTreeNode<T>* cur = node->right;
while (cur && cur->left != nullptr) {
cur = cur->left;
}
// update node key with its successor
node->data = cur->data;
deleteNode(node->right, cur->data);
} else {
BinarySearchTreeNode<T>* temp = node;
node = (node->left != nullptr) ? node->left : node->right;
delete temp;
n--;
}
}
template <typename T>
void BinarySearchTree<T>::insertItem(BinarySearchTreeNode<T>*& node, T value) {
// base case
if (node == nullptr) {
node = new BinarySearchTreeNode<T>(value);
n++;
}
// recursive case
if (value < node->data)
insertItem(node->left, value);
else if (value > node->data)
insertItem(node->right, value);
}
template <typename T>
BinarySearchTree<T>::BinarySearchTreeNode<T>* BinarySearchTree<T>::searchItem(
BinarySearchTreeNode<T>* node, T value) {
// base case either hit to null link or found
if (node == nullptr)
return nullptr;
else if (value == node->data)
return node;
if (value < node->data)
return searchItem(node->left, value);
else if (value > node->data)
return searchItem(node->right, value);
}
template <typename T>
void BinarySearchTree<T>::inOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc) {
if (node) {
inOrderWalk(node->left, nodefunc);
nodefunc(node->data);
inOrderWalk(node->right, nodefunc);
}
}
template <typename T>
void BinarySearchTree<T>::inOrderWalk(std::function<void(T val)> nodefunc) {
Stack<BinarySearchTreeNode<T>*> stack;
BinarySearchTreeNode<T>* cur = this->root;
bool done = false;
while (!done) {
if (cur != nullptr) {
stack.Push(cur);
cur = cur->left;
} else {
if (!stack.IsEmpty()) {
cur = stack.Peek();
stack.Pop();
nodefunc(cur->data);
cur = cur->right;
} else {
done = true;
}
}
}
}
template <typename T>
void BinarySearchTree<T>::preOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc) {
if (node) {
nodefunc(node->data);
preOrderWalk(node->left, nodefunc);
preOrderWalk(node->right, nodefunc);
}
}
template <typename T>
void BinarySearchTree<T>::postOrderWalk(BinarySearchTreeNode<T>* node,
std::function<void(T val)> nodefunc) {
if (node) {
postOrderWalk(node->left, nodefunc);
postOrderWalk(node->right, nodefunc);
nodefunc(node->data);
}
}
template <typename T>
BinarySearchTree<T>::BinarySearchTreeNode<T>* BinarySearchTree<T>::findMin() {
BinarySearchTreeNode<T>* cur = getRoot();
// check for the null case first
if (cur) {
// then traverse to the left
while (cur->left != nullptr) {
cur = cur->left;
}
}
return cur;
}
template <typename T>
BinarySearchTree<T>::BinarySearchTreeNode<T>* BinarySearchTree<T>::findMax() {
BinarySearchTreeNode<T>* cur = getRoot();
// check for the null case first
if (cur) {
// then traverse to the right
while (cur->right != nullptr) {
cur = cur->right;
}
}
return cur;
}
template <typename T>
void BinarySearchTree<T>::insertItem(T value) {
BinarySearchTreeNode<T>*cur = getRoot(), *parent = nullptr;
// find a proper leaf node to insert the new node
while (cur) {
parent = cur;
if (value >= cur->data) {
cur = cur->right;
} else {
cur = cur->left;
}
}
// empty tree case
if (parent == nullptr) {
root = new BinarySearchTreeNode<T>(value);
} else if (value < parent->data) {
// attach to the left child of parent
parent->left = new BinarySearchTreeNode<T>(value);
} else {
// attach to the right child of parent
parent->right = new BinarySearchTreeNode<T>(value);
}
// increment element count
n++;
}
#endif