-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplayTree.java
307 lines (272 loc) · 8.52 KB
/
SplayTree.java
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
/**
* @author Alan Garcia
* Node of the tree.
* There are some helper methods here specific to the application.
*/
class Node {
public Point record;
public String satellite;
public Node left, right, parent;
Node(Point rec, String sat) {
record = rec;
satellite = sat;
}
public String toString() {
return "record = " + record + ", satellite = " + satellite;
}
boolean isRoot() {
return parent == null;
}
boolean isLeftChild() {
return this == parent.left;
}
boolean isRightChild() {
return this == parent.right;
}
}
/**
* Splay tree (BST invented by Sleator and Tarjan).
*/
public class SplayTree {
Node root, current;
public SplayTree() {
root = null;
}
/**
* Portray tree as a string.
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder("");
toStringHelper(sb, root);
return sb.toString();
}
/**
* Pre-order traversal of tree.
*/
private void toStringHelper( StringBuilder strb, Node current ) {
if(current == null)
return;
strb.append("point " + current.record.toString() +
" " + current.satellite);
if(current.left != null)
strb.append(" " + current.left.record.toString());
if(current.right != null)
strb.append(" " + current.right.record.toString());
strb.append("\n");
if(current.left != null)
toStringHelper(strb, current.left);
if(current.right != null)
toStringHelper(strb, current.right);
}
/**
* Search tree for key k. Return its satellite data if found,
* and splay the found node.
* If not found, return null, and splay the would-be parent node.
*/
public String lookup(Point key) {
return lookupHelper(key, root);
}
private String lookupHelper(Point key, Node current) {
if(current == null)
return null;
// recurse down the tree
if(key.compareTo(current.record) > 0)
return lookupHelper(key, current.left);
else if(key.compareTo(current.record) < 0)
return lookupHelper(key, current.right);
else if(key.compareTo(current.record) == 0) {
splay(current);
return current.satellite;
} else
return null;
}
/**
* It's lookup but without splaying and
* returns a node instead of a point.
* Used for deletion.
*/
private Node find(Point key, Node current) {
if(current == null)
return null;
if(key.compareTo(current.record) > 0)
return find(key, current.left);
else if(key.compareTo(current.record) < 0)
return find(key, current.right);
else if(key.compareTo(current.record) == 0)
return current;
else
return null;
}
/**
* Insert a new record.
* First we do a search for the key.
* If the search fails, we insert a new record.
* Otherwise we update the satellite data with sat.
* Splay the new, or altered, node.
*/
public void insert_record(Point key, String sat) {
root = insertHelper(key, sat, root);
splay(root);
}
/**
* Recurses down the tree to where the new node
* should go, if a node exists with that key already,
* the data is updated instead.
*/
private Node insertHelper(Point key, String sat, Node current) {
if(current == null) // tree is empty or node doesnt exist: create it
current = new Node(key, sat);
if(key.compareTo(current.record) > 0) {
Node temp = insertHelper(key, sat, current.left);
current.left = temp;
temp.parent = current;
}
else if(key.compareTo(current.record) < 0) {
Node temp = insertHelper(key, sat, current.right);
current.right = temp;
temp.parent = current;
}
else if(key.compareTo(current.record) == 0)
current.satellite = sat;
return current;
}
/**
* Remove a record.
* Search for the key. If not found, return null.
* If found, save the satellite data, remove the record,
* and splay the bereaved parent.
*
* Return the satellite data from the deleted node.
*/
public String delete(Point key) {
Node n = find(key, root);
// avoid null pointers for return string
String result = n == null ? null : n.satellite;
// do the work
root = deleteHelper(key, root);
// splay the bereaved parent
if(root != null && !root.isRoot())
splay(root.parent);
return result;
}
/**
* Recurses down the tree, finds the node to delete
* and gets rid of it.
*/
private Node deleteHelper(Point key, Node current) {
if(current == null)
return current;
if(key.compareTo(current.record) > 0)
current.left = deleteHelper(key, current.left);
else if(key.compareTo(current.record) < 0)
current.right = deleteHelper(key, current.right);
else {
if(current.left == null) {
if(current.right != null)
current.right.parent = current.parent;
return current.right;
}
else if(current.right == null) {
if(current.left != null)
current.left.parent = current.parent;
return current.left;
}
else {
Node temp = findMin(current.right); // in-order successor
current.record = temp.record; // update values
current.satellite = temp.satellite;
temp = deleteHelper(current.record, current.right);
current.right = temp; // update right and parent pointers
if(temp != null)
temp.parent = current;
}
}
return current;
}
/**
* Recurses down the tree and finds the
* minimum value in that tree.
* Useful for finding the in-order successor
* of a given node by calling findMin(node.right).
* Uses tail recursion.
*/
private Node findMin(Node current) {
if(current == null || current.left == null)
return current;
return findMin(current.left);
}
/**
* Following two methods are simple
* node rotations, there's nothing
* special to them.
*/
private void rotateLeft(Node n) {
Node temp = n.right;
if(temp != null) {
n.right = temp.left;
if(temp.left != null)
temp.left.parent = n;
temp.parent = n.parent;
}
if(n.isRoot())
root = temp;
else if(n.isLeftChild())
n.parent.left = temp;
else
n.parent.right = temp;
if(temp != null)
temp.left = n;
n.parent = temp;
}
private void rotateRight(Node n) {
Node temp = n.left;
if(temp != null) {
n.left = temp.right;
if(temp.right != null)
temp.right.parent = n;
temp.parent = n.parent;
}
if(n.isRoot())
root = temp;
else if(n.isLeftChild())
n.parent.left = temp;
else
n.parent.right = temp;
if(temp != null)
temp.right = n;
n.parent = temp;
}
/**
* Recursive bottom-up splaying of tree at node n.
*/
private void splay(Node n) {
if(n.isRoot()) // n is the root, we're done.
return;
if(n.parent.isRoot()) { // zig!
if(n.isLeftChild())
rotateRight(n.parent);
else
rotateLeft(n.parent);
}
else if(n.isLeftChild() && n.parent.isLeftChild()) { // zig-zig!
rotateRight(n.parent.parent);
rotateRight(n.parent);
}
else if(n.isRightChild() && n.parent.isRightChild()) { // zig-zig!
rotateLeft(n.parent.parent);
rotateLeft(n.parent);
}
else if(n.isLeftChild() && n.parent.isRightChild()) { // zig-zag!
rotateRight(n.parent);
rotateLeft(n.parent);
}
else { // zig-zag!
rotateLeft(n.parent);
rotateRight(n.parent);
}
splay(n); // tail recursion!
}
}