-
Notifications
You must be signed in to change notification settings - Fork 0
/
splay_tree.cpp
171 lines (156 loc) · 3.97 KB
/
splay_tree.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
#include <cassert>
#include <cstddef>
using namespace std;
template<typename T>
struct node{
T value;//change this to make weird trees
node<T> *left, *right;
node<T> *parent;
node(const T& val) : value(val),left(NULL),right(NULL),parent(NULL){}
~node(){
if(left) delete left;
if(right) delete right;
}
};
//this just finds the node
template<typename T>
node<T> *node_find(node<T> *n,const T& val){
while(n){
if(n->value<val) n = n->right;
else if(val<n->value) n = n->left;
else return n;
}
return NULL;
}
template<typename T>
void disconnect_left(node<T> *n){
if(!n) return;
if(n->left) n->left->parent = NULL;
n->left = NULL;
}
template<typename T>
void disconnect_right(node<T> *n){
if(!n) return;
if(n->right) n->right->parent = NULL;
n->right = NULL;
}
template<typename T>
void disconnect_parent(node<T> *n){
if(!n || !n->parent) return;
if(n->parent->left == n) disconnect_left(n->parent);
else disconnect_right(n->parent);
}
template<typename T>
void connect_left(node<T> *n, node<T> *l){
disconnect_left(n);
disconnect_parent(l);
if(n) n->left = l;
if(l) l->parent = n;
}
template<typename T>
void connect_right(node<T> *n, node<T> *r){
disconnect_right(n);
disconnect_parent(r);
if(n) n->right= r;
if(r) r->parent = n;
}
template<typename T>
node<T> *node_insert(node<T> *n,const T& val){
if(!n) return new node<T>(val);
while(true){//this loop leaks if we are given a bad comparator
if(n->value<val){
if(!n->right) connect_right(n,new node<T>(val));
n = n->right;
}
else if(val<n->value){
if(!n->left) connect_left(n,new node<T>(val));
n = n->left;
}
else return n;
}
}
template<typename T>
void rotate_left(node<T> *n){
node<T> *r = n->right;
node<T> *p = n->parent;
if(p){
if(p->right==n) connect_right(p,r);
else connect_left(p,r);
}
connect_right(n,r->left);
connect_left(r,n);
}
template<typename T>
void rotate_right(node<T> *n){
node<T> *l = n->left;
node<T> *p = n->parent;
if(p){
if(p->right==n) connect_right(p,l);
else connect_left(p,l);
}
connect_left(n,l->right);
connect_right(l,n);
}
template<typename T>
void splay_node(node<T> *n){
if(!n) return;
while(n->parent){
if(!n->parent->parent){
if(n->parent->left==n) rotate_right(n->parent);
else rotate_left(n->parent);
}
else if(n->parent->left == n){
if(n->parent->parent->left == n->parent){
rotate_right(n->parent->parent);
rotate_right(n->parent);
}
else{
rotate_right(n->parent);
rotate_left(n->parent);
}
}
else if(n->parent->right == n){
if(n->parent->parent->right == n->parent){
rotate_left(n->parent->parent);
rotate_left(n->parent);
}
else{
rotate_left(n->parent);
rotate_right(n->parent);
}
}
}
}
//note, copying trees shares data, you have to do deep copies manually
template<typename T>
struct tree_set{
node<T> *root;
tree_set() : root(NULL) {}
~tree_set() {if(root) delete root;}
node<T> *find(const T& val){
root = node_find(root,val);
splay_node(root);
return root;
}
node<T> *insert(const T& val){
root = node_insert(root,val);
splay_node(root);
return root;
}
void erase(node<T> *n){
splay_node(n);
node<T> *l = n->left;
node<T> *r = n->right;
disconnect_left<T>(n);
disconnect_right<T>(n);
delete n;
if(l){
while(l->right) l = l->right;
splay_node(l);
connect_right(l,r);
root = l;
}
else
root = r;
}
};