-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
308 lines (272 loc) · 8.2 KB
/
BinarySearchTree.cpp
File metadata and controls
308 lines (272 loc) · 8.2 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
#include "BinarySearchTree.h"
// constructors
Node::Node(int value) : data(value), left(nullptr), right(nullptr) {}
BinarySearchTree::BinarySearchTree() : root(nullptr) {}
// insert helper function
Node* BinarySearchTree::insertHelper(Node* root, int value) {
// if tree is empty
if (root == nullptr) {
return new Node(value);
}
// if tree is NOT empty, use recursion
else if (value <= root->data) {
root->left = insertHelper(root->left, value);
}
else {
root->right = insertHelper(root->right, value);
}
return root;
}
// function to insert element into tree
void BinarySearchTree::insert(int value) {
root = insertHelper(root, value);
}
// contains helper function
bool BinarySearchTree::containsHelper(Node* root, int value) const {
// if tree is empty, return false
if (root == nullptr) {
return false;
}
// if root is value, return true
if (root->data == value) {
return true;
}
// recursion to search for left and right
else if (value < root->data) {
return containsHelper(root->left, value);
}
else {
return containsHelper(root->right, value);
}
}
// function to search for element in tree. return true if found
bool BinarySearchTree::contains(int value) const {
return containsHelper(root, value);
}
// find helper
Node* BinarySearchTree::findHelper(Node* root, int value) const {
// edge cases
if (root == nullptr || root->data == value) {
return root;
}
// recursion to search for left and right
else if (value < root->data) {
return findHelper(root->left, value);
}
else {
return findHelper(root->right, value);
}
}
// function to find element by value
Node* BinarySearchTree::find(int value) const {
return findHelper(root, value);
}
// min helper
Node* BinarySearchTree::minHelper(Node* root) const {
if (root == nullptr) {
std::cout << "[ERROR] Tree is empty.\n";
return nullptr;
}
// root is not local, so cannot just use root node
Node* current = root;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
// function to find min element in BST. iterative implementation
Node* BinarySearchTree::min() const {
return minHelper(root);
}
// max helper
Node* BinarySearchTree::maxHelper(Node* root) const {
if (root == nullptr) {
std::cout << "[ERROR] Tree is empty.\n";
return nullptr;
}
// root is not local, so cannot just use root node
Node* current = root;
while (current->right != nullptr) {
current = current->right;
}
return current;
}
// function to find max element in BST. iterative implementation
Node* BinarySearchTree::max() const {
return maxHelper(root);
}
// find height helper function. counting edges from root
int BinarySearchTree::findHeightHelper(Node* root) const {
if (root == nullptr) {
return -1;
}
int leftHeight = findHeightHelper(root->left);
int rightHeight = findHeightHelper(root->right);
return std::max(leftHeight, rightHeight) + 1;
}
// function to find height of BST. count edges from root
int BinarySearchTree::findHeight() const {
return findHeightHelper(root);
}
// get balance factor of node
int BinarySearchTree::getBalanceFactor(Node* root) const {
if (root == nullptr) {
return -1;
}
else {
return (findHeightHelper(root->left) - findHeightHelper(root->right));
}
}
// function to level order traverse the tree
void BinarySearchTree::levelOrderTraversal() const {
if (root == nullptr) {
return;
}
std::queue<Node*> discoveredNodeQueue;
discoveredNodeQueue.push(root);
// while queue is not empty
while(!discoveredNodeQueue.empty()) {
Node* current = discoveredNodeQueue.front();
std::cout << current->data << ' ';
if (current->left != nullptr) {
discoveredNodeQueue.push(current->left);
}
if (current->right != nullptr) {
discoveredNodeQueue.push(current->right);
}
discoveredNodeQueue.pop();
}
std::cout << '\n';
}
// pre-order traversal helper
void BinarySearchTree::preOrderTraversalHelper(Node* root) const {
if (root == nullptr) {
return;
}
std::cout << root->data << ' ';
preOrderTraversalHelper(root->left);
preOrderTraversalHelper(root->right);
}
// function to depth first pre-order traversal
void BinarySearchTree::preOrderTraversal() const {
preOrderTraversalHelper(root);
}
// in-order traversal helper
void BinarySearchTree::inOrderTraversalHelper(Node* root) const {
if (root == nullptr) {
return;
}
inOrderTraversalHelper(root->left);
std::cout << root->data << ' ';
inOrderTraversalHelper(root->right);
}
// function to depth first in-order traversal
void BinarySearchTree::inOrderTraversal() const {
inOrderTraversalHelper(root);
}
// post-order traversal helper
void BinarySearchTree::postOrderTraversalHelper(Node* root) const {
if (root == nullptr) {
return;
}
postOrderTraversalHelper(root->left);
postOrderTraversalHelper(root->right);
std::cout << root->data << ' ';
}
// function to depth first post-order traversal
void BinarySearchTree::postOrderTraversal() const {
postOrderTraversalHelper(root);
}
// is binary search tree helper
bool BinarySearchTree::isBinarySearchTreeHelper(Node* root, int minValue, int maxValue) const {
if (root == nullptr) {
return true;
}
if (root->data > minValue && root->data < maxValue
&& isBinarySearchTreeHelper(root->left, minValue, root->data)
&& isBinarySearchTreeHelper(root->right, root->data, maxValue)) {
return true;
}
else {
return false;
}
}
// function to check if binary tree is binary search tree (more for general binary trees)
bool BinarySearchTree::isBinarySearchTree() const {
return isBinarySearchTreeHelper(root, INT_MIN, INT_MAX);
}
// delete Node helper
Node* BinarySearchTree::deleteNodeHelper(Node* root, int value) {
if (root == nullptr) {
return nullptr;
}
// node to be deleted in left sub tree
else if (value < root->data) {
root->left = deleteNodeHelper(root->left, value);
}
// node to be deleted in right sub tree
else if (value > root->data) {
root->right = deleteNodeHelper(root->right, value);
}
// found the node to be deleted
else {
// case 1: no children
if (root->left == nullptr && root->right == nullptr) {
delete root;
root = nullptr; // to handle dangling pointer
}
// case 2: 1 child
else if (root->left == nullptr) {
Node* temp = root;
root = root->right;
delete temp;
}
else if (root->right == nullptr) {
Node* temp = root;
root = root->left;
delete temp;
}
// case 3: 2 child
else {
Node* temp = minHelper(root->right);
root->data = temp->data;
root->right = deleteNodeHelper(root->right, temp->data);
}
}
return root;
}
// function to delete node
void BinarySearchTree::deleteNode(int value) {
root = deleteNodeHelper(root, value);
}
// function to find inorder successor of value in a BST
Node* BinarySearchTree::getSuccessorHelper(Node* root, int value) const {
Node* current = findHelper(root, value);
// if value not found, return nullptr
if (current == nullptr) {
return nullptr;
}
// case 1: node has right subtree
if (current->right != nullptr) {
return minHelper(current->right);
}
// case 2: node has no right subtree
else {
Node* successor = nullptr;
Node* ancestor = root;
while(ancestor != current) {
if (current->data < ancestor->data) {
successor = ancestor;
ancestor = ancestor->left;
}
else {
ancestor = ancestor->right;
}
}
return successor;
}
}
// function to get successor of value
Node* BinarySearchTree::getSuccessor(int value) const {
return getSuccessorHelper(root, value);
}