-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplay.java
More file actions
323 lines (235 loc) · 9.78 KB
/
Splay.java
File metadata and controls
323 lines (235 loc) · 9.78 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
/* Whitney Humecky
* Project 1
* CS 3345.0U2
* Dr. Zhao
* Class constructs a splay tree, performs
* insertion, deletion, and search functions
*/
import java.util.*;
public class Splay {
Node root;
Splay() {
root = null;
}
Splay(int k) {
root = new Node(k);
travel(root);
}
// Method locates appropriate location via binary search for new node
// then calls bottomUp(node) to push new node to be new root
// and travel(node) to print the new splay tree
void insert(int k) {
Node K = new Node(k);
Stack<Node> stack1 = new Stack<Node>(); // To store parents of K
System.out.print("\n\nInserting: " + k);
if (root == null) { // set root
root = K;
} else {
Node current = root;
// locate appropriate parent to insert K in BST
while ((k < current.value && current.left != null) || (k >= current.value && current.right != null)) {
stack1.push(current);
if (current.value <= k) { // K is right child or of right subtree
current = current.right;
} else { // K is left child or of left subtree
current = current.left;
}
} // current is now set to desired parent of K
stack1.push(current);
if (k < current.value) { // make K left child
current.left = new Node(k);
current.left = K;
} else { // OR make K right child
current.right = new Node(k);
current.right = K;
}
}
bottomUp(K, stack1); // Push K to root
travel(root); // Print tree
}
// Method calls searchFunction() to identify node and move it to root
// then performs replacement of root and prints new tree via travel()
void delete(int k) {
Stack<Node> stack2 = new Stack<Node>(); // Store parents
System.out.print("\n\nDeleting: " + k);
// Search for K
if (searchFunction(k)) {
// If K was found, it is now root
if (root.right == null) {
// no right subtree, top of left subtree is now root
root = root.left;
} else if (root.left == null) {
// no left subtree, top of right subtree is now root
root = root.right;
} else {
Node current = root.left;
// identify largest value of left subtree
while (current.right != null) {
stack2.push(current);
current = current.right;
}
if (!stack2.empty()) {
// If largest value is not the top of left subtree, set parent to point to null
Node parent = stack2.pop();
parent.right = null;
}
// make value root, thus removing K
current.right = root.right;
root.left = current.left;
root = current;
}
travel(root);
}
}
// Method identifies if tree contains value k
// calls bottomUp() to perform rotations to move node
// for use by deletion and search methods
// returns true if value is found in tree
boolean searchFunction(int k) {
Node current = root;
Stack<Node> stack3 = new Stack<Node>();
while ((k < current.value && current.left != null) || (k > current.value && current.right != null)) {
stack3.push(current);
if (current.value < k) {
current = current.right;
} else {
current = current.left;
}
}
bottomUp(current, stack3);
if (current.value == k) {
return true;
} else { // k is not found in tree
return false;
}
}
// Method calls searchFunction() to perform search
// and travel() to print new tree, returns true if value is in tree
boolean search(int k) {
System.out.print("\n\nSearching for: " + k);
if (searchFunction(k)) {
travel(root);
return true;
} else { // k is not found in tree
travel(root);
return false;
}
}
// Method performs splay rotations to move node K to root
void bottomUp(Node K, Stack<Node> stack) {
Node grand = null, parent = null;
if (root == K) {
return;
} else { // K is not root, has at least one parent
parent = stack.pop();
if (!stack.empty()) { // K has grand parent
grand = stack.pop();
// current is Left Left Grand child "zig-zig"
if (K.value < parent.value && parent.value < grand.value) {
// Right rotate about grandparent, grand becomes right child of parent
grand.left = parent.right;
parent.right = grand;
// Right rotate about parent, parent becomes right child of K
parent.left = K.right;
K.right = parent;
if (!stack.empty()) { // update new parent of K to point to K
if (stack.peek().value > K.value)
stack.peek().left = K;
else
stack.peek().right = K;
} else { // There is no next parent of K, K is root
root = K;
}
}
// Right Right Grandchild "zig-zig"
else if (K.value >= parent.value && parent.value >= grand.value) {
// Left rotate about grandparent, grand becomes left child of parent
grand.right = parent.left;
parent.left = grand;
// Left rotate about parent, parent becomes left child of K
parent.right = K.left;
K.left = parent;
if (!stack.empty()) { // update new parent of K to point to K
if (stack.peek().value > K.value)
stack.peek().left = K;
else
stack.peek().right = K;
} else { // There is no next parent of K, K is root
root = K;
}
}
// Left Right Grandchild "zig-zig"
else if (K.value < parent.value && parent.value >= grand.value) {
// Right rotate about parent, parent becomes right child of K
parent.left = K.right;
grand.right = K;
K.right = parent;
// Left rotate about grandparent, grand becomes left child of K
grand.right = K.left;
K.left = grand;
if (!stack.empty()) { // update new parent of K to point to K
if (stack.peek().value > K.value)
stack.peek().left = K;
else
stack.peek().right = K;
} else { // There is no next parent of K, K is root
root = K;
}
}
// Right Left Grandchild "zig-zag"
else {
// Left rotate about parent, parent becomes left child of K
parent.right = K.left;
grand.left = K;
K.left = parent;
// Right rotate about grandparent, grand becomes right child of K
grand.left = K.right;
K.right = grand;
if (!stack.empty()) { // update new parent of K to point to K
if (stack.peek().value > K.value)
stack.peek().left = K;
else
stack.peek().right = K;
} else { // There is no next parent of K, K is root
root = K;
}
}
} else { // K is direct child of root
Node temp = root;
// K is Left child
if (K.value < root.value) {
temp.left = K.right;
// K becomes root
temp.left = K.right;
root = K;
root.right = temp;
}
// K is Right Child
else {
temp = root;
// K becomes root
temp.right = K.left;
root = K;
root.left = temp;
}
}
if (root != K)
bottomUp(K, stack); // recurr until K is root
}
}
// Print tree via preorder traversal
void travel(Node current) {
if (root == current) {
System.out.print("\n" + current.value + "RT");
}
if (current.left != null) { // print left child
System.out.print(", " + current.left.value + "L");
travel(current.left); // print left child until bottom layer is reached
}
if (current.right != null) {
System.out.print(", " + current.right.value + "R");
travel(current.right);
return;
}
}
}