Operations on binomial heaps

Algorithm

Algorithm to find minimum
Input	Binomial heap .
Output	pointer of minimum key
Complexity	O(lgn).

BINOMIAL-HEAP-MINIMUM(H)
1      y= NIL
2      x = head[H]
3     min = ∞
4     while x ≠ NIL
5        do  if key[x] < min
6                  then min = key[x]
7                          y =x
8               x = sibling[x]
9 return y

Algorithm for union operation
Input	Two Binomial heap H1 and H2  .
Output	Head pointer of final Binomial heap
Complexity	O(lgN).

BINOMIAL-LINK(y, z)
    1 p[y] =z
    2 sibling[y] = child[z]
    3 child[z] =y
    4 degree[z] = degree[z] + 1


BINOMIAL-HEAP-UNION (H1, H2)
1 H = MAKE-BINOMIAL-HEAP ( )
2 head [H] = BINOMIAL-HEAP-MERGE(H1, H2)
3 free the objects H1 and H2 but not the lists they point to
4 if head[H] = NIL
5 then return H
6 prev-x =NIL
7 x =head[H]
8 next-x = sibling[x]
9 while next-x ≠ NIL
10 do if (degree[x] ≠ degree[next-x]) or
     (sibling[next-x] ≠ NIL and degree[sibling[next-x]] =degree[x])
11 then prev-x = x 
12 x = next-x 
13 else if key[x] ≤ key[next-x]
14 then sibling[x] = sibling[next-x] 
15 BINOMIAL-LINK(next-x, x) 
16 else if prev-x = NIL 
17 then head[H] = next-x 
18 else sibling[prev-x] =next-x 
19 BINOMIAL-LINK(x, next-x) 
20 x =next-x 
21 next-x = sibling[x]
22 return H

Binomial heap insertion
Input	Binomial heap .
Output	pointer of minimum key
Complexity	O(logn).

BINOMIAL-HEAP-INSERT(H, x)
1 H′ = MAKE-BINOMIAL-HEAP()
2 p[x] = NIL
3 child[x] = NIL
4 sibling[x] =NIL
5 degree[x] = 0
6 head[H′] = x
7 H = BINOMIAL-HEAP-UNION(H, H′)

Binomial heap extract min.
Input	Binomial heap .
Output	pointer of the extracted node
Complexity	O(logn).

BINOMIAL-HEAP-EXTRACT-MIN(H)
1 find the root x with the minimum key in the root list of H,
and remove x from the root list of H
2 H′ = MAKE-BINOMIAL-HEAP()
3 reverse the order of the linked list of x's children,
and set head[H′] to point to the head of the resulting list
4 H = BINOMIAL-HEAP-UNION(H, H′)
5 return x;

Binomial heap decrease key.
Input	Binomial heap .
Output	Binomial heap after decrease key
Complexity	O(logn).

BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
1 if k > key[x]
2 then error "new key is greater than current key"
3 key[x] =k
4 y = x
5 z = p[y]
6 while z ≠ NIL and key[y] < key[z]
7 do exchange key[y] = key[z]
8  If y and z have satellite fields, exchange them, too.
9 y =z
10 z = p[y]

Binomial heap delete key.
Input	Binomial heap .
Complexity	O(logn).

BINOMIAL-HEAP-DELETE(H, x)
        1 BINOMIAL-HEAP-DECREASE-KEY(H, x, -∞)
         2 BINOMIAL-HEAP-EXTRACT-MIN(H)

Algorithm Description

Algorithm to find minimum

  1. The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node binomial heap H.
  2. Since a binomial heap is min-heap-ordered, the minimum key must reside in a root node. The BINOMIAL-HEAP-MINIMUM procedure checks all roots, which number at most⌊lg n⌋ + 1,saving the current minimum in min and a pointer to the current minimum in y.

Algorithm for union operation

  1. The BINOMIAL-LINK procedure makes node the new head of the linked list of node z's children in O(1) time.
  2. BINOMIAL-HEAP-UNION () function unites binomial heaps H1 and H2, returning the resulting heap.
  3. 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.
  4. Initially, is the first root on the root list of .
  5. The BINOMIAL-HEAP-UNION procedure has two phases. The first phase, performed by the call of BINOMIAL-HEAP-MERGE, merges the root lists of binomial heaps H1 and H2 into a single linked list that is sorted by degree into monotonically increasing order. There might be as many as two roots (but no more) of each degree, however, so the second phase links roots of equal degree until at most one root remains of each degree. Because the linked list is sorted by degree, we can perform all the link operations quickly.

Binomail heap insertion

  1. The following procedure inserts node into binomial heap , assuming that has already been allocated and key[x] has already been filled in.
  2. The procedure simply makes a one-node binomial heap H? in O(1) time and unites it with the n-node binomial heap in O(lg n) time. The call to BINOMIAL-HEAP-UNION takes care of freeing the temporary binomial heap H?

Binomial heap extract min

  1. The following procedure extracts the node with the minimum key from binomial heap H and returns a pointer to the extracted node.

Binomial heap decrease key

  1. The following procedure decreases the key of a node in a binomial heap to a new value k.It signals an error if is greater than x's current key.

Binomial heap delete key

  1. The BINOMIAL-HEAP-DELETE procedure makes node have the unique minimum key in the entire binomial heap by giving it a key of-∞.
  2. It then bubbles this key and the associated satellite information up to a root by calling BINOMIAL-HEAP-DECREASEKEY.This root is then removed from by a call of BINOMIAL-HEAP-EXTRACT-MIN.