30#ifndef PAIRING_HEAP_CPP
31#define PAIRING_HEAP_CPP
40 this->lessThan=lessThan;
66 compareAndLink( root,
newNode );
92 if( root->leftChild ==
nullptr )
95 root = combineSiblings( root->leftChild );
106 return root ==
nullptr;
115 reclaimMemory( root );
128 reclaimMemory( t->leftChild );
129 reclaimMemory( t->nextSibling );
147 if( second ==
nullptr )
149 if( lessThan(second->element,first->element) )
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;
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;
183 if( firstSibling->nextSibling ==
nullptr )
186 vector<PairNode<T>*> treeArray;
189 size_t numSiblings = 0;
190 for( ; firstSibling !=
nullptr; numSiblings++ )
192 treeArray.push_back(firstSibling);
193 firstSibling->prev->nextSibling =
nullptr;
194 firstSibling = firstSibling->nextSibling;
196 treeArray.push_back(
nullptr);
200 for( ; i + 1 < numSiblings; i += 2 )
201 compareAndLink( treeArray[ i ], treeArray[ i + 1 ] );
207 if (j + 3 == numSiblings)
208 compareAndLink( treeArray[ j ], treeArray[ j + 2 ] );
212 for( ; j >= 2; j -= 2 )
213 compareAndLink( treeArray[ j - 2 ], treeArray[ j ] );
214 return treeArray[ 0 ];
221 if (b.root !=
nullptr) {
223 list<PairNode<T>*> q;
228 if (r->leftChild !=
nullptr) {
229 os << *r->element <<
">";
231 while (c !=
nullptr) {
233 os <<
"," << *c->element;
ostream & operator<<(ostream &os, const PairingHeap< T > &b)
Pairing heap datastructure implementation.
const T & findMin() const
PairNode< T > * insert(const T &x)
PairingHeap(bool(*lessThan)(T const &lhs, T const &rhs))
static node_list * newNode(Grid *g, Agnode_t *n, node_list *nxt)