Graphviz 12.1.0~dev.20240716.0947
Loading...
Searching...
No Matches
PairingHeap.cpp
Go to the documentation of this file.
1
25#include <vector>
26#include <list>
29
30#ifndef PAIRING_HEAP_CPP
31#define PAIRING_HEAP_CPP
32using namespace std;
36template <class T>
37PairingHeap<T>::PairingHeap( bool (*lessThan)(T const &lhs, T const &rhs) )
38{
39 root = nullptr;
40 this->lessThan=lessThan;
41}
42
43
47template <class T>
49{
50 makeEmpty( );
51}
52
57template <class T>
60{
62
63 if( root == nullptr )
64 root = newNode;
65 else
66 compareAndLink( root, newNode );
67 return newNode;
68}
73template <class T>
74const T & PairingHeap<T>::findMin( ) const
75{
76 if( isEmpty( ) )
77 throw Underflow( );
78 return root->element;
79}
84template <class T>
86{
87 if( isEmpty( ) )
88 throw Underflow( );
89
90 PairNode<T> *oldRoot = root;
91
92 if( root->leftChild == nullptr )
93 root = nullptr;
94 else
95 root = combineSiblings( root->leftChild );
96 delete oldRoot;
97}
98
103template <class T>
105{
106 return root == nullptr;
107}
108
112template <class T>
114{
115 reclaimMemory( root );
116 root = nullptr;
117}
118
123template <class T>
125{
126 if( t != nullptr )
127 {
128 reclaimMemory( t->leftChild );
129 reclaimMemory( t->nextSibling );
130 delete t;
131 }
132}
133
142template <class T>
144compareAndLink( PairNode<T> * & first,
145 PairNode<T> *second ) const
146{
147 if( second == nullptr )
148 return;
149 if( lessThan(second->element,first->element) )
150 {
151 // Attach first as leftmost child of second
152 second->prev = first->prev;
153 first->prev = second;
154 first->nextSibling = second->leftChild;
155 if( first->nextSibling != nullptr )
156 first->nextSibling->prev = first;
157 second->leftChild = first;
158 first = second;
159 }
160 else
161 {
162 // Attach second as leftmost child of first
163 second->prev = first;
164 first->nextSibling = second->nextSibling;
165 if( first->nextSibling != nullptr )
166 first->nextSibling->prev = first;
167 second->nextSibling = first->leftChild;
168 if( second->nextSibling != nullptr )
169 second->nextSibling->prev = second;
170 first->leftChild = second;
171 }
172}
173
179template <class T>
181PairingHeap<T>::combineSiblings( PairNode<T> *firstSibling ) const
182{
183 if( firstSibling->nextSibling == nullptr )
184 return firstSibling;
185
186 vector<PairNode<T>*> treeArray;
187
188 // Store the subtrees in an array
189 size_t numSiblings = 0;
190 for( ; firstSibling != nullptr; numSiblings++ )
191 {
192 treeArray.push_back(firstSibling);
193 firstSibling->prev->nextSibling = nullptr; // break links
194 firstSibling = firstSibling->nextSibling;
195 }
196 treeArray.push_back(nullptr);
197
198 // Combine subtrees two at a time, going left to right
199 size_t i = 0;
200 for( ; i + 1 < numSiblings; i += 2 )
201 compareAndLink( treeArray[ i ], treeArray[ i + 1 ] );
202
203 size_t j = i - 2;
204
205 // j has the result of last compareAndLink.
206 // If an odd number of trees, get the last one.
207 if (j + 3 == numSiblings)
208 compareAndLink( treeArray[ j ], treeArray[ j + 2 ] );
209
210 // Now go right to left, merging last tree with
211 // next to last. The result becomes the new last.
212 for( ; j >= 2; j -= 2 )
213 compareAndLink( treeArray[ j - 2 ], treeArray[ j ] );
214 return treeArray[ 0 ];
215}
216
217template <class T>
218ostream& operator <<(ostream &os, const PairingHeap<T> &b)
219{
220 os<<"Heap:";
221 if (b.root != nullptr) {
222 PairNode<T> *r = b.root;
223 list<PairNode<T>*> q;
224 q.push_back(r);
225 while (!q.empty()) {
226 r = q.front();
227 q.pop_front();
228 if (r->leftChild != nullptr) {
229 os << *r->element << ">";
230 PairNode<T> *c = r->leftChild;
231 while (c != nullptr) {
232 q.push_back(c);
233 os << "," << *c->element;
234 c = c->nextSibling;
235 }
236 os << "|";
237 }
238 }
239 }
240 return os;
241}
242#endif
ostream & operator<<(ostream &os, const PairingHeap< T > &b)
Pairing heap datastructure implementation.
Definition PairingHeap.h:68
const T & findMin() const
PairNode< T > * insert(const T &x)
void deleteMin()
PairingHeap(bool(*lessThan)(T const &lhs, T const &rhs))
bool isEmpty() const
static node_list * newNode(Grid *g, Agnode_t *n, node_list *nxt)
Definition grid.c:138
static mytime_t T
Definition timing.c:41