/**
* @file
* Pairing heap datastructure implementation.
*
* Based on example code in "Data structures and Algorithm Analysis in C++"
* by Mark Allen Weiss, used and released under the LGPL by permission
* of the author.
*
* No promises about correctness. Use at your own risk!
*/
/*
* Authors:
* Mark Allen Weiss
* Tim Dwyer <tgdwyer@gmail.com>
*
* Copyright (C) 2005 Authors
*
* Released under GNU LGPL. Read the file 'COPYING' for more information.
*/
#include <vector>
#include <list>
#include "dsexceptions.h"
#include "PairingHeap.h"
#ifndef PAIRING_HEAP_CPP
#define PAIRING_HEAP_CPP
using namespace std;
/**
* Construct the pairing heap.
*/
template <class T>
{
counter=0;
}
/**
* Copy constructor
*/
template <class T>
{
*this = rhs;
}
/**
* Destroy the leftist heap.
*/
template <class T>
PairingHeap<T>::~PairingHeap( )
{
makeEmpty( );
}
/**
* Insert item x into the priority queue, maintaining heap order.
* Return a pointer to the node containing the new item.
*/
template <class T>
PairNode<T> *
{
else
counter++;
return newNode;
}
template <class T>
return counter;
}
/**
* Find the smallest item in the priority queue.
* Return the smallest item, or throw Underflow if empty.
*/
template <class T>
{
if( isEmpty( ) )
throw Underflow( );
}
/**
* Remove the smallest item from the priority queue.
* Throws Underflow if empty.
*/
template <class T>
{
if( isEmpty( ) )
throw Underflow( );
else
counter--;
delete oldRoot;
}
/**
* Test if the priority queue is logically empty.
* Returns true if empty, false otherwise.
*/
template <class T>
{
}
/**
* Test if the priority queue is logically full.
* Returns false in this implementation.
*/
template <class T>
{
return false;
}
/**
* Make the priority queue logically empty.
*/
template <class T>
{
reclaimMemory( root );
}
/**
* Deep copy.
*/
template <class T>
const PairingHeap<T> &
{
if( this != &rhs )
{
makeEmpty( );
}
return *this;
}
/**
* Internal method to make the tree empty.
* WARNING: This is prone to running out of stack space.
*/
template <class T>
{
if( t != NULL )
{
reclaimMemory( t->leftChild );
reclaimMemory( t->nextSibling );
delete t;
}
}
/**
* Change the value of the item stored in the pairing heap.
* Does nothing if newVal is larger than currently stored value.
* p points to a node returned by insert.
* newVal is the new value, which must be smaller
* than the currently stored value.
*/
template <class T>
const T & newVal )
{
return; // newVal cannot be bigger
if( p != root )
{
if( p->nextSibling != NULL )
else
p->nextSibling = NULL;
compareAndLink( root, p );
}
}
/**
* Internal method that is the basic operation to maintain order.
* Links first and second together to satisfy heap order.
* first is root of tree 1, which may not be NULL.
* first->nextSibling MUST be NULL on entry.
* second is root of tree 2, which may be NULL.
* first becomes the result of the tree merge.
*/
template <class T>
void PairingHeap<T>::
{
return;
{
// Attach first as leftmost child of second
}
else
{
// Attach second as leftmost child of first
}
}
/**
* Internal method that implements two-pass merging.
* firstSibling the root of the conglomerate;
* assumed not NULL.
*/
template <class T>
PairNode<T> *
{
return firstSibling;
// Allocate the array
// Store the subtrees in an array
int numSiblings = 0;
{
}
// Combine subtrees two at a time, going left to right
int i = 0;
int j = i - 2;
// j has the result of last compareAndLink.
// If an odd number of trees, get the last one.
if( j == numSiblings - 3 )
// Now go right to left, merging last tree with
// next to last. The result becomes the new last.
for( ; j >= 2; j -= 2 )
return treeArray[ 0 ];
}
/**
* Internal method to clone subtree.
* WARNING: This is prone to running out of stack space.
*/
template <class T>
PairNode<T> *
{
if( t == NULL )
return NULL;
else
{
p->nextSibling->prev = p;
return p;
}
}
template <class T>
{
os<<"Heap:";
q.push_back(r);
while (!q.empty()) {
r = q.front();
q.pop_front();
while (c != NULL) {
q.push_back(c);
c = c->nextSibling;
}
os << "|";
}
}
}
return os;
}
#endif