2
* \brief Pairing heap datastructure implementation
4
* Based on example code in "Data structures and Algorithm Analysis in C++"
5
* by Mark Allen Weiss, used and released under the LGPL by permission
8
* No promises about correctness. Use at your own risk!
12
* Tim Dwyer <tgdwyer@gmail.com>
14
* Copyright (C) 2005 Authors
16
* Released under GNU LGPL. Read the file 'COPYING' for more information.
18
#ifndef PAIRING_HEAP_H_
19
#define PAIRING_HEAP_H_
24
// CONSTRUCTION: with no parameters
26
// ******************PUBLIC OPERATIONS*********************
27
// PairNode & insert( x ) --> Insert x
28
// deleteMin( minItem ) --> Remove (and optionally return) smallest item
29
// T findMin( ) --> Return smallest item
30
// bool isEmpty( ) --> Return true if empty; else false
31
// bool isFull( ) --> Return true if empty; else false
32
// void makeEmpty( ) --> Remove all items
33
// void decreaseKey( PairNode p, newVal )
34
// --> Decrease value in node p
35
// ******************ERRORS********************************
36
// Throws Underflow as warranted
39
// Node and forward declaration because g++ does
40
// not understand nested classes.
45
std::ostream& operator<< (std::ostream &os,const PairingHeap<T> &b);
50
friend std::ostream& operator<< <T>(std::ostream &os,const PairingHeap<T> &b);
53
PairNode *nextSibling;
56
PairNode( const T & theElement ) :
57
element( theElement ),
58
leftChild(NULL), nextSibling(NULL), prev(NULL)
60
friend class PairingHeap<T>;
67
virtual bool isLessThan(T const &lhs, T const &rhs) const = 0;
73
friend std::ostream& operator<< <T>(std::ostream &os,const PairingHeap<T> &b);
75
PairingHeap( bool (*lessThan)(T const &lhs, T const &rhs) );
76
PairingHeap( const PairingHeap & rhs );
77
virtual ~PairingHeap( );
79
bool isEmpty( ) const;
83
PairNode<T> *insert( const T & x );
84
const T & findMin( ) const;
86
const T extractMin( ) {
92
void decreaseKey( PairNode<T> *p, const T & newVal );
93
void merge( PairingHeap<T> *rhs )
95
PairNode<T> *broot=rhs->getRoot();
101
compareAndLink(root, broot);
103
counter+=rhs->size();
106
const PairingHeap & operator=( const PairingHeap & rhs );
108
PairNode<T> * getRoot() {
115
bool (*lessThan)(T const &lhs, T const &rhs);
117
void reclaimMemory( PairNode<T> *t ) const;
118
void compareAndLink( PairNode<T> * & first, PairNode<T> *second ) const;
119
PairNode<T> * combineSiblings( PairNode<T> *firstSibling ) const;
120
PairNode<T> * clone( PairNode<T> * t ) const;
123
#include "PairingHeap.cpp"