binomial heap
SEARCH
; it can take a while to find a node with a given key. for this reason, operations such as DECREASE-KEY
and DELETE
that refer to a given node require a pointer to that node as part of their input. this requirement poses no problem in many applications.binomial tree
- a binomial tree of order 0 is a single node
- a binomial tree of order
has a root node whose children are roots of binomial trees of orders,
(in this order)
this ensures that the root of each binomial tree contains the smallest key in the tree. it follows that the smallest key in the entire heap is one of the roots.
this property implies that a binomial heap with
theoretical implementation
because no operation requires random access to the root nodes of the binomial trees, the roots of the binomial trees can be stored in a linked list, ordered by increasing order of the tree. because the number of children for each node is variable, it does not work well for each node to have separate links to each of its children, as would be common in a binary tree; instead, it is possible to implement this tree using links from each node to its highest-order child in the tree, and to its sibling of the next smaller order than it. these sibling pointers can be interpreted as the next pointers in a linked list of the children of each node, but with the opposite order from the linked list of roots: from largest to smallest order, rather than vice versa. this representation allows two trees of the same order to be linked together, making a tree of the next larger order, in constant time.
each node x
has a key field and contains pointers parent(x)
to its parent, child(x)
to its leftmost child, and sibling(x)
to the sibling of x immediately to its right. if node x is a root, then parent(x) = nil
. if node x has no children, then child(x) = nil
, and if x is the rightmost child of its parent, then sibling(x) = nil
. each node x also contains the field degree(x)
, which is the number of children of x.
the roots of the binomial trees within a binomial heap are organized in a linked list, which we refer to as the root list. the degrees of the roots strictly increase as we traverse the root list. in an n-node binomial heap the degrees of the roots are a subset of . the sibling field has a different meaning for roots than for nonroots. if x is a root, then
sibling(x)
points to the next root in the root list. (as usual, sibling(x) = nil
if x is the last root in the root list.)
a given binomial heap h
is accessed by the field head(h)
, which is simply a pointer to the first root in the root list of h. if binomial heap H has no elements, then head[h] = nil
.
binomial heap creation
to make an empty binomial heap, the MAKE-BINOMIAL-HEAP
procedure simply allocates and returns an object h
, where head[h] = nil
. the running time is .
binomial heap union
the operation of uniting two binomial heaps is used as a subroutine by most of the remaining operations. merging of binomial trees is done by comparing the keys at the roots of two trees, and the root node with the larger key will become the child of the root with the smaller key. the time complexity for finding a union is .
to perform the union of two binomial heaps, first we merge the heaps into one, such that the trees in the resulting heap would be in monotonically ascending order, then we traverse through the roots of the binomial trees, we consider the following cases where x
denotes the current root:
- if
degree(x)
is not equal todegree(next(x))
, then move the pointer ahead - if
degree(x) = degree(next(x)) = degree(next(next(x)))
then move the pointer ahead - if
degree(x) = degree(next(x))
but not equal todegree(next(next(x)))
, andkey(x) < key(next(x))
then removenext(x)
from root and attach it to x - if
degree(x) = degree(next(x))
but not equal todegree(next(next(x)))
andkey(x) > key(next(x))
then remove x from root and attach it tonext(x)
.
each binomial tree's traversal during merge only involves roots, hence making the time taken at the biggest order
binomial heap insert
inserting a new element to a heap can be done by creating a new heap containing only this element and then merging it with the original heap. because of the merge, a single insertion takes time . however, this can be sped up using a merge procedure that shortcuts the merge after it reaches a point where only one of the merged heaps has trees of larger order. with this speedup, across a series of
consecutive insertions, the total time for the insertions is
. another way of stating this is that (after logarithmic overhead for the first insertion in a sequence) each successive insert has an amortized time of
(i.e. constant time) per insertion.
a variant of the binomial heap, the skew binomial heap, achieves constant worst case insertion time by using forests whose tree sizes are based on the skew binary number system rather than on the binary number system.