-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplayTree.h
executable file
·343 lines (297 loc) · 8.69 KB
/
SplayTree.h
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef SPLAY_TREE_H
#define SPLAY_TREE_H
#include "dsexceptions.h"
#include <iostream>
using namespace std;
// SplayTree class
//
// CONSTRUCTION: with no parameters
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// bool contains( x ) --> Return true if x is present
// Comparable findMin( ) --> Return smallest item
// Comparable findMax( ) --> Return largest item
// bool isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
// ******************ERRORS********************************
// Throws UnderflowException as warranted
template <typename Comparable>
class SplayTree
{
public:
SplayTree( )
{
nullNode = new BinaryNode;
nullNode->left = nullNode->right = nullNode;
root = nullNode;
}
SplayTree( const SplayTree & rhs )
{
nullNode = new BinaryNode;
nullNode->left = nullNode->right = nullNode;
root = clone( rhs.root );
}
SplayTree( SplayTree && rhs ) : root{ rhs.root }, nullNode{ rhs.nullNode }
{
rhs.root = nullptr;
rhs.nullNode = nullptr;
}
~SplayTree( )
{
makeEmpty( );
delete nullNode;
}
/**
* Deep copy.
*/
SplayTree & operator=( const SplayTree & rhs )
{
SplayTree copy = rhs;
std::swap( *this, copy );
return *this;
}
/**
* Move.
*/
SplayTree & operator=( SplayTree && rhs )
{
std::swap( root, rhs.root );
std::swap( nullNode, rhs.nullNode );
return *this;
}
/**
* 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.
*/
const Comparable & findMin( )
{
if( isEmpty( ) )
throw UnderflowException{ };
BinaryNode *ptr = root;
while( ptr->left != nullNode )
ptr = ptr->left;
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.
*/
const Comparable & findMax( )
{
if( isEmpty( ) )
throw UnderflowException{ };
BinaryNode *ptr = root;
while( ptr->right != nullNode )
ptr = ptr->right;
splay( ptr->element, root );
return ptr->element;
}
bool contains( const Comparable & x )
{
if( isEmpty( ) )
return false;
splay( x, root );
return root->element == x;
}
bool isEmpty( ) const
{
return root == nullNode;
}
void printTree( ) const
{
if( isEmpty( ) )
cout << "Empty tree" << endl;
else
printTree( root );
}
void makeEmpty( )
{
/******************************
* Comment this out, because it is prone to excessive
* recursion on degenerate trees. Use alternate algorithm.
reclaimMemory( root );
root = nullNode;
*******************************/
while( !isEmpty( ) )
{
findMax( ); // Splay max item to root
remove( root->element );
}
}
void insert( const Comparable & x )
{
static BinaryNode *newNode = nullptr;
if( newNode == nullptr )
newNode = new BinaryNode;
newNode->element = x;
if( root == nullNode )
{
newNode->left = newNode->right = nullNode;
root = newNode;
}
else
{
splay( x, root );
if( x < root->element )
{
newNode->left = root->left;
newNode->right = root;
root->left = nullNode;
root = newNode;
}
else
if( root->element < x )
{
newNode->right = root->right;
newNode->left = root;
root->right = nullNode;
root = newNode;
}
else
return;
}
newNode = nullptr; // So next insert will call new
}
void remove( const Comparable & x )
{
// If x is found, it will be splayed to the root by contains
if( !contains( x ) )
return; // Item not found; do nothing
BinaryNode *newTree;
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;
splay( x, newTree );
newTree->right = root->right;
}
delete root;
root = newTree;
}
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode( ) : left{ nullptr }, right{ nullptr } { }
BinaryNode( const Comparable & theElement, BinaryNode *lt, BinaryNode *rt )
: element{ theElement }, left{ lt }, right{ rt } { }
};
BinaryNode *root;
BinaryNode *nullNode;
/**
* Internal method to reclaim internal nodes in subtree t.
* WARNING: This is prone to running out of stack space.
*/
void reclaimMemory( BinaryNode * t )
{
if( t != t->left )
{
reclaimMemory( t->left );
reclaimMemory( t->right );
delete t;
}
}
/**
* Internal method to print a subtree t in sorted order.
* WARNING: This is prone to running out of stack space.
*/
void printTree( BinaryNode *t ) const
{
if( t != t->left )
{
printTree( t->left );
cout << t->element << endl;
printTree( t->right );
}
}
/**
* Internal method to clone subtree.
* WARNING: This is prone to running out of stack space.
*/
BinaryNode * clone( BinaryNode * t ) const
{
if( t == t->left ) // Cannot test against nullNode!!!
return nullNode;
else
return new BinaryNode{ t->element, clone( t->left ), clone( t->right ) };
}
// Tree manipulations
void rotateWithLeftChild( BinaryNode * & k2 )
{
BinaryNode *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2 = k1;
}
void rotateWithRightChild( BinaryNode * & k1 )
{
BinaryNode *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1 = k2;
}
/**
* Internal method to perform a top-down splay.
* The last accessed node becomes the new root.
* This method may be overridden to use a different
* splaying algorithm, however, the splay tree code
* depends on the accessed item going to the root.
* x is the target item to splay around.
* t is the root of the subtree to splay.
*/
void splay( const Comparable & x, BinaryNode * & t )
{
BinaryNode *leftTreeMax, *rightTreeMin;
static BinaryNode header;
header.left = header.right = nullNode;
leftTreeMax = rightTreeMin = &header;
nullNode->element = x; // Guarantee a match
for( ; ; )
if( x < t->element )
{
if( x < t->left->element )
rotateWithLeftChild( t );
if( t->left == nullNode )
break;
// Link Right
rightTreeMin->left = t;
rightTreeMin = t;
t = t->left;
}
else if( t->element < x )
{
if( t->right->element < x )
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;
}
};
#endif