Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binHeap.H File Reference

Binary heap implementation using tree structure. More...

#include <type_traits>
#include <utility>
#include <ah-concepts.H>
#include <htlist.H>
#include <tpl_arrayStack.H>
#include <tpl_binNode.H>
#include <tpl_dynListQueue.H>
Include dependency graph for tpl_binHeap.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::BinHeapNode_Data
 
struct  Aleph::BinHeapNode_Data::Control_Fields
 
class  Aleph::BinHeapNode< Key >
 
class  Aleph::BinHeapNodeVtl< Key >
 
class  Aleph::GenBinHeap< NodeType, Key, Compare >
 Generic heap of nodes. More...
 
class  Aleph::GenBinHeap< NodeType, Key, Compare >::Iterator
 
struct  Aleph::BinHeap< Key, Compare >
 Node heap without virtual destructor. More...
 
struct  Aleph::BinHeapVtl< Key, Compare >
 Heap of nodes with virtual destructor. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Macros

#define PREV(p)   (p->getL())
 
#define NEXT(p)   (p->getR())
 
#define ULINK(p)   reinterpret_cast<Node*&>((p)->getU())
 
#define IS_LEAF(p)   ((p)->get_control_fields().is_leaf)
 
#define IS_LEFT(p)   ((p)->get_control_fields().is_left)
 
#define CTRL_BITS(p)   ((p)->get_control_fields())
 

Detailed Description

Binary heap implementation using tree structure.

This file provides a binary heap implementation that uses a binary tree structure instead of an array. This allows for dynamic sizing and efficient node-based operations.

A heap on an array finds the parent and the children of an entry with arithmetic on its index, and its last entry at the end of the array. The heap of this file has no array and no indexes: it gets the same from the links of its nodes, in O(1), because it keeps the nodes of the bottom of the tree in a list. The section "Representation" describes how; whoever changes this file must read it first.

Heap Types

  • GenBinHeap: Generic base heap with customizable comparison
  • BinHeap: Min-heap (smallest element at root)
  • BinHeapVtl: Min-heap with virtual destructor for polymorphism

With the reverse comparison, any of them is a max-heap.

Key Features

  • Tree-based structure (not array-based)
  • Parent pointers for efficient operations
  • The place of an insertion and the last node, each in O(1), from a list of the nodes that lack a child
  • Supports iteration via Iterator class
  • The nodes belong to the caller, which allocates and frees them

Complexity

Operation Time
insert O(log n)
top O(1)
getMin/getMax O(log n)
remove O(log n)
update O(log n)

Usage Example

BinHeap<int> heap;
BinHeap<int>::Node a(5), b(3), c(7); // the nodes belong to the caller
heap.insert(&a);
heap.insert(&b);
heap.insert(&c);
while (not heap.is_empty())
{
BinHeap<int>::Node *min = heap.getMin(); // removes the minimum
std::cout << min->get_key() << '\n'; // 3, then 5, then 7
}
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189

Representation

The tree

The tree is complete: all its levels are full except, at most, the last one, whose nodes are at its left. Number the places of the nodes by levels and from left to right, with the root at 1: the place i has its parent at i / 2 and its children at 2 i and 2 i + 1, and a heap of n nodes fills the places 1 to n. These are the indexes of a heap on an array. Here no node knows its own, but the links follow them. The shape of the tree depends only on n: the operations change which node is at each place.

Each node has three links, LLINK, RLINK and ULINK, and two flags: IS_LEAF, it has no children, and IS_LEFT, it is the left child of its parent. ULINK is its parent; that of the root is the header node of the heap, whose right child is the root. The heap keeps the root, the node at n, which is called last, and n.

The list of the nodes that lack a child

A node with two children has them in LLINK and RLINK, as in any binary tree. A node that lacks a child has links to spare, and the heap uses those links to keep all such nodes in a circular doubly linked list, in the order of their places: RLINK is the next node of the list and LLINK the previous one.

Those nodes are the ones at the places f to n, where f = (n + 1) / 2, that is, n / 2 rounded up: all the leaves and, if n is even, the parent of the last node, which has a left child only. The list starts at f and ends at last, which is followed by the node at f again.

If n is even, the first node of the list is the parent of the last one. Its LLINK is then its left child and its previous node of the list at once, with no conflict, since both are last. Its RLINK is its next node of the list, not a child.

1 n = 10, f = 5
/ \
2 3 1 to 4: two children
/ \ / \
4 5 6 7 5: a left child only
/ \ /
8 9 10 6 to 10: leaves
the list: 5 <-> 6 <-> 7 <-> 8 <-> 9 <-> 10 = last
^ ^
+------------------------------+
LLINK(5) = 10: its left child and its previous node of the list
RLINK(5) = 6: its next node of the list

A heap of one node has it as its own next and previous node. In an empty heap the root is null and last is the header node.

What the list is for

The two nodes around last in the list are the ones that an insertion and a removal need:

  • RLINK(last), the node at f, is the parent of the place n + 1, because (n + 1) / 2 = f. An insertion makes the new node its child, the left one if it had none and the right one otherwise, and puts it in the list after last, as the new last node. A parent that gets its second child leaves the list.
  • LLINK(last), the node at n - 1, is the last node once last is out. A removal takes last out of the list, and a parent that loses its second child enters the list as its first node.

So both find their place in O(1): with no array, no search and no path computed from the bits of n. Only the sifts, which exchange a node with its parent level by level, take O(log n).

Reading the links

Whether a link is a child or a neighbour of the list comes from the flags and from the list itself:

  • If IS_LEAF(p), both links of p are of the list.
  • Otherwise LLINK(p) is the left child of p, and RLINK(p) is its right child unless the left one is the only child. A node is an only child when its next node of the list is its own parent, which only happens to last, when n is even: this is has_sibling().

advance_left() and advance_right() give the children of a node by those rules, and null where there is none. Whatever walks the tree must use them: followed blindly, the links never reach a null pointer, they go round the list.

Exchanges

A sift exchanges a node with its parent, swap_with_parent(), and an extraction exchanges the root with the last node, swap_root_with_last(). An exchange gives each of the two nodes the place of the other, in the tree and in the list, flags included, so that the shape and the list stay those of n. That is why swap_with_parent() tells apart whether the nodes are in the list: neither, the child only, or both, which is the case of last under a parent that has no other child.

Since the links depend only on which node is at each place, an exchange is undone by exchanging the same two nodes again, and the linking of a last node by its removal. That is how an operation leaves the heap as it was when the comparison throws: it takes each node back to its place, with exchanges, which compare nothing and do not throw.

When to Use

Use tree-based heap when:

  • Need dynamic resizing without reallocation
  • Working with node pointers directly
  • Need to update priorities of existing nodes

For array-based heaps (more cache-friendly), see:

See also
tpl_arrayHeap.H Array-based heap implementation
tpl_dynBinHeap.H Dynamic heap with key updates
tpl_fibonacci_heap.H Fibonacci heap for advanced priority queues
Author
Leandro Rabindranath León

Definition in file tpl_binHeap.H.

Macro Definition Documentation

◆ CTRL_BITS

#define CTRL_BITS (   p)    ((p)->get_control_fields())

Definition at line 272 of file tpl_binHeap.H.

◆ IS_LEAF

#define IS_LEAF (   p)    ((p)->get_control_fields().is_leaf)

Definition at line 270 of file tpl_binHeap.H.

◆ IS_LEFT

#define IS_LEFT (   p)    ((p)->get_control_fields().is_left)

Definition at line 271 of file tpl_binHeap.H.

◆ NEXT

#define NEXT (   p)    (p->getR())

Definition at line 268 of file tpl_binHeap.H.

◆ PREV

#define PREV (   p)    (p->getL())

Definition at line 267 of file tpl_binHeap.H.

◆ ULINK

#define ULINK (   p)    reinterpret_cast<Node*&>((p)->getU())

Definition at line 269 of file tpl_binHeap.H.