-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTreeUtilities.h
223 lines (198 loc) · 6.85 KB
/
BinarySearchTreeUtilities.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
#pragma once
//helper function to perform operations common to all BST
#include "BinarySearchTree.h"
#include "Vector.h"
#include "Queue.h"
#include "BinarySearchTreeTraversals.h"
namespace jumbuna {
template<class T,class C>
class BstUtility {
using Node = BstNode<T>;
static void inOrderTraversal(Node *, Vector<T> &, Node *);
static void postOrderTraversal(Node *, Vector<T> &, Node *);
static void preOrderTraversal(Node *, Vector<T> &, Node *);
static void levelOrderTraversal(Node *, Vector<T> &, Node *);
static void leftRotation(Node *, BinarySearchTree<T, C> *, Node *);
static void rightRotation(Node *, BinarySearchTree<T, C> *, Node *);
public:
//gateway to private traversal functions
static Vector<T> treeTraversal(Node *, TraversalOrder, Node * = nullptr);
static Node *contains(Node *, T, Node * = nullptr);
static T preOrderSuccessor(Node *, Node * = nullptr);
static T postOrderSuccessor(Node *, Node * = nullptr);
//geteway to private rotation functions
static void leftLeftCase(Node *, BinarySearchTree<T, C> *, Node * = nullptr);
static void leftRightCase(Node *, BinarySearchTree<T, C> *, Node * = nullptr);
static void rightRightCase(Node *, BinarySearchTree<T, C> *, Node * = nullptr);
static void rightLeftCase(Node *, BinarySearchTree<T, C> *, Node * = nullptr);
static C comparator;
};
}
using namespace jumbuna;
//same to that of the tree
template<class T, class C>
C BstUtility<T, C>::comparator = C{};
template<class T, class C>
Vector<T> BstUtility<T, C>::treeTraversal(Node *startingNode, TraversalOrder order, Node *leafSentinel) {
Vector<T> vector {128};
//tree/sub-tree is empty
if(startingNode == leafSentinel) {
return vector;
}
switch(order) {
case IN_ORDER:
BstUtility<T, C>::inOrderTraversal(startingNode, vector, leafSentinel);
break;
case POST_ORDER:
BstUtility<T, C>::postOrderTraversal(startingNode, vector, leafSentinel);
break;
case PRE_ORDER:
BstUtility<T, C>::preOrderTraversal(startingNode, vector, leafSentinel);
break;
case LEVEL_ORDER:
BstUtility<T, C>::levelOrderTraversal(startingNode, vector, leafSentinel);
}
return vector;
}
template<class T, class C>
void BstUtility<T, C>::inOrderTraversal(Node *startingNode, Vector<T> &vector, Node *leafSentinel) {
//sorted
if(startingNode != leafSentinel) {
inOrderTraversal(startingNode->leftChild, vector, leafSentinel);
vector.push_back(startingNode->element);
inOrderTraversal(startingNode->rightChild, vector, leafSentinel);
}
}
template<class T, class C>
void BstUtility<T, C>::postOrderTraversal(Node *startingNode, Vector<T> &vector, Node *leafSentinel) {
//DFS
if(startingNode != leafSentinel) {
inOrderTraversal(startingNode->leftChild, vector, leafSentinel);
inOrderTraversal(startingNode->rightChild, vector, leafSentinel);
vector.push_back(startingNode->element);
}
}
template<class T, class C>
void BstUtility<T, C>::preOrderTraversal(Node *startingNode, Vector<T> &vector, Node *leafSentinel) {
if(startingNode != leafSentinel) {
vector.push_back(startingNode->element);
inOrderTraversal(startingNode->leftChild, vector, leafSentinel);
inOrderTraversal(startingNode->rightChild, vector, leafSentinel);
}
}
template<class T, class C>
void BstUtility<T, C>::levelOrderTraversal(Node *startingtNode, Vector<T> &vector, Node *leafSentinel) {
//BFS
if(startingtNode != leafSentinel) {
Queue<Node*> q;
q.push(startingtNode);
while(!q.empty()) {
Node *temp = q.pop();
if(temp->leftChild != leafSentinel) {
q.push(temp->leftChild);
}
if(temp->rightChild != leafSentinel) {
q.push(temp->rightChild);
}
vector.push_back(temp->element);
}
}
}
template<class T, class C>
BstNode<T>* BstUtility<T, C>::contains(Node *startingNode, T element, Node *leafSentinel) {
//binary search for node with given element
if(startingNode != leafSentinel) {
Node *temp = startingNode;
while(temp != leafSentinel) {
if(temp->element == element) {
return temp;
}
if(BstUtility<T, C>::comparator(temp->element, element)) {
temp = temp->leftChild;
}else {
temp = temp->rightChild;
}
}
}
return leafSentinel;
}
template<class T, class C>
T BstUtility<T, C>::preOrderSuccessor(Node *startingNode, Node *leafSentinel) {
//right most element in left subtree
while(startingNode->rightChild != leafSentinel) {
startingNode = startingNode->rightChild;
}
return startingNode->element;
}
template<class T, class C>
T BstUtility<T, C>::postOrderSuccessor(Node *startingNode, Node *leafSentinel) {
//left most element in right subtree
while(startingNode->leftChild != leafSentinel) {
startingNode = startingNode->leftChild;
}
return startingNode->element;
}
template<class T, class C>
void BstUtility<T, C>::leftRotation(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
Node *parent = candidate->parent;
Node *leftChild = candidate->leftChild;
Node *grandparent = parent->parent;
parent->rightChild = leftChild;
candidate->leftChild = parent;
parent->parent = candidate;
candidate->parent = grandparent;
if(leftChild != leafSentinel) {
leftChild->parent = parent;
}
if(grandparent) {
if(grandparent->leftChild == parent) {
grandparent->leftChild = candidate;
}else {
grandparent->rightChild = candidate;
}
}else {
tree->root = candidate;
}
}
template<class T, class C>
void BstUtility<T, C>::rightRotation(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
Node *parent = candidate->parent;
Node *rightChild = candidate->rightChild;
Node *grandParent = parent->parent;
candidate->rightChild = parent;
parent->leftChild = rightChild;
parent->parent = candidate;
candidate->parent = grandParent;
if(rightChild != leafSentinel) {
rightChild->parent = parent;
}
if(grandParent) {
if(grandParent->leftChild == parent) {
grandParent->leftChild = candidate;
}else {
grandParent->rightChild = candidate;
}
}else {
tree->root = candidate;
}
}
template<class T, class C>
void BstUtility<T, C>::leftLeftCase(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
BstUtility<T, C>::rightRotation(candidate, tree, leafSentinel);
}
template<class T, class C>
void BstUtility<T, C>::leftRightCase(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
Node *rightChild = candidate->rightChild;
BstUtility<T, C>::leftRotation(rightChild, tree, leafSentinel);
BstUtility<T, C>::leftLeftCase(rightChild, tree, leafSentinel);
}
template<class T, class C>
void BstUtility<T, C>::rightRightCase(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
BstUtility<T, C>::leftRotation(candidate, tree, leafSentinel);
}
template<class T, class C>
void BstUtility<T, C>::rightLeftCase(Node *candidate, BinarySearchTree<T, C> *tree, Node *leafSentinel) {
Node *leftChild = candidate->leftChild;
BstUtility<T, C>::rightRotation(leftChild, tree, leafSentinel);
BstUtility<T, C>::rightRightCase(leftChild, tree, leafSentinel);
}