-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
SplayTree.java
326 lines (262 loc) · 8.59 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// javac -d classes -sourcepath src/main/java
// src/main/java/com/williamfiset/algorithms/datastructures/binarysearchtree/SplayTree.java
// java -cp classes com.williamfiset.algorithms.datastructures.binarysearchtree.SplayTreeRun
package com.williamfiset.algorithms.datastructures.binarysearchtree;
import com.williamfiset.algorithms.datastructures.utils.TreePrinter;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Standard Splay Tree Implementation, supports generic data(must implement Comparable)
*
* <p>The Basic Concept of SplayTree is to keep frequently used nodes close to the root of the tree
* It performs basic operations such as insertion,search,delete,findMin,findMax in O(log n)
* amortized time Having frequently-used nodes near to the root can be useful in implementing many
* algorithms. e.g: Implementing caches, garbage collection algorithms etc Primary disadvantage of
* the splay tree can be the fact that its height can go linear. This causes the worst case running
* times to go O(n) However, the amortized costs of this worst case situation is logarithmic, O(log
* n)
*
* @author Ashiqur Rahman,https://github.com/ashiqursuperfly
*/
public class SplayTree<T extends Comparable<T>> {
private BinaryTree<T> root;
public static class BinaryTree<T extends Comparable<T>> implements TreePrinter.PrintableNode {
private T data;
private BinaryTree<T> leftChild, rightChild;
public BinaryTree(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}
@Override
public BinaryTree<T> getLeft() {
return leftChild;
}
public void setLeft(BinaryTree<T> leftChild) {
this.leftChild = leftChild;
}
@Override
public BinaryTree<T> getRight() {
return rightChild;
}
public void setRight(BinaryTree<T> rightChild) {
this.rightChild = rightChild;
}
@Override
public String getText() {
return data.toString();
}
public T getData() {
return data;
}
public void setData(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
}
@Override
public String toString() {
return TreePrinter.getTreeDisplay(this);
}
}
/** Public Methods * */
public SplayTree() {
this.root = null;
}
public SplayTree(BinaryTree<T> root) {
this.root = root;
}
public BinaryTree<T> getRoot() {
return root;
}
/** Searches a node and splays it on top,returns the new root * */
public BinaryTree<T> search(T node) {
if (root == null) return null;
this.root = splayUtil(root, node);
return this.root.getData().compareTo(node) == 0 ? this.root : null;
}
/** Inserts a node into the tree and splays it on top, returns the new root* */
public BinaryTree<T> insert(T node) {
if (root == null) {
root = new BinaryTree<>(node);
return root;
}
splay(node);
ArrayList<BinaryTree<T>> l_r = split(node);
BinaryTree<T> left = l_r.get(0);
BinaryTree<T> right = l_r.get(1);
root = new BinaryTree<>(node);
root.setLeft(left);
root.setRight(right);
return root;
}
/** Deletes a node,returns the new root * */
public BinaryTree<T> delete(T node) {
if (root == null) return null;
BinaryTree<T> searchResult = splay(node);
if (searchResult.getData().compareTo(node) != 0) return null;
BinaryTree<T> leftSubtree = root.getLeft();
BinaryTree<T> rightSubtree = root.getRight();
// Set the 'to be deleted' key ready for garbage collection
root.setLeft(null);
root.setRight(null);
root = join(leftSubtree, rightSubtree);
return root;
}
/** To FindMax Of Entire Tree * */
public T findMax() {
BinaryTree<T> temp = root;
while (temp.getRight() != null) temp = temp.getRight();
return temp.getData();
}
/** To FindMin Of Entire Tree * */
public T findMin() {
BinaryTree<T> temp = root;
while (temp.getLeft() != null) temp = temp.getLeft();
return temp.getData();
}
/** * To FindMax Of Tree with specified root * */
public T findMax(BinaryTree<T> root) {
BinaryTree<T> temp = root;
while (temp.getRight() != null) temp = temp.getRight();
return temp.getData();
}
/** * To FindMin Of Tree with specified root * */
public T findMin(BinaryTree<T> root) {
BinaryTree<T> temp = root;
while (temp.getLeft() != null) temp = temp.getLeft();
return temp.getData();
}
@Override
public String toString() {
System.out.println("Elements:" + inorder(root, new ArrayList<>()));
return (root != null) ? root.toString() : null;
}
/** Private Methods * */
private BinaryTree<T> rightRotate(BinaryTree<T> node) {
BinaryTree<T> p = node.getLeft();
node.setLeft(p.getRight());
p.setRight(node);
return p;
}
private BinaryTree<T> leftRotate(BinaryTree<T> node) {
BinaryTree<T> p = node.getRight();
node.setRight(p.getLeft());
p.setLeft(node);
return p;
}
private BinaryTree<T> splayUtil(BinaryTree<T> root, T key) {
if (root == null || root.getData() == key) return root;
if (root.getData().compareTo(key) > 0) {
if (root.getLeft() == null) return root;
if (root.getLeft().getData().compareTo(key) > 0) {
root.getLeft().setLeft(splayUtil(root.getLeft().getLeft(), key));
root = rightRotate(root);
} else if (root.getLeft().getData().compareTo(key) < 0) {
root.getLeft().setRight(splayUtil(root.getLeft().getRight(), key));
if (root.getLeft().getRight() != null) root.setLeft(leftRotate(root.getLeft()));
}
return (root.getLeft() == null) ? root : rightRotate(root);
} else {
if (root.getRight() == null) return root;
if (root.getRight().getData().compareTo(key) > 0) {
root.getRight().setLeft(splayUtil(root.getRight().getLeft(), key));
if (root.getRight().getLeft() != null) root.setRight(rightRotate(root.getRight()));
} else if (root.getRight().getData().compareTo(key) < 0) // Zag-Zag (Right Right)
{
root.getRight().setRight(splayUtil(root.getRight().getRight(), key));
root = leftRotate(root);
}
return (root.getRight() == null) ? root : leftRotate(root);
}
}
private BinaryTree<T> splay(T node) {
if (root == null) return null;
this.root = splayUtil(root, node);
return this.root;
}
private ArrayList<BinaryTree<T>> split(T node) {
BinaryTree<T> right;
BinaryTree<T> left;
if (node.compareTo(root.getData()) > 0) {
right = root.getRight();
left = root;
left.setRight(null);
} else {
left = root.getLeft();
right = root;
right.setLeft(null);
}
ArrayList<BinaryTree<T>> l_r = new ArrayList<>();
l_r.add(left);
l_r.add(right);
return l_r;
}
private BinaryTree<T> join(BinaryTree<T> L, BinaryTree<T> R) {
if (L == null) {
root = R;
return R;
}
root = splayUtil(L, findMax(L));
root.setRight(R);
return root;
}
private ArrayList<T> inorder(BinaryTree<T> root, ArrayList<T> sorted) {
if (root == null) {
return sorted;
}
inorder(root.getLeft(), sorted);
sorted.add(root.getData());
inorder(root.getRight(), sorted);
return sorted;
}
}
class SplayTreeRun {
public static void main(String[] args) {
SplayTree<Integer> splayTree = new SplayTree<>();
Scanner sc = new Scanner(System.in);
int[] data = {2, 29, 26, -1, 10, 0, 2, 11};
int c = 0;
for (int i : data) {
splayTree.insert(i);
}
while (c != 7) {
System.out.println("1. Insert 2. Delete 3. Search 4.FindMin 5.FindMax 6. PrintTree 7. Exit");
c = sc.nextInt();
switch (c) {
case 1:
System.out.println("Enter Data :");
splayTree.insert(sc.nextInt());
break;
case 2:
System.out.println("Enter Element to be Deleted:");
splayTree.delete(sc.nextInt());
break;
case 3:
System.out.println("Enter Element to be Searched and Splayed:");
splayTree.search(sc.nextInt());
break;
case 4:
System.out.println("Min: " + splayTree.findMin());
break;
case 5:
System.out.println("Max: " + splayTree.findMax());
break;
case 6:
System.out.println(splayTree);
break;
case 7:
sc.close();
break;
}
}
}
}