-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplayTree.cpp
More file actions
329 lines (304 loc) · 8.3 KB
/
SplayTree.cpp
File metadata and controls
329 lines (304 loc) · 8.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// SplayTree.cpp
// Dylan Vanmali
// CS130A Spring 2017 Prof. Suri
#include <iostream>
#include <string>
#include "BSTNode.h"
#include "SplayTree.h"
using namespace std;
template < typename T >
std::ostream& SplayTree<T>::showTree(std::ostream &os, BSTNode<T> *n) const {
// This is printed from top down
int h = getHeight(n);
for (int i=1; i<=h; i++) {
printLevel(os, n, i, false);
os << endl;
}
}
template < typename T >
bool SplayTree<T>::printLevel(std::ostream &os, BSTNode<T> *n, int level, bool first) const {
if (n != NULL) {
if (level == 1) {
if (first)
os << ", ";
os << n->getValue();
return true;
}
else if (level > 1) {
first = printLevel(os, n->leftChild, level-1, first);
first = printLevel(os, n->rightChild, level-1, first);
return first;
}
}
}
template < typename T >
BSTNode<T>* SplayTree<T>::copyTree(const BSTNode<T> *n) {
if(n == NULL)
return NULL;
else {
BSTNode<T> * mine = new BSTNode<T>(n->getValue());
mine->leftChild = copyTree(n->leftChild);
mine->rightChild = copyTree(n->rightChild);
return mine;
}
}
template < typename T >
void SplayTree<T>::deleteTree(BSTNode<T> *n) {
if (n != NULL) {
deleteTree(n->leftChild);
deleteTree(n->rightChild);
delete n;
}
}
template < typename T >
int SplayTree<T>::getSize(const BSTNode<T> *n) const {
if (n == NULL) {
return 0;
}
else {
int leftSide = getSize(n->leftChild);
int rightSide = getSize(n->rightChild);
return 1 + leftSide + rightSide;
}
}
template < typename T >
int SplayTree<T>::getHeight(const BSTNode<T> *n) const {
if (n == NULL) {
return 0;
}
else {
int leftSide = getHeight(n->leftChild);
int rightSide = getHeight(n->rightChild);
if (leftSide < rightSide)
return 1 + rightSide;
else
return 1 + leftSide;
}
}
// Function used to get the height to the value v
// Used in splay function for recursive behavior
template < typename T >
int SplayTree<T>::getHeightToValue(const BSTNode<T> *n, T v) const {
while (n != NULL) {
if (v == n->getValue())
return 1;
else if (v < n->getValue())
return 1 + getHeightToValue(n->leftChild,v);
else if (v > n->getValue())
return 1 + getHeightToValue(n->rightChild,v);
}
return 0;
}
// Returns NULL if not present
template < typename T >
BSTNode<T>* SplayTree<T>::deleteValue(BSTNode<T> *n, const T v) {
BSTNode<T>* location = find(root,v);
if (location == NULL)
return NULL;
// Delete value is at top of tree, t1 after split
root = splay(root,v,getHeightToValue(root,v));
BSTNode<T>* tmp = root;
BSTNode<T>* t1 = root->leftChild;
BSTNode<T>* t2 = split(root,v);
delete tmp;
root = join(t1, t2);
return root;
}
// TODO
template < typename T >
BSTNode<T>* SplayTree<T>::join(BSTNode<T> *t1, BSTNode<T> *t2) {
if (t1 == NULL)
return t2;
if (t2 == NULL)
return t1;
BSTNode<T>* newRoot = t1;
BSTNode<T>* previous = t1;
if (newRoot->rightChild == NULL) {
newRoot->rightChild = t2;
}
else {
while (newRoot->rightChild != NULL) {
previous = newRoot;
newRoot = newRoot->rightChild;
}
previous->rightChild = newRoot->leftChild;
newRoot->leftChild = t1;
newRoot->rightChild = t2;
}
root = newRoot;
return root;
}
// Split returns the broken tree from the root
// The other tree can be obtained as the root value
template < typename T >
BSTNode<T>* SplayTree<T>::split(BSTNode<T> *n, T v) {
if (n == NULL)
return NULL;
else if (v < n->getValue()) {
BSTNode<T>* brokenTree = n->leftChild;
n->leftChild = NULL;
return brokenTree;
}
else {
BSTNode<T>* brokenTree = n->rightChild;
n->rightChild = NULL;
return brokenTree;
}
}
template < typename T >
BSTNode<T>* SplayTree<T>::find(BSTNode<T> *n, const T v) const {
if(n == NULL)
return NULL;
if(v == n->getValue())
return n;
else if(v < n->getValue())
return find(n->leftChild, v);
else
return find(n->rightChild, v);
}
// Input root and value to be inserted
// Returns root
template < typename T >
BSTNode<T>* SplayTree<T>::insert(BSTNode<T> *n, const T v) {
if (n == NULL) {
BSTNode<T>* newNode = new BSTNode<T>(v);
cout << "item " << v << " inserted" << endl;
return newNode;
}
if (v == n->getValue()) {
cout << "item " << v << " not inserted; already present" << endl;
return n;
}
else if (v < n->getValue()) {
n->leftChild = insert(n->leftChild,v);
return n;
}
else if (v > n->getValue()) {
n->rightChild = insert(n->rightChild,v);
return n;
}
}
// Zig in Right Rotation (for left zig)
template < typename T >
BSTNode<T>* SplayTree<T>::rightRotate(BSTNode<T> *n) {
BSTNode<T> *m = n->leftChild;
n->leftChild = m->rightChild;
m->rightChild = n;
return m;
}
// Zig in Left Rotation (for right zig)
template < typename T >
BSTNode<T>* SplayTree<T>::leftRotate(BSTNode<T> *n) {
BSTNode<T> *m = n->rightChild;
n->rightChild = m->leftChild;
m->leftChild = n;
return m;
}
// Splays from root to the value v
// heightAtNode needed for recursive behavior
// Initial call should be from getHeightAtNode function
// Returns root
template < typename T >
BSTNode<T>* SplayTree<T>::splay(BSTNode<T> *n, T v, int heightAtNode) {
if (n == NULL || v == n->getValue()) {
return n;
}
// If Height to the value is odd, recurse normally with grandparent
// Should not rotate once because of this statement, check and balance maintained though
// for debugging purposes (no HERE_R or HERE_L should exist if single rotation occurs at leaf)
//if (heightAtNode % 2 == 1) {
// Left Subtree
if (v < n->getValue()) {
if (n->leftChild == NULL)
return n;
// Value is the Left Child
else if (v == n->leftChild->getValue()) {
//cout << "HERE_R" << endl;
return rightRotate(n);
}
// Zig-Zig: Left, Left
else if (v < n->leftChild->getValue()) {
if (n->leftChild->leftChild == NULL) {
//cout << "HERE_R" << endl;
return rightRotate(n);
}
//cout << "HERE_RR" << endl;
n->leftChild->leftChild = splay(n->leftChild->leftChild,v,heightAtNode-2);
return rightRotate(rightRotate(n));
}
// Zig-Zag: Left, Right
else if (v > n->leftChild->getValue()) {
if (n->leftChild->rightChild == NULL) {
//cout << "HERE_R" << endl;
return rightRotate(n);
}
//cout << "HERE_LR" << endl;
n->leftChild->rightChild = splay(n->leftChild->rightChild,v,heightAtNode-2);
n->leftChild = leftRotate(n->leftChild);
return rightRotate(n);
}
}
// Right Subtree
else if (v > n->getValue()) {
if (n->rightChild == NULL)
return n;
// Value is the Right Child
else if (v == n->rightChild->getValue()) {
//cout << "HERE_L" << endl;
return leftRotate(n);
}
// Zig-Zig: Right, Left
else if (v < n->rightChild->getValue()) {
if (n->rightChild->leftChild == NULL) {
//cout << "HERE_L" << endl;
return leftRotate(n);
}
//cout << "HERE_RL" << endl;
n->rightChild->leftChild = splay(n->rightChild->leftChild,v,heightAtNode-2);
n->rightChild = rightRotate(n->rightChild);
return leftRotate(n);
}
// Zig-Zag: Right, Right
else if (v > n->rightChild->getValue()) {
if (n->rightChild->rightChild == NULL) {
//cout << "HERE_L" << endl;
return leftRotate(n);
}
//cout << "HERE_LL" << endl;
n->rightChild->rightChild = splay(n->rightChild->rightChild,v,heightAtNode-2);
return leftRotate(leftRotate(n));
}
}
//}
// If Height to the value is even, recurse once in
/*
else {
if (v < n->getValue()) {
if (n->leftChild == NULL)
return n;
// Value is the Left Child
else if (v == n->leftChild->getValue()) {
cout << "HERE_Top_R" << endl;
return rightRotate(n);
}
// Right Rotate on top node
cout << "HERE_Top_R" << endl;
n->leftChild = splay(n->leftChild,v,heightAtNode-1);
return rightRotate(n);
}
else if (v > n->getValue()) {
if (n->rightChild == NULL)
return n;
// Value is the Right Child
else if (v == n->rightChild->getValue()) {
cout << "HERE_Top_L" << endl;
return leftRotate(n);
}
// Left Rotate on top node
cout << "HERE_Top_L" << endl;
n->rightChild = splay(n->rightChild,v,heightAtNode-1);
return leftRotate(n);
}
}
*/
}