-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftistHeap.cpp
More file actions
221 lines (201 loc) · 6.39 KB
/
LeftistHeap.cpp
File metadata and controls
221 lines (201 loc) · 6.39 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
#include "LeftistHeap.h"
#include "dsexceptions.h"
/**
* Construct the leftist heap.
*/
template <class Comparable>
LeftistHeap<Comparable>::LeftistHeap( )
{
root = NULL;
}
/**
* Copy constructor.
*/
template <class Comparable>
LeftistHeap<Comparable>::LeftistHeap( const LeftistHeap<Comparable> & rhs )
{
root = NULL;
*this = rhs;
}
/**
* Destruct the leftist heap.
*/
template <class Comparable>
LeftistHeap<Comparable>::~LeftistHeap( )
{
makeEmpty( );
}
/**
* Merge rhs into the priority queue.
* rhs becomes empty. rhs must be different from this.
*/
template <class Comparable>
void LeftistHeap<Comparable>::merge( LeftistHeap & rhs )
{
if( this == &rhs ) // Avoid aliasing problems
return;
root = merge( root, rhs.root );
rhs.root = NULL;
}
/**
* Internal method to merge two roots.
* Deals with deviant cases and calls recursive merge1.
*/
template <class Comparable>
LeftistNode<Comparable> *
LeftistHeap<Comparable>::merge( LeftistNode<Comparable> * h1,
LeftistNode<Comparable> * h2 ) const
{
if( h1 == NULL )
return h2;
if( h2 == NULL )
return h1;
if( h1->element < h2->element )
return merge1( h1, h2 );
else
return merge1( h2, h1 );
}
/**
* Internal method to merge two roots.
* Assumes trees are not empty, and h1's root contains smallest item.
*/
template <class Comparable>
LeftistNode<Comparable> *
LeftistHeap<Comparable>::merge1( LeftistNode<Comparable> * h1,
LeftistNode<Comparable> * h2 ) const
{
if( h1->left == NULL ) // Single node
h1->left = h2; // Other fields in h1 already accurate
else
{
h1->right = merge( h1->right, h2 );
if( h1->left->npl < h1->right->npl )
swapChildren( h1 );
h1->npl = h1->right->npl + 1;
}
return h1;
}
/**
* Swaps t's two children.
*/
template <class Comparable>
void LeftistHeap<Comparable>::swapChildren( LeftistNode<Comparable> * t ) const
{
LeftistNode<Comparable> *tmp = t->left;
t->left = t->right;
t->right = tmp;
}
/**
* Insert item x into the priority queue, maintaining heap order.
*/
template <class Comparable>
void LeftistHeap<Comparable>::insert( const Comparable & x )
{
root = merge( new LeftistNode<Comparable>( x ), root );
}
/**
* Find the smallest item in the priority queue.
* Return the smallest item, or throw Underflow if empty.
*/
template <class Comparable>
const Comparable & LeftistHeap<Comparable>::findMin( ) const
{
if( isEmpty( ) )
throw Underflow( );
return root->element;
}
/**
* Remove the smallest item from the priority queue.
* Throws Underflow if empty.
*/
template <class Comparable>
void LeftistHeap<Comparable>::deleteMin( )
{
if( isEmpty( ) )
throw Underflow( );
LeftistNode<Comparable> *oldRoot = root;
root = merge( root->left, root->right );
delete oldRoot;
}
/**
* Remove the smallest item from the priority queue.
* Pass back the smallest item, or throw Underflow if empty.
*/
template <class Comparable>
void LeftistHeap<Comparable>::deleteMin( Comparable & minItem )
{
minItem = findMin( );
deleteMin( );
}
/**
* Test if the priority queue is logically empty.
* Returns true if empty, false otherwise.
*/
template <class Comparable>
bool LeftistHeap<Comparable>::isEmpty( ) const
{
return root == NULL;
}
/**
* Test if the priority queue is logically full.
* Returns false in this implementation.
*/
template <class Comparable>
bool LeftistHeap<Comparable>::isFull( ) const
{
return false;
}
/**
* Make the priority queue logically empty.
*/
template <class Comparable>
void LeftistHeap<Comparable>::makeEmpty( )
{
reclaimMemory( root );
root = NULL;
}
/**
* Deep copy.
*/
template <class Comparable>
const LeftistHeap<Comparable> &
LeftistHeap<Comparable>::
operator=( const LeftistHeap<Comparable> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
root = clone( rhs.root );
}
return *this;
}
/**
* Internal method to make the tree empty.
* WARNING: This is prone to running out of stack space;
* exercises suggest a solution.
*/
template <class Comparable>
void LeftistHeap<Comparable>::reclaimMemory( LeftistNode<Comparable> * t ) const
{
if( t != NULL )
{
reclaimMemory( t->left );
reclaimMemory( t->right );
delete t;
}
}
/**
* Internal method to clone subtree.
* WARNING: This is prone to running out of stack space.
* exercises suggest a solution.
*/
template <class Comparable>
LeftistNode<Comparable> *
LeftistHeap<Comparable>::clone( LeftistNode<Comparable> * t ) const
{
if( t == NULL )
return NULL;
else
return new LeftistNode<Comparable>( t->element, clone( t->left ),
clone( t->right ), t->npl );
}