-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplayTree.h
More file actions
273 lines (247 loc) · 8.81 KB
/
Copy pathSplayTree.h
File metadata and controls
273 lines (247 loc) · 8.81 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
#ifndef PROJECT3STARTER_SPLAYTREE_H
#define PROJECT3STARTER_SPLAYTREE_H
template <typename Comparable>
class SplayTree {
private:
struct SplayNode {
Comparable value;
SplayNode* leftChild;
SplayNode* rightChild;
SplayNode* parent;
// Constructors
SplayNode() : value(Comparable()), leftChild(nullptr), rightChild(nullptr), parent(nullptr) {}
explicit SplayNode(Comparable c) : value(c), leftChild(nullptr), rightChild(nullptr), parent(nullptr) {}
SplayNode(Comparable c, SplayNode* l, SplayNode* r, SplayNode* p = nullptr) : value(c), leftChild(l), rightChild(r), parent(p) {}
};
SplayNode* root;
unsigned long size;
// Helper recursive function to destroy the tree.
void destroy(SplayNode* &n) {
if (n != nullptr) {
destroy(n->leftChild);
destroy(n->rightChild);
delete n;
n = nullptr;
}
}
// Helper recursive function to copy the tree.
SplayNode* copyNode(SplayNode* n) {
if (n == nullptr) {
return nullptr;
}
++size;
return new SplayNode(n->value, copyNode(n->leftChild), copyNode(n->rightChild), n->parent);
}
// Helper method to perform a single right rotation.
SplayNode* singleRotationRightFromChild(SplayNode* n) {
// n has to move up,
// n's parent has to move down to the right.
n->parent->leftChild = n->rightChild;
if (n->rightChild != nullptr) {
n->rightChild->parent = n->parent;
}
n->rightChild = n->parent;
n->parent = n->rightChild->parent;
if (n->rightChild->parent != nullptr) {
// don't know if n's original parent was a left or right child
if (n->rightChild->parent->leftChild == n->rightChild) {
n->rightChild->parent->leftChild = n;
} else {
n->rightChild->parent->rightChild = n;
}
}
n->rightChild->parent = n;
if (n->rightChild == root) {
root = n;
}
return n;
}
// Helper method to perform a single left rotation.
SplayNode* singleRotationLeftFromChild(SplayNode* n) {
// n has to move up,
// n's parent has to move down to the left.
n->parent->rightChild = n->leftChild;
if (n->leftChild != nullptr) {
n->leftChild->parent = n->parent;
}
n->leftChild = n->parent;
n->parent = n->leftChild->parent;
if (n->leftChild->parent != nullptr) {
// don't know if n's original parent was a left or right child
if (n->leftChild->parent->leftChild == n->leftChild) {
n->leftChild->parent->leftChild = n;
} else {
n->leftChild->parent->rightChild = n;
}
}
n->leftChild->parent = n;
if (n->leftChild == root) {
root = n;
}
return n;
}
// Method that will move a node higher in the tree
void splay(SplayNode* n) {
if (n->parent == nullptr) {
// n is root. Nothing to do.
return;
}
else if (n->parent->leftChild == n && n->parent->parent == nullptr) {
// n is left child of root. Need single rotation to the right.
singleRotationRightFromChild(n);
return;
}
else if (n->parent->rightChild == n && n->parent->parent == nullptr) {
// n is right child of root. Need single rotation to the left.
singleRotationLeftFromChild(n);
return;
}
else if (n->parent->leftChild == n && n->parent->parent->leftChild == n->parent) {
// left-left case. Need to see-saw to the right.
n = singleRotationRightFromChild(n->parent);
n = singleRotationRightFromChild(n->leftChild);
}
else if (n->parent->rightChild == n && n->parent->parent->rightChild == n->parent) {
// right-right case. Need to see-saw to the left.
n = singleRotationLeftFromChild(n->parent);
n = singleRotationLeftFromChild(n->rightChild);
}
else if (n->parent->leftChild == n && n->parent->parent->rightChild == n->parent) {
// grandparent-to-n right-left case. Need a double rotation (right-left).
n = singleRotationRightFromChild(n);
n = singleRotationLeftFromChild(n);
}
else if (n->parent->rightChild == n && n->parent->parent->leftChild == n->parent) {
// grandparent-to-n left-right case. Need a double rotation (left-right).
n = singleRotationLeftFromChild(n);
n = singleRotationRightFromChild(n);
}
if (n != root) {
splay(n);
}
}
// Helper recursive function to find a value in the tree.
int find(const Comparable &c, SplayNode* &n, int & count) {
if (n == nullptr) {
// Reached a dead end. Value not in tree.
return count;
}
if (c < n->value) {
// Value is less than current node. Go to node's left child.
return find(c, n->leftChild, ++count);
}
if (n->value < c) {
// Value is greater than current node. Go to node's right child.
return find(c, n->rightChild, ++count);
}
// If code reaches here, c == n->value. Node found!
splay(n);
return count;
}
// Helper recursive function to add a value to the tree.
bool add(const Comparable &c, SplayNode* &n, SplayNode* p) {
if (n == nullptr) {
// We found the place where we can add the node.
n = new SplayNode(c, nullptr, nullptr, p);
++size;
splay(n);
return true;
}
else if (c < n->value) {
// Value is less than current node. Go to left child.
return add(c, n->leftChild, n);
}
else if (n->value < c) {
// Value is greater than current node. Go to right child.
return add(c, n->rightChild, n);
}
// If code reaches here, value is a duplicate. Cannot add it to the tree.
return false;
}
// Helper recursive method to find the maximum value from a given node.
Comparable& findMax(SplayNode* n) {
if (n->rightChild == nullptr) {
return n->value;
}
return findMax(n->rightChild);
}
// Helper recursive function to delete a value from the tree.
void remove(const Comparable &c, SplayNode* &n) {
if (n == nullptr) {
// We did not find the value. Cannot remove it. Nothing to do.
return;
}
else if (c < n->value) {
// Value is less than current node. Go to left child.
remove(c, n->leftChild);
}
else if (n->value < c) {
// Value is greater than current node. Go to right child.
remove(c, n->rightChild);
}
// If code reaches here, we found the node. Now to remove it.
else if (n->leftChild != nullptr && n->rightChild != nullptr) {
// The node we want to remove has two children
// Find the largest value from the left subtree
n->value = findMax(n->leftChild);
remove(n->value, n->leftChild);
}
else {
// The node we want to remove has 0 or 1 child.
// If it has a child, move it up. If not, set to nullptr.
SplayNode *oldNode = n;
n = (n->leftChild != nullptr) ? n->leftChild : n->rightChild;
delete oldNode;
oldNode = nullptr;
--size;
}
}
public:
// Default Constructor
SplayTree() {
root = nullptr;
size = 0;
}
// Copy Constructor
SplayTree(const SplayTree &b) {
size = 0;
// calls private helper function
root = copyNode(b.root);
}
// Destructor
~SplayTree() {
// calls private helper function
destroy(root);
}
// Method to destroy tree
void timber() {
// calls private helper function
destroy(root);
}
bool isEmpty() const {
return (root == nullptr);
}
int find(const Comparable &c) {
// calls private helper function
int countD = 0;
return this->find(c, root, countD);
}
bool add(const Comparable &c) {
// calls private helper function
return add(c, root, nullptr);
}
void remove(const Comparable &c) {
// calls private helper function
remove(c, root);
}
unsigned long getSize() const {
return size;
}
// Overloaded = operator
SplayTree& operator = (const SplayTree &rhs) {
destroy(root);
size = 0;
root = copyNode(rhs.root);
}
};
#endif //PROJECT3STARTER_SPLAYTREE_H