|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
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>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()) |
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.
With the reverse comparison, any of them is a max-heap.
| Operation | Time |
|---|---|
| insert | O(log n) |
| top | O(1) |
| getMin/getMax | O(log n) |
| remove | O(log n) |
| update | O(log n) |
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.
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.
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.
The two nodes around last in the list are the ones that an insertion and a removal need:
last, as the new last node. A parent that gets its second child leaves the list.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).
Whether a link is a child or a neighbour of the list comes from the flags and from the list itself:
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.
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.
Use tree-based heap when:
For array-based heaps (more cache-friendly), see:
Definition in file tpl_binHeap.H.
| #define CTRL_BITS | ( | p | ) | ((p)->get_control_fields()) |
Definition at line 272 of file tpl_binHeap.H.
| #define IS_LEAF | ( | p | ) | ((p)->get_control_fields().is_leaf) |
Definition at line 270 of file tpl_binHeap.H.
| #define IS_LEFT | ( | p | ) | ((p)->get_control_fields().is_left) |
Definition at line 271 of file tpl_binHeap.H.
| #define NEXT | ( | p | ) | (p->getR()) |
Definition at line 268 of file tpl_binHeap.H.
| #define PREV | ( | p | ) | (p->getL()) |
Definition at line 267 of file tpl_binHeap.H.
| #define ULINK | ( | p | ) | reinterpret_cast<Node*&>((p)->getU()) |
Definition at line 269 of file tpl_binHeap.H.