-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplayTree.java
More file actions
311 lines (267 loc) · 8.62 KB
/
SplayTree.java
File metadata and controls
311 lines (267 loc) · 8.62 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
// SplayTree class
//
// CONSTRUCTION: with no initializer
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// boolean contains( x ) --> Return true if x is found
// Comparable findMin( ) --> Return smallest item
// Comparable findMax( ) --> Return largest item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// Throws UnderflowException as appropriate
/**
* Implements a top-down splay tree.
* Note that all "matching" is based on the compareTo method.
* @author Mark Allen Weiss
*/
public class SplayTree<AnyType extends Comparable<? super AnyType>>
{
/**
* Construct the tree.
*/
public SplayTree( )
{
nullNode = new BinaryNode<AnyType>( null );
nullNode.left = nullNode.right = nullNode;
root = nullNode;
}
private BinaryNode<AnyType> newNode = null; // Used between different inserts
/**
* Insert into the tree.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
if( newNode == null )
newNode = new BinaryNode<AnyType>( null );
newNode.element = x;
if( root == nullNode )
{
newNode.left = newNode.right = nullNode;
root = newNode;
}
else
{
root = splay( x, root );
int compareResult = x.compareTo( root.element );
if( compareResult < 0 )
{
newNode.left = root.left;
newNode.right = root;
root.left = nullNode;
root = newNode;
}
else
if( compareResult > 0 )
{
newNode.right = root.right;
newNode.left = root;
root.right = nullNode;
root = newNode;
}
else
return; // No duplicates
}
newNode = null; // So next insert will call new
}
/**
* Remove from the tree.
* @param x the item to remove.
*/
public void remove( AnyType x )
{
if( !contains( x ) )
return;
BinaryNode<AnyType> newTree;
// If x is found, it will be splayed to the root by contains
if( root.left == nullNode )
newTree = root.right;
else
{
// Find the maximum in the left subtree
// Splay it to the root; and then attach right child
newTree = root.left;
newTree = splay( x, newTree );
newTree.right = root.right;
}
root = newTree;
}
/**
* Find the smallest item in the tree.
* Not the most efficient implementation (uses two passes), but has correct
* amortized behavior.
* A good alternative is to first call find with parameter
* smaller than any item in the tree, then call findMin.
* @return the smallest item or throw UnderflowException if empty.
*/
public AnyType findMin( )
{
if( isEmpty( ) )
throw new UnderflowException( );
BinaryNode<AnyType> ptr = root;
while( ptr.left != nullNode )
ptr = ptr.left;
root = splay( ptr.element, root );
return ptr.element;
}
/**
* Find the largest item in the tree.
* Not the most efficient implementation (uses two passes), but has correct
* amortized behavior.
* A good alternative is to first call find with parameter
* larger than any item in the tree, then call findMax.
* @return the largest item or throw UnderflowException if empty.
*/
public AnyType findMax( )
{
if( isEmpty( ) )
throw new UnderflowException( );
BinaryNode<AnyType> ptr = root;
while( ptr.right != nullNode )
ptr = ptr.right;
root = splay( ptr.element, root );
return ptr.element;
}
/**
* Find an item in the tree.
* @param x the item to search for.
* @return true if x is found; otherwise false.
*/
public boolean contains( AnyType x )
{
if( isEmpty( ) )
return false;
root = splay( x, root );
return root.element.compareTo( x ) == 0;
}
/**
* Make the tree logically empty.
*/
public void makeEmpty( )
{
root = nullNode;
}
/**
* Test if the tree is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( )
{
return root == nullNode;
}
private BinaryNode<AnyType> header = new BinaryNode<AnyType>( null ); // For splay
/**
* Internal method to perform a top-down splay.
* The last accessed node becomes the new root.
* @param x the target item to splay around.
* @param t the root of the subtree to splay.
* @return the subtree after the splay.
*/
private BinaryNode<AnyType> splay( AnyType x, BinaryNode<AnyType> t )
{
BinaryNode<AnyType> leftTreeMax, rightTreeMin;
header.left = header.right = nullNode;
leftTreeMax = rightTreeMin = header;
nullNode.element = x; // Guarantee a match
for( ; ; )
{
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
{
if( x.compareTo( t.left.element ) < 0 )
t = rotateWithLeftChild( t );
if( t.left == nullNode )
break;
// Link Right
rightTreeMin.left = t;
rightTreeMin = t;
t = t.left;
}
else if( compareResult > 0 )
{
if( x.compareTo( t.right.element ) > 0 )
t = rotateWithRightChild( t );
if( t.right == nullNode )
break;
// Link Left
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;
}
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
*/
private static <AnyType> BinaryNode<AnyType> rotateWithLeftChild( BinaryNode<AnyType> k2 )
{
BinaryNode<AnyType> k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
return k1;
}
/**
* Rotate binary tree node with right child.
* For AVL trees, this is a single rotation for case 4.
*/
private static <AnyType> BinaryNode<AnyType> rotateWithRightChild( BinaryNode<AnyType> k1 )
{
BinaryNode<AnyType> k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
return k2;
}
// Basic node stored in unbalanced binary search trees
private static class BinaryNode<AnyType>
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode<AnyType> left; // Left child
BinaryNode<AnyType> right; // Right child
}
private BinaryNode<AnyType> root;
private BinaryNode<AnyType> nullNode;
// Test program; should print min and max and nothing else
public static void main( String [ ] args )
{
SplayTree<Integer> t = new SplayTree<Integer>( );
final int NUMS = 40000;
final int GAP = 307;
System.out.println( "Checking... (no bad output means success)" );
for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
t.insert( i );
System.out.println( "Inserts complete" );
for( int i = 1; i < NUMS; i += 2 )
t.remove( i );
System.out.println( "Removes complete" );
if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
System.out.println( "FindMin or FindMax error!" );
for( int i = 2; i < NUMS; i += 2 )
if( !t.contains( i ) )
System.out.println( "Error: find fails for " + i );
for( int i = 1; i < NUMS; i += 2 )
if( t.contains( i ) )
System.out.println( "Error: Found deleted item " + i );
}
}