Loading...
Searching...
No Matches
BinomialHeap< PR, IM, CMP > Class Template Reference
Data Structures » Heap Structures

## Detailed Description

template<typename PR, typename IM, typename CMP>
class lemon::BinomialHeap< PR, IM, CMP >

This class implements the binomial heap data structure. It fully conforms to the heap concept.

The methods increase() and erase() are not efficient in a binomial heap. In case of many calls of these operations, it is better to use other heap structure, e.g. binary heap.

Template Parameters

  PR | Type of the priorities of the items.
  IM | A read-writable item map with `int` values, used internally to handle the cross references.
  CMP | A functor class for comparing the priorities. The default is `std::less<PR>`.

`#include <lemon/binomial_heap.h>`

## Public Types

enum | State { IN_HEAP = 0 , PRE_HEAP = -1 , POST_HEAP = -2 }
| Type to represent the states of the items. More...
typedef IM | ItemIntMap
| Type of the item-int map.
typedef PR | Prio
| Type of the priorities.
typedef ItemIntMap::Key | Item
| Type of the items stored in the heap.
typedef CMP | Compare
| Functor type for comparing the priorities.

## Public Member Functions

| BinomialHeap (ItemIntMap &map)
| Constructor.
| BinomialHeap (ItemIntMap &map, const Compare &comp)
| Constructor.
int | size () const
| The number of items stored in the heap.
bool | empty () const
| Check if the heap is empty.
void | clear ()
| Make the heap empty.
void | set (const Item &item, const Prio &value)
| Set the priority of an item or insert it, if it is not stored in the heap.
void | push (const Item &item, const Prio &value)
| Insert an item into the heap with the given priority.
Item | top () const
| Return the item having minimum priority.
Prio | prio () const
| The minimum priority.
const Prio & | operator[] (const Item &item) const
| The priority of the given item.
void | pop ()
| Remove the item having minimum priority.
void | erase (const Item &item)
| Remove the given item from the heap.
void | decrease (Item item, const Prio &value)
| Decrease the priority of an item to the given value.
void | increase (Item item, const Prio &value)
| Increase the priority of an item to the given value.
State | state (const Item &item) const
| Return the state of an item.
void | state (const Item &i, State st)
| Set the state of an item in the heap.

## Member Enumeration Documentation

## State

template<typename PR, typename IM, typename CMP>

enum State

Each item has a state associated to it. It can be "in heap", "pre-heap" or "post-heap". The latter two are indifferent from the heap's point of view, but may be useful to the user.

The item-int map must be initialized in such way that it assigns `PRE_HEAP` (-1) to any element to be put in the heap.

Enumerator
IN_HEAP | = 0.
PRE_HEAP | = -1.
POST_HEAP | = -2.

## Constructor & Destructor Documentation

## BinomialHeap() [1/2]

template<typename PR, typename IM, typename CMP>

BinomialHeap | ( | ItemIntMap & | map | ) |

inline explicit

Constructor.

Parameters

  map | A map that assigns `int` values to the items. It is used internally to handle the cross references. The assigned value must be `PRE_HEAP` (-1) for each item.

## BinomialHeap() [2/2]

template<typename PR, typename IM, typename CMP>

BinomialHeap | ( | ItemIntMap & | map,
|  | const Compare & | comp )

inline

Constructor.

Parameters

  map | A map that assigns `int` values to the items. It is used internally to handle the cross references. The assigned value must be `PRE_HEAP` (-1) for each item.
  comp | The function object used for comparing the priorities.

## Member Function Documentation

## size()

template<typename PR, typename IM, typename CMP>

int size | ( |  | ) | const

inline

This function returns the number of items stored in the heap.

## empty()

template<typename PR, typename IM, typename CMP>

bool empty | ( |  | ) | const

inline

This function returns `true` if the heap is empty.

## clear()

template<typename PR, typename IM, typename CMP>

void clear | ( |  | ) |

inline

This functon makes the heap empty. It does not change the cross reference map. If you want to reuse a heap that is not surely empty, you should first clear it and then you should set the cross reference map to `PRE_HEAP` for each item.

## set()

template<typename PR, typename IM, typename CMP>

void set | ( | const Item & | item,
|  | const Prio & | value )

inline

This method sets the priority of the given item if it is already stored in the heap. Otherwise it inserts the given item into the heap with the given priority.

Parameters

  item | The item.
  value | The priority.

## push()

template<typename PR, typename IM, typename CMP>

void push | ( | const Item & | item,
|  | const Prio & | value )

inline

This function inserts the given item into the heap with the given priority.

Parameters

  item | The item to insert.
  value | The priority of the item.

Precondition
  item must not be stored in the heap.

## top()

template<typename PR, typename IM, typename CMP>

Item top | ( |  | ) | const

inline

This function returns the item having minimum priority.

Precondition
  The heap must be non-empty.

## prio()

template<typename PR, typename IM, typename CMP>

Prio prio | ( |  | ) | const

inline

This function returns the minimum priority.

Precondition
  The heap must be non-empty.

## operator[]()

template<typename PR, typename IM, typename CMP>

const Prio & operator[] | ( | const Item & | item | ) | const

inline

This function returns the priority of the given item.

Parameters

  item | The item.

Precondition
  item must be in the heap.

## pop()

template<typename PR, typename IM, typename CMP>

void pop | ( |  | ) |

inline

This function removes the item having minimum priority.

Precondition
  The heap must be non-empty.

## erase()

template<typename PR, typename IM, typename CMP>

void erase | ( | const Item & | item | ) |

inline

This function removes the given item from the heap if it is already stored.

Parameters

  item | The item to delete.

Precondition
  item must be in the heap.

## decrease()

template<typename PR, typename IM, typename CMP>

void decrease | ( | Item | item,
|  | const Prio & | value )

inline

This function decreases the priority of an item to the given value.

Parameters

  item | The item.
  value | The priority.

Precondition
  item must be stored in the heap with priority at least value.

## increase()

template<typename PR, typename IM, typename CMP>

void increase | ( | Item | item,
|  | const Prio & | value )

inline

This function increases the priority of an item to the given value.

Parameters

  item | The item.
  value | The priority.

Precondition
  item must be stored in the heap with priority at most value.

## state() [1/2]

template<typename PR, typename IM, typename CMP>

State state | ( | const Item & | item | ) | const

inline

This method returns `PRE_HEAP` if the given item has never been in the heap, `IN_HEAP` if it is in the heap at the moment, and `POST_HEAP` otherwise. In the latter case it is possible that the item will get back to the heap again.

Parameters

  item | The item.

## state() [2/2]

template<typename PR, typename IM, typename CMP>

void state | ( | const Item & | i,
|  | State | st )

inline

This function sets the state of the given item in the heap. It can be used to manually clear the heap when it is important to achive better time complexity.

Parameters

  i | The item.
  st | The state. It should not be `IN_HEAP`.