-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplayTree.h
81 lines (63 loc) · 2.66 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
#ifndef _SPLAY_TREE_H_
#define _SPLAY_TREE_H_
#include "dsexceptions.h"
#include <stdlib.h>
// SplayTree class
//
// CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// Comparable find( x ) --> Return item that matches x
// Comparable findMin( ) --> Return smallest item
// Comparable findMax( ) --> Return largest item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
// Node and forward declaration because g++ does
// not understand nested classes.
template <class Comparable>
class SplayTree;
template <class Comparable>
class SplayBinaryNode
{
Comparable element;
SplayBinaryNode *left;
SplayBinaryNode *right;
SplayBinaryNode( ) : left( NULL ), right( NULL ) { }
SplayBinaryNode( const Comparable & theElement, SplayBinaryNode *lt, SplayBinaryNode *rt )
: element( theElement ), left( lt ), right( rt ) { }
friend class SplayTree<Comparable>;
};
template <class Comparable>
class SplayTree
{
public:
explicit SplayTree( const Comparable & notFound );
SplayTree( const SplayTree & rhs );
~SplayTree( );
const Comparable & findMin( );
const Comparable & findMax( );
const Comparable & find( const Comparable & x );
bool isEmpty( ) const;
void printTree( ) const;
void makeEmpty( );
void insert( const Comparable & x );
void remove( const Comparable & x );
const SplayTree & operator=( const SplayTree & rhs );
private:
SplayBinaryNode<Comparable> *root;
SplayBinaryNode<Comparable> *nullNode;
const Comparable ITEM_NOT_FOUND;
const Comparable & elementAt( SplayBinaryNode<Comparable> *t ) const;
void reclaimMemory( SplayBinaryNode<Comparable> * t ) const;
void printTree( SplayBinaryNode<Comparable> *t ) const;
SplayBinaryNode<Comparable> * clone( SplayBinaryNode<Comparable> *t ) const;
// Tree manipulations
void rotateWithLeftChild( SplayBinaryNode<Comparable> * & k2 ) const;
void rotateWithRightChild( SplayBinaryNode<Comparable> * & k1 ) const;
void splay( const Comparable & x, SplayBinaryNode<Comparable> * & t ) const;
};
#include "SplayTree.cpp"
#endif