-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplayTree.java
215 lines (178 loc) · 5.46 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
package org.gra4j.trochilus;
import java.util.LinkedList;
import java.util.Queue;
public class SplayTree<E extends Comparable<? super E>> {
private BinaryNode<E> root;
private BinaryNode<E> nullNode;
private BinaryNode<E> header = new BinaryNode<>(null);
private BinaryNode<E> newNode = null;
public SplayTree () {
nullNode = new BinaryNode<>(null);
nullNode.left = nullNode.right = nullNode;
root = nullNode;
}
private static class BinaryNode<E> {
E element;
BinaryNode<E> left;
BinaryNode<E> right;
BinaryNode(E element) {
this(element, null, null);
}
BinaryNode(E element, BinaryNode<E> left, BinaryNode<E> right) {
this.element = element;
this.left = left;
this.right = right;
}
}
public boolean isEmpty () {
return root == nullNode;
}
public void makeEmpty () {
root = nullNode;
}
public E findMin () {
return findMin(root);
}
private E findMin (BinaryNode<E> t) {
if (t == nullNode)
return null;
if (t.left == nullNode)
return t.element;
return findMin(t.left);
}
public E findMax () {
return findMax(root);
}
private E findMax (BinaryNode<E> t) {
if (t == nullNode)
return null;
if (t.right == nullNode)
return t.element;
return findMax(t.right);
}
private BinaryNode<E> splay (E element, BinaryNode<E> t) {
BinaryNode<E> leftTreeMax, rightTreeMin;
header.left = header.right = nullNode;
leftTreeMax = rightTreeMin = header;
nullNode.element = element;
for (;;)
if (element.compareTo(t.element) < 0) {
if (element.compareTo(t.left.element) < 0)
t = rotateWithLeftChild(t);
if (t.left == nullNode)
break;
rightTreeMin.left = t;
rightTreeMin = t;
t = t.left;
} else if (element.compareTo(t.element) > 0) {
if (element.compareTo(t.right.element) > 0)
t = rotateWithRightChild(t);
if (t.right == nullNode)
break;
leftTreeMax.right = t;
leftTreeMax = t;
t = t.right;
} else
break;
leftTreeMax.right = t.left;
rightTreeMin.left = t.right;
t.left = header.right;
t.right = header.left;
return t;
}
public void insert (E element) {
if (newNode == null)
newNode = new BinaryNode<>(null);
newNode.element = element;
if (root == nullNode) {
newNode.left = newNode.right = nullNode;
root = newNode;
} else {
root = splay(element, root);
if (element.compareTo(root.element) < 0) {
newNode.left = root.left;
newNode.right = root;
root.left = nullNode;
root = newNode;
} else if (element.compareTo(root.element) > 0) {
newNode.right = root.right;
newNode.left = root;
root.right = nullNode;
root = newNode;
} else
return;
}
newNode = null;
}
public void remove (E element) {
BinaryNode<E> newTree;
if (!contains(element))
return;
root = splay(element, root);
if (root.left == nullNode)
newTree = root.right;
else {
newTree = root.left;
newTree = splay(element, newTree);
newTree.right = root.right;
}
root = newTree;
}
public boolean contains (E element) {
return contains(element, root);
}
private boolean contains (E element, BinaryNode<E> t) {
if (t == nullNode)
return false;
int compareResult = element.compareTo(t.element);
if (compareResult > 0)
return contains(element, t.right);
else if (compareResult < 0)
return contains(element, t.left);
else
return true;
}
public void printTree () {
if (isEmpty())
System.out.println("Empty tree");
else
printTree(root);
}
private void printTree (BinaryNode<E> t) {
if (t == nullNode)
return;
printTree(t.left);
System.out.println(t.element);
printTree(t.right);
}
public void levelOrder () {
if (isEmpty())
System.out.println("Empty tree");
else
levelOrder(root);
}
private void levelOrder (BinaryNode<E> t) {
Queue<BinaryNode> queue = new LinkedList<>();
queue.add(t);
while (!queue.isEmpty()) {
BinaryNode node = queue.poll();
if (node != nullNode) {
System.out.println(node.element);
queue.add(node.left);
queue.add(node.right);
}
}
}
private BinaryNode<E> rotateWithRightChild(BinaryNode<E> t) {
BinaryNode<E> right = t.right;
t.right = right.left;
right.left = t;
return right;
}
private BinaryNode<E> rotateWithLeftChild(BinaryNode<E> t) {
BinaryNode<E> left = t.left;
t.left = left.right;
left.right = t;
return left;
}
}