-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplaytree.h
232 lines (207 loc) · 5.44 KB
/
splaytree.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
#include "bst.h"
#include <iostream>
#include <exception>
#include <cstdlib>
template<typename KeyType, typename ValueType>
class SplayTree : public BinarySearchTree<KeyType,ValueType>{
private:
Node_<KeyType, ValueType>* internalFind(const KeyType& k, bool& found) const {
Node_<KeyType, ValueType> *curr = BinarySearchTree<KeyType,ValueType>::root;
Node_<KeyType,ValueType>* last_visited = BinarySearchTree<KeyType,ValueType>::root;
while (curr) {
if (curr->getKey() == k) {
found = 1;
return curr;
}
else if (k < curr->getKey()) {
last_visited = curr;
curr = curr->getLeft();
}
else {
last_visited = curr;
curr = curr->getRight();
}
}
return last_visited;
}
public:
typename BinarySearchTree<KeyType,ValueType>::iterator find(const KeyType& k){
if(BinarySearchTree<KeyType,ValueType>::root != NULL){
bool found = 0;
Node_<KeyType,ValueType>* searched_node_ = this->internalFind(k, found);
climb(searched_node_);
if(found){
typename BinarySearchTree<KeyType,ValueType>::iterator it(searched_node_);
return it;
}
else{
typename BinarySearchTree<KeyType,ValueType>::iterator it(NULL);
return it;
}
}
else{
typename BinarySearchTree<KeyType,ValueType>::iterator it(NULL);
return it;
}
}
void insert(const std::pair<const KeyType, ValueType>& new_item){
if(BinarySearchTree<KeyType,ValueType>::root == NULL){
Node_<KeyType,ValueType>* root_ = new Node_<KeyType, ValueType>(new_item.first, new_item.second, NULL);
root_->setLeft(NULL);
root_->setRight(NULL);
BinarySearchTree<KeyType, ValueType>::root = root_;
return;
}
else{
Node_<KeyType,ValueType>* tobefixed = new Node_<KeyType, ValueType>(new_item.first, new_item.second, NULL);
BinarySearchTree<KeyType,ValueType>::insert(tobefixed);
climb(tobefixed);
}
}
protected:
void climb(Node_<KeyType,ValueType>* current_node_){
if(current_node_ == NULL);//NULL calse
else if(current_node_->getParent() == NULL);//if current is the root
else if(current_node_->getParent()->getParent() == NULL){
if(AmIaRightChild(current_node_)){
rotateLeft(current_node_->getParent());
}
else if(AmIaLeftChild(current_node_)){
rotateRight(current_node_->getParent());
}
}
else{
//case 2: leftzig-zig:
if(AmIleftzigzig(current_node_)){
rotateRight(current_node_->getParent()->getParent());
rotateRight(current_node_->getParent());
}
else if(AmIrightzigzig(current_node_)){//case 2: rightzig-zig
rotateLeft(current_node_->getParent()->getParent());
rotateLeft(current_node_->getParent());
}
else if(AmIleftzigzag(current_node_)){
//case 3: leftzig-zag. current node_ is leftchild
//parent of current node_ is right child
rotateRight(current_node_->getParent());
rotateLeft(current_node_->getParent());
}
else if(AmIrightzigzag(current_node_)){
//case 3: right zigzag
rotateLeft(current_node_->getParent());
rotateRight(current_node_->getParent());
}
climb(current_node_);
}
}
/* g
/ \
p
/
node_ */
bool AmIleftzigzig(Node_<KeyType,ValueType>* node_){
if(AmIaLeftChild(node_)){
if(AmIaLeftChild(node_->getParent())){
return true;
}
return false;
}
return false;
}
/* g
/ \
p
\
node_ */
bool AmIrightzigzig(Node_<KeyType,ValueType>* node_){
if(AmIaRightChild(node_)){
if(AmIaRightChild(node_->getParent())){
return true;
}
return false;
}
return false;
}
/* g
/ \
p
/
node_ */
bool AmIleftzigzag(Node_<KeyType,ValueType>* node_){
if(AmIaLeftChild(node_)){
if(AmIaRightChild(node_->getParent())){
return true;
}
return false;
}
return false;
}
/* g
/ \
p
\
node_ */
bool AmIrightzigzag(Node_<KeyType,ValueType>* node_){
if(AmIaRightChild(node_)){
if(AmIaLeftChild(node_->getParent())){
return true;
}
return false;
}
return false;
}
void rotateRight(Node_<KeyType, ValueType>* node_){
Node_<KeyType,ValueType>* temp = node_->getLeft();
node_->setLeft(node_->getLeft()->getRight());
if(temp->getRight() != NULL){
temp->getRight()->setParent(node_);
}
temp->setParent(node_->getParent());
if(node_->getParent() == NULL){
BinarySearchTree<KeyType, ValueType>::root = temp;
}
else{
if(AmIaRightChild(node_)){
node_->getParent()->setRight(temp);
}
else{
node_->getParent()->setLeft(temp);
}
}
temp->setRight(node_);
node_->setParent(temp);
}
void rotateLeft(Node_<KeyType, ValueType>* node_){
Node_<KeyType,ValueType>* temp = node_->getRight();
node_->setRight(node_->getRight()->getLeft());
if(temp->getLeft() != NULL){
temp->getLeft()->setParent(node_);
}
temp->setParent(node_->getParent());
if(node_->getParent() == NULL){
BinarySearchTree<KeyType, ValueType>::root = temp;
}
else{
if(AmIaLeftChild(node_)){
node_->getParent()->setLeft(temp);
}
else{
node_->getParent()->setRight(temp);
}
}
temp->setLeft(node_);
node_->setParent(temp);
}
bool AmIaRightChild(const Node_<KeyType, ValueType>* new_item){
if(new_item->getParent() == NULL){
return 0;
}
return new_item->getParent()->getRight() == new_item;
}
bool AmIaLeftChild(const Node_<KeyType, ValueType>* new_item){
if(new_item->getParent() == NULL){
return 0;
}
return new_item->getParent()->getLeft() == new_item;
}
};