58 rank(0), parent(nullptr), next(nullptr), child(nullptr)
74 template<
typename T,
typename C = std::less<T>>
88 explicit BinomialHeap(
const C &cmp = C(),
int initialSize = -1);
99 const T &top()
const override;
107 BinomialHeapNode<T> *push(
const T &
value)
override;
125 void decrease(BinomialHeapNode<T> *heapNode,
const T &
value)
override;
142 const T &
value(BinomialHeapNode<T> *heapNode)
const override {
143 return heapNode->
value;
150 BinomialHeapNode<T> *join(BinomialHeapNode<T> *a, BinomialHeapNode<T> *b);
152 void merge(BinomialHeapNode<T> *other);
155 static void link(BinomialHeapNode<T> *
parent, BinomialHeapNode<T> *
child);
158 static void release(BinomialHeapNode<T> *heapNode);
162 template<
typename T,
typename C>
167 template<
typename T,
typename C>
175 template<
typename T,
typename C>
178 while(heapNode !=
nullptr) {
181 BinomialHeapNode<T> *next = heapNode->
next;
188 template<
typename T,
typename C>
191 BinomialHeapNode<T> *min =
m_root;
192 for(BinomialHeapNode<T> *it =
m_root->next; it !=
nullptr; it = it->next) {
202 template<
typename T,
typename C>
205 BinomialHeapNode<T> *heapNode =
new BinomialHeapNode<T>(
value);
212 template<
typename T,
typename C>
215 BinomialHeapNode<T> *curr =
m_root, *min =
m_root, *minPrev =
nullptr;
217 while(curr->
next !=
nullptr) {
228 minPrev->next = min->next;
232 BinomialHeapNode<T> *reversed =
nullptr, *child = min->child;
233 while(child !=
nullptr) {
234 BinomialHeapNode<T> *next = child->next;
235 child->next = reversed;
244 template<
typename T,
typename C>
252 while(heapNode->
parent !=
nullptr &&
253 this->comparator()(heapNode->
value, heapNode->
parent->value))
255 std::swap(heapNode->
value, heapNode->
parent->value);
256 heapNode = heapNode->
parent;
261 template<
typename T,
typename C>
269 template<
typename T,
typename C>
271 BinomialHeapNode<T> *a, BinomialHeapNode<T> *b)
284 BinomialHeapNode<T> *head = a;
285 while(b !=
nullptr) {
286 if(a->
next ==
nullptr) {
292 BinomialHeapNode<T> *nextB = b->
next;
307 template<
typename T,
typename C>
315 BinomialHeapNode<T> *prev =
nullptr, *curr =
m_root, *next =
m_root->next;
316 while(next !=
nullptr) {
317 if(curr->rank != next->rank || (next->next !=
nullptr &&
318 next->next->rank == curr->rank))
326 if(this->
comparator()(curr->value, next->value)) {
327 curr->next = next->next;
329 }
else if(prev ==
nullptr) {
343 template<
typename T,
typename C>
345 BinomialHeapNode<T> *parent, BinomialHeapNode<T> *child)
349 parent->
child = child;
BinomialHeapNode< T > * m_root
Root node of the heap.
void pop() override
Removes the top element from the heap.
static void link(BinomialHeapNode< T > *parent, BinomialHeapNode< T > *child)
Makes child node a child of parent node.
BinomialHeapNode< T > * join(BinomialHeapNode< T > *a, BinomialHeapNode< T > *b)
Joins heap lists a and b into single list sorted by the ranks.
BinomialHeapNode(const T &nodeValue)
Creates heap node with a given nodeValue.
T value
Value contained in the node.
Interface for heap implementations.
BinomialHeap(const C &cmp=C(), int initialSize=-1)
Creates empty binomial heap.
virtual ~BinomialHeap()
Destructs the heap.
const T & top() const override
Returns reference to the top element in the heap.
BinomialHeapNode< T > * parent
Parent of the node.
virtual const C & comparator() const
Returns the comparator used to sort the values in the heap.
BinomialHeapNode< T > * next
Next sibling of the node.
BinomialHeapNode< T > * child
First child of the node.
BinomialHeapNode< T > * push(const T &value) override
Inserts a new node with given value into a heap.
Binomial heap implementation.
size_t rank
Determines rank of a node.
Common interface for all heap classes.
friend class BinomialHeap
static void release(BinomialHeapNode< T > *heapNode)
Releases memory occupied by list of heaps given as heapNode.
The namespace for all OGDF objects.
void decrease(BinomialHeapNode< T > *heapNode, const T &value) override
Decreases value of the given heapNode to value.
const T & value(BinomialHeapNode< T > *heapNode) const override
Returns the value of the node.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
void merge(BinomialHeap< T, C > &other) override
Merges in values of other heap.