-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBST.h
390 lines (325 loc) · 7.08 KB
/
BST.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#ifndef TREE_H
#define TREE_H
#include "BSNode.h"
#include "Stack.h"
#include <QString>
#include <QTextEdit>
template <class T>
class BST
{
public:
//Constructor / Destructor
BST();
~BST();
//Insert / Remove
void insert(QString,T);
void remove(QString);
BSNode<T>* search(QString);
bool isEmpty();
//Iterator
void iterBegin();
bool iterHasNext();
BSNode<T> * iterNext();
//Print
QString preOrderPrint();
QString inOrderPrint();
QString postOrderPrint();
virtual operator QString();
private:
BSNode<T> *root;
void destroy(BSNode<T>*);
BSNode<T>* insert(BSNode<T>*,QString, T);
BSNode<T>* remove(BSNode<T> *,QString);
BSNode<T>* searchUtility(BSNode<T>*, QString);
BSNode<T>* minValueNode(BSNode<T> *);
Stack<BSNode<T> *> iterStack;
void iterPushAll(BSNode<T> *);
BSNode<T>* rightRotate(BSNode<T>*);
BSNode<T>* leftRotate(BSNode<T>*);
int max(int,int);
int height(BSNode<T>*);
int getBalance(BSNode<T>*);
void preOrderPrintUtility(BSNode<T>*,QString&);
void inOrderPrintUtility(BSNode<T>*,QString&);
void postOrderPrintUtility(BSNode<T>*,QString&);
};
template <class T>
BST<T>::BST()
{
root = NULL;
}
template <class T>
BST<T>::~BST()
{
destroy(root);
}
template <class T>
void BST<T>::destroy(BSNode<T> * node) {
if (!node) return;
destroy(node->LC);
destroy(node->RC);
delete node;
}
template <class T>
void BST<T>::insert(QString key,T dataIn)
{
root = insert(root,key, dataIn);
}
//Utility function for insert
template <class T>
BSNode<T>* BST<T>::insert(BSNode<T>* node, QString key, T dataIn)
{
/*if (temp == 0)
temp = new BSNode<T>(key,dataIn);
else
{
if (key < temp->key) {
insert(temp->LC,key, dataIn);
}
else if (key >(temp)->key)
{
insert(temp->RC, key, dataIn);
}
else{
}
}*/
if (node == NULL)
return new BSNode<T>(key,dataIn);
if (key < node->key)
node->LC = insert(node->LC, key, dataIn);
else if (key > node->key)
node->RC = insert(node->RC, key,dataIn);
else // Equal keys are not allowed in BST
return node;
/* 2. Update height of this ancestor node */
node->height = 1 + max(height(node->LC),
height(node->RC));
int balance = getBalance(node);
// Left Left Case
if (balance > 1 && key < node->LC->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->RC->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->LC->key)
{
node->LC = leftRotate(node->LC);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->RC->key)
{
node->RC = rightRotate(node->RC);
return leftRotate(node);
}
/* return the (unchanged) node pointer */
return node;
}
template <class T>
void BST<T>::remove(QString key)
{
root = remove(root, key);
}
//utility function for remove
template <class T>
BSNode<T> * BST<T>::remove(BSNode<T> * p, QString key)
{
// base case
if (p == NULL) return p;
// search for node
if (key < p->key)
p->LC = remove(p->LC, key);
else if (key > p->key)
p->RC = remove(p->RC, key);
// node found !
else
{
// node with only one child or no child
if (p->LC == NULL)
{
BSNode<T> *temp = p->RC;
delete p;
return temp;
}
else if (p->RC == NULL)
{
BSNode<T> *temp = p->LC;
delete p;
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
BSNode<T> * temp = minValueNode(p->RC);
// Copy the inorder successor's content to this node
p->key = temp->key;
p->data = temp->data;
// Delete the inorder successor
p->RC = remove(p->RC, temp->key);
}
return p;
}
template <class T>
BSNode<T> * BST<T>::minValueNode(BSNode<T> * node)
{
BSNode<T> * current = node;
while (current->LC != NULL)
current = current->LC;
return current;
}
template <class T>
bool BST<T>::isEmpty() {
return root == NULL;
}
template <class T>
void BST<T>::iterBegin() {
while (!iterStack.isEmpty()) {
iterStack.pop();
}
iterPushAll(root);
}
template <class T>
void BST<T>::iterPushAll(BSNode<T> * p) {
while (p != NULL) {
iterStack.push(p);
p = p->LC;
}
}
template <class T>
bool BST<T>::iterHasNext() {
return !iterStack.isEmpty();
}
template <class T>
BSNode<T>* BST<T>::iterNext() {
BSNode<T> * temp;
int s = iterStack.pop(temp);
if (s) {
iterPushAll(temp->RC);
return temp;
}
}
template <class T>
QString BST<T>::preOrderPrint()
{
QString result = "";
preOrderPrintUtility(root,result);
return result;
}
template <class T>
void BST<T>::preOrderPrintUtility(BSNode<T>* temp,QString & out)
{
if (temp != 0)
{
out += *temp + "\n";
preOrderPrintUtility(temp->LC, out);
preOrderPrintUtility(temp->RC,out);
}
}
//Prints inOrder (HTML Format)
template <class T>
QString BST<T>::inOrderPrint()
{
QString result = "<table>";
inOrderPrintUtility(root,result);
result += "</table>";
return result;
}
template <class T>
void BST<T>::inOrderPrintUtility(BSNode<T>* temp,QString & out)
{
if (temp != 0)
{
inOrderPrintUtility(temp->LC,out);
out += "<tr><td><font color=\"darkkhaki\">" + temp->key + "</font></td><td> -> </td><td>" + temp->data + "</td></tr>";
inOrderPrintUtility(temp->RC,out);
}
}
template <class T>
QString BST<T>::postOrderPrint()
{
QString result = "";
postOrderPrintUtility(root,result);
return result;
}
template <class T>
void BST<T>::postOrderPrintUtility(BSNode<T>* temp, QString & out)
{
if (temp != 0)
{
postOrderPrintUtility(temp->LC,out);
postOrderPrintUtility(temp->RC,out);
out += *temp + "\n";
}
}
template <class T>
BSNode<T>* BST<T>::search(QString key)
{
return searchUtility(root, key);
}
template <class T>
BSNode<T>* BST<T>::searchUtility(BSNode<T>* leaf, QString key)
{
if (leaf != NULL)
{
if (key == leaf->key)
return leaf;
if (key < leaf->key)
return searchUtility(leaf->LC, key);
else
return searchUtility(leaf->RC, key);
}
else
return NULL;
}
template <class T>
BST<T>::operator QString() {
return inOrderPrint();
}
template <class T>
int BST<T>::height(BSNode<T> * N)
{
if (N == NULL)
return 0;
return N->height;
}
template <class T>
int BST<T>::max(int a, int b)
{
return (a > b)? a : b;
}
template <class T>
int BST<T>::getBalance(BSNode<T>* N)
{
if (N == NULL)
return 0;
return height(N->LC) - height(N->RC);
}
template <class T>
BSNode<T>* BST<T>::rightRotate(BSNode<T>* y)
{
BSNode<T>* x = y->LC;
BSNode<T>* T2 = x->RC;
// Perform rotation
x->RC = y;
y->LC = T2;
// Update heights
y->height = max(height(y->LC), height(y->RC))+1;
x->height = max(height(x->LC), height(x->RC))+1;
// Return new root
return x;
}
template <class T>
BSNode<T>* BST<T>::leftRotate(BSNode<T>* x)
{
BSNode<T>* y = x->RC;
BSNode<T>* T2 = y->LC;
// Perform rotation
y->LC = x;
x->RC = T2;
// Update heights
x->height = max(height(x->LC), height(x->RC))+1;
y->height = max(height(y->LC), height(y->RC))+1;
// Return new root
return y;
}
#endif