Mergeable Heap
Mergeable Heap
Lines 8-9 update the pointer to the minimum node of Fibonacci heap H if necessary.
FIB-HEAP-INSERT makes no attempt to consolidate the trees within the Fibonacci heap. If k consecutive FIB-HEAP-INSERT
operations occur, then k single-node trees are added to the root list.
Lines 1-3 concatenate the root lists of H1 and H2 into a new root list H.
It is also where the delayed work of consolidating trees in the root list finally occurs.
The code assumes for convenience that when a node is removed from a linked list, pointers remaining in the list are updated, but pointers in the
extracted node are left unchanged. It also uses the auxiliary procedure CONSOLIDATE
The binomial tree Bk is an ordered tree defined recursively. (An ordered tree is a rooted tree in which the children of each node
are ordered. That is, if a node has k children, then there is a first child, a second child,..., and a kth child.)
(a) The recursive definition of the binomial tree Bk. Triangles represent rooted subtrees. (b) The binomial trees B0 through B4
Proof:
The proof is by induction on k.
For each property, the basis is the binomial tree B0.
Verifying that each property holds for B0 is trivial.
For the inductive step, we assume that the lemma holds for Bk-1.
1. Binomial tree Bk consists of two copies of Bk-1, and so Bk has 2k-1 + 2k-1 = 2k nodes.
2. Because of the way in which the two copies of Bk-1 are linked to form Bk, the maximum depth of a node in Bk is one greater
than the maximum depth in Bk-1. By the inductive hypothesis, this maximum depth is (k - 1) + 1 = k.
3. Let D(k, i) be the number of nodes at depth i of binomial tree Bk. Since Bk is composed of two copies of Bk-1 linked together, a
node at depth i in Bk-1 appears in Bk once at depth i and once at depth i + 1. In other words, the number of nodes at depth i in Bk
is the number of nodes at depth i in Bk-1 plus the number of nodes at depth i - 1 in Bk-1. Thus,
4. The only node with greater degree in Bk than in Bk-1 is the root, which has one more child than in Bk-1. Since the root of
Bk-1 has degree k - 1, the root of Bk has degree k. Now, by the inductive hypothesis, from left to right, the children of the root
of Bk-1 are roots of Bk-2, Bk-3, ..., B0. When Bk-1 is linked to Bk-1, therefore, the children of the resulting root are roots of
Bk-1, Bk-2, ..., B0.
The maximum degree of any node in an n-node binomial tree is lg n.
The term "binomial tree" comes from property 3, since the terms are the binomial
coefficients
A binomial heap H with 13 nodes.
The binary representation of 13 is 1101,
and H consists of min-heap-ordered
binomial trees B3, B2, and B0, having 8,
4, and 1 nodes respectively, for a total of
13 nodes.
Each node has a key field and any other satellite information required by the application. In addition, each node x contains:
a. pointers p[x] to its parent,
b. child[x] to its leftmost child, and
c. sibling[x] to the sibling of x immediately to its right.
d. field degree[x], which is the number of children of x.
Root-list: The roots of the binomial trees within a binomial heap are organized in a linked list.The degrees of the roots strictly increase as we traverse the
root list.
By the second binomial-heap property, in an n-node binomial heap the degrees of the roots are a subset of {0, 1, ..., ⌊lg n⌋}.
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.)
head[H}: 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
To make an empty binomial heap,
MAKE-BINOMIAL-HEAP procedure simply allocates and
returns an object H , where head[H ] = NIL.
The running time is Θ(1).
The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node binomial heap H. This implementation
assumes that there are no keys with value ∞
Since a binomial heap is min-heap-ordered, the minimum key must reside in a root node.
When called on the binomial heap, BINOMIAL-HEAP-MINIMUM returns a pointer to the node with
key 1 (refer same example shared earlier).
Because there are at most ⌊lg n⌋ + 1 roots to check, the running time of BINOMIAL-HEAP-
MINIMUM is O(lg n).
The operation of uniting two binomial heaps is used as a subroutine by most of the remaining operations.
The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have the same
degree.
The following procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted at node z; that is, it
makes z the parent of y. Node z thus becomes the root of a Bk tree.
Besides BINOMIAL-LINK, the procedure uses an auxiliary procedure BINOMIAL-HEAP-MERGE that merges the root lists of H1 and H2 into a single
linked list that is sorted by degree into monotonically increasing order.
The running time of BINOMIAL-HEAP-UNION is O(lg n), where n is the total number of nodes in binomial heaps H1 and H2.
The procedure simply makes a one-node binomial heap H′ in O(1) time and unites it with the n-node binomial heap H in O(lg n)
time.
The call to BINOMIAL-HEAP-UNION takes care of freeing the temporary binomial heap H′.
The following procedure extracts the node with the minimum key from binomial heap H and returns a pointer to the extracted node.
Since each of lines 1-4 takes O(lg n) time if H has n nodes, BINOMIAL-HEAP-EXTRACT-MIN runs in O(lg n) time
The action of BINOMIAL-HEAP-EXTRACT-MIN
(a) A binomial heap H
(b) The root x with minimum key is removed from the root list of H
(c) The linked list of x's children is reversed, giving another binomial heap H′
(d) The result of uniting H and H'.
The following procedure decreases the key of a node x in a binomial heap H to a new value k. It signals an error if k is greater than x's current key
As shown in Figure 19.8, this procedure decreases a key in the same manner as in a binary min-heap:
by "bubbling up" the key in the heap.
After ensuring that the new key < current key and then assigning the new key to x, the procedure goes up the tree, with y initially pointing to node x.
In each iteration of the while loop of lines 6-10, key[y] is checked against the key of y's parent z.
If y is the root or key[y] ≥ key[z], the binomial tree is now min-heap-ordered.
Otherwise, node y violates min-heap ordering, and so its key is exchanged with the key of its parent z, along with any other satellite information.
The procedure then sets y to z, going up one level in the tree, and continues with the next iteration.
It is easy to delete a node x's key and satellite information from binomial heap H in O(lg n) time. The following implementation assumes that
no node currently in the binomial heap has a key of -∞.
The BINOMIAL-HEAP-DELETE procedure makes node x have the unique minimum key in the entire binomial heap by giving it a key of -∞.
It then bubbles this key and the associated satellite information up to a root by calling BINOMIAL-HEAP-DECREASE-KEY.
This root is then removed from H by a call of BINOMIAL-HEAP-EXTRACT-MIN. The BINOMIAL-HEAP-DELETE procedure takes O(lg n)
time.
Refer the link mentioned below for animated view of the operations over Binomial
Heap
https://www.chrislaux.com/binomialheap.html