Graphviz 12.1.0~dev.20240716.0947
Loading...
Searching...
No Matches
PairingHeap.h
Go to the documentation of this file.
1
24#ifndef PAIRING_HEAP_H_
25#define PAIRING_HEAP_H_
26#include <stdlib.h>
27#include <fstream>
28// Pairing heap class
29//
30// CONSTRUCTION: with no parameters
31//
32// ******************PUBLIC OPERATIONS*********************
33// PairNode & insert( x ) --> Insert x
34// deleteMin( minItem ) --> Remove (and optionally return) smallest item
35// T findMin( ) --> Return smallest item
36// bool isEmpty( ) --> Return true if empty; else false
37// void makeEmpty( ) --> Remove all items
38// ******************ERRORS********************************
39// Throws Underflow as warranted
40
41
42// Node and forward declaration because g++ does
43// not understand nested classes.
44template <class T>
45class PairingHeap;
46
47template <class T>
48std::ostream& operator<< (std::ostream &os,const PairingHeap<T> &b);
49
50template <class T>
52{
53 friend std::ostream& operator<< <T>(std::ostream &os,const PairingHeap<T> &b);
54 T element;
55 PairNode *leftChild;
56 PairNode *nextSibling;
57 PairNode *prev;
58
59 PairNode( const T & theElement ) :
60 element( theElement ),
61 leftChild(nullptr), nextSibling(nullptr), prev(nullptr)
62 { }
63 friend class PairingHeap<T>;
64};
65
66template <class T>
68{
69 friend std::ostream& operator<< <T>(std::ostream &os,const PairingHeap<T> &b);
70public:
71 PairingHeap( bool (*lessThan)(T const &lhs, T const &rhs) );
72 ~PairingHeap( );
73
74 bool isEmpty( ) const;
75
76 PairNode<T> *insert( const T & x );
77 const T & findMin( ) const;
78 void deleteMin( );
79 void makeEmpty( );
80 void merge( PairingHeap<T> *rhs )
81 {
82 PairNode<T> *broot=rhs->getRoot();
83 if (root == nullptr) {
84 if(broot != nullptr) {
85 root = broot;
86 }
87 } else {
88 compareAndLink(root, broot);
89 }
90 }
91
92 PairingHeap(const PairingHeap &rhs) = delete;
93 PairingHeap(PairingHeap &&rhs) = delete;
94 PairingHeap &operator=(const PairingHeap &rhs) = delete;
96protected:
98 PairNode<T> *r=root;
99 root=nullptr;
100 return r;
101 }
102private:
103 PairNode<T> *root;
104 bool (*lessThan)(T const &lhs, T const &rhs);
105 void reclaimMemory( PairNode<T> *t ) const;
106 void compareAndLink( PairNode<T> * & first, PairNode<T> *second ) const;
107 PairNode<T> * combineSiblings( PairNode<T> *firstSibling ) const;
108};
109
110#include "PairingHeap.cpp"
111#endif
std::ostream & operator<<(std::ostream &os, const PairingHeap< T > &b)
Pairing heap datastructure implementation.
Definition PairingHeap.h:68
PairingHeap & operator=(const PairingHeap &rhs)=delete
const T & findMin() const
PairingHeap(PairingHeap &&rhs)=delete
PairNode< T > * insert(const T &x)
void deleteMin()
PairingHeap(const PairingHeap &rhs)=delete
PairNode< T > * getRoot()
Definition PairingHeap.h:97
PairingHeap & operator=(PairingHeap &&rhs)=delete
void merge(PairingHeap< T > *rhs)
Definition PairingHeap.h:80
bool isEmpty() const
$2 u p prev
Definition htmlparse.y:495
static mytime_t T
Definition timing.c:41