This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.cpp
315 lines (291 loc) · 8.04 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
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
#include "BST.h"
#include <iostream>
template <typename T>
BST<T>::BST() {
root = 0;
}
template <typename T>
BST<T>::~BST() {
}
template <typename T>
bool BST<T>::find(T v) {
Node<T>* temp = new Node<T>(v);
root = temp;
return true; //wat
}
template <typename T>
void BST<T>::insert(T v) {
Node<T>* toBeAdded = new Node<T>(v);
if(root == 0){
root = toBeAdded;
return;
}
//As per the book... (mostly)
Node<T>** Q;
Node<T>** P = &root;
Node<T>* R;
bool critNodeFound = false;
Node<T>** critNode;
while((*P) != 0 && ((*P)->getValue()) != v){
if((*P)->getBalance()!=0){
critNodeFound = true;
critNode = P;
}
if(v < (*P)->getValue()){ P = &((*P)->getLeftChild());}
else { P = &((*P)->getRightChild());}
}
if((*P)!=0) return;
*P = toBeAdded;
if(!critNodeFound)
R = root;
else{
Node<T>** C = 0;
Node<T>** B = 0;
int d1=0;
int d2=0;
int d3=0;
op(d1, C, v, critNode);
if ((*critNode)->getBalance() != d1){
(*critNode)->setBalance(0);
R = *P;
}
else {
op(d2, B, v, C);
if(d2 == d1){
(*critNode)->setBalance(0);
(*C)->setBalance(0);
}
else{
Q = &R;
op(d3, Q, v, B);
if(d3 == d2){
(*critNode)->setBalance(0);
(*C)->setBalance(d1);
}
else if (d3 == d2 * -1)
(*critNode)->setBalance(0);
else
(*critNode)->setBalance(0);
if(d2 == 1)
rotateRight(C);
else
rotateLeft(C);
if(d1 == 1)
rotateRight(critNode);
else
rotateRight(critNode);
}
}
}
Q = &R;
P = &R;
int d;
while((*Q)->getValue() != v){
d = (*Q)->getBalance();
op(d, Q, v, P);
(*Q)->setBalance(d);
}
}
template <typename T>
void BST<T>::op(int& d, Node<T>**& Q, T v, Node<T>** P){
if (v == (*P)->getValue()){
d = 0;
Q = P;
}
else if(v < (*P)->getValue()){
d = -1;
Q = &((*P)->getLeftChild());
}
else {
d = 1;
Q = &((*P)->getRightChild());
}
}
template <typename T>
void BST<T>::remove(T v) { // in order predecessor swap
//find node
Node<T>** curr = &root;
while(*curr != 0 && v!=(*curr)->getValue()) { // finds the node, otherwise the tree is empty/the value was not found
if(v < (*curr)->getValue())
curr = &((*curr)->getLeftChild());
else if(v > (*curr)->getValue())
curr = &((*curr)->getRightChild());
}
Node<T>* iop = *curr; // will become pointer to IOP
//find IOP
if(iop != 0){ // tree is not empty or the value is in the tree
Node<T>* temp = *curr;
if(iop->getLeftChild()!=0){ // node has the in-order predecessor in its children
iop = iop->getLeftChild(); // PREPARE FOR TRAVERSAL
while(iop->getRightChild() != 0){ // traverse to the IOP
iop = iop->getRightChild();
}
iop->setRightChild(*(*curr)->getRightChild()); // set the IOP's right child to be the removeNode's right tree
temp = *curr; // create a temporary pointer so we don't lose the node in mem
*curr = (*curr)->getLeftChild(); // redirect the removeNode pointer to its left child
}
else if ((*curr)->getRightChild()!=0){ // removeNode does not have a left subtree, so no IOP there
temp = *curr; // pointer so we don't lose the node
*curr = (*curr)->getRightChild(); // redirect pointer
}
else { // Has no children
temp = *curr;
*curr = 0;
}
delete temp;
//If the value wasn't in the tree, or the tree is empty... nothing needs to be done!
}
}
template <typename T>
void BST<T>::print() {
inOrderTraversal(root);
}
template <typename T>
void BST<T>::inOrderTraversal(Node<T>* root) {
if(root != 0) {
inOrderTraversal(root->getLeftChild());
std::cout << root->getValue() << std::endl;
inOrderTraversal(root->getRightChild());
}
}
template <typename T>
void BST<T>::postOrderTraversal(Node<T>* root) {
if(root != 0) {
postOrderTraversal(root->getLeftChild());
postOrderTraversal(root->getRightChild());
std::cout << root->getValue() << std::endl;
}
}
template <typename T>
void BST<T>::breadthPrint() {
std::list< Node<T>* >* topLevel = new std::list< Node<T>* >();
topLevel->push_back(root);
levelTraversal(topLevel,0);
}
template <typename T>
int BST<T>::depth(Node<T>* root, int dpth, int maxDepth) {
if(root != 0 && (root->getLeftChild() !=0 || root->getRightChild() !=0)) {
int leftDepth = depth(root->getLeftChild(),dpth+1,maxDepth);
int rightDepth = depth(root->getRightChild(),dpth+1,maxDepth);
if(leftDepth > rightDepth) return leftDepth;
else return rightDepth;
}
else {
return dpth;
}
}
template <typename T>
int BST<T>::pow(int a, int p) {
if(p == 0) return 1;
else return a*pow(a,p-1);
}
template <typename T>
void BST<T>::levelTraversal(std::list< Node<T>* >* parents, int level) {
bool keepGoing = false;
int treeDepth = depth(root,0,0);
int width = pow(2,treeDepth)*3;
std::list< Node<T>* >* holder = new std::list< Node<T>* >();
int startSpace = width - 1;
for(int i = 0; i<level+1; i++) {
startSpace = startSpace/2;
}
int numEls = pow(2,level);
if(treeDepth != level) {
for(int i = 0; i<startSpace; i++) {std::cout << " ";} //spacing before the level
}
for (int i = 0; i<numEls; i++ ) {
Node<T>* temp = parents->front();
parents->pop_front();
if(temp == 0) {
holder->push_back((Node<T>*)0);
holder->push_back((Node<T>*)0);
std::cout << "-";
}
else if(temp->getLeftChild() == 0 && temp->getRightChild() == 0) {
//do nothing
holder->push_back((Node<T>*)0);
holder->push_back((Node<T>*)0);
std::cout << temp->getValue();
}
else if(temp->getLeftChild() == 0) {
holder->push_back((Node<T>*)0);
holder->push_back(temp->getRightChild());
std::cout << temp->getValue();
keepGoing = true;
}
else if(temp->getRightChild() == 0) {
holder->push_back(temp->getLeftChild());
holder->push_back((Node<T>*)0);
std::cout << temp->getValue();
keepGoing = true;
}
else {
holder->push_back(temp->getLeftChild());
holder->push_back(temp->getRightChild());
std::cout << temp->getValue();
keepGoing = true;
}
if(!parents->empty()) {
int spacing = (width/numEls)-1;
if(spacing!=2) { // spacing case
for(int i = 0; i<spacing; i++) {std::cout << " ";}
}
else { // case for the bottom row
if(i%2==0) std::cout << " ";
else std::cout << " ";
}
}
}
if(keepGoing) {
delete parents;
int lines = (width/4)-1;
for(int i = 0; i<level; i++) { lines = lines/2; }
int inBetweenSpacing = 1;
int spacing = (width/numEls)-1;
for(int i = 0; i<lines; i++) {
std::cout << std::endl;
spacing = spacing-2;
startSpace = startSpace - 1;
for(int j = 0; j<startSpace; j++) {
std::cout << " ";
}
for(int j = 0; j<numEls; j++) {
std::cout << "/";
for(int k = 0; k<inBetweenSpacing; k++) {
std::cout << " ";
}
std::cout << "\\";
for(int l = 0; l<spacing; l++) {
std::cout << " ";
}
}
inBetweenSpacing = inBetweenSpacing+2;
}
std::cout << std::endl;
levelTraversal(holder, level+1);
}
else {
std::cout << std:: endl;
}
}
template <typename T>
void BST<T>::rotateRight(Node<T>** crit){
if(crit==0)
crit = &root;
Node<T>* temp = (*crit)->getLeftChild();
(*crit)->setLeftChild(*(temp->getRightChild()));
temp->setRightChild(**crit);
*crit = temp;
}
template <typename T>
void BST<T>::rotateLeft(Node<T>** crit){
if(crit==0)
crit = &root;
Node<T>* temp = (*crit)->getRightChild();
(*crit)->setRightChild(*(temp->getLeftChild()));
temp->setLeftChild(**crit);
*crit = temp;
}
template class BST<int>;
template class BST<double>;
template class BST<std::string>;