Mergeable heaps
- MAKE-HEAP() creates and returns a new heap containing no elements.
- INSERT(H, x) inserts node x, whose key field has already been filled in, into heap H.
- MINIMUM(H) returns a pointer to the node in heap H whose key is minimum.
- EXTRACT-MIN(H) deletes the node from heap H whose key is minimum, returning a pointer to the node.
- UNION(H1, H2) creates and returns a new heap that contains all the nodes of heaps H1 and H2. Heaps H1 and H2 are "destroyed" by this operation.
- DECREASE-KEY(H, x, k) assigns to node x within heap H the new key value k, which is assumed to be no greater than its current key value.
- DELETE(H, x) deletes node x from heap H.
Procedure | Binary heap (worst-case) | Binomial heap (worst-case) | Fibonacci heap (amortized) |
---|---|---|---|
| |||
MAKE-HEAP | Θ(1) | Θ(1) | Θ(1) |
INSERT | Θ(lg n) | O(lg n) | Θ(1) |
MINIMUM | Θ(1) | O(lg n) | Θ(1) |
EXTRACT-MIN | Θ(lg n) | Θ(lg n) | O(lg n) |
UNION | Θ(n) | O(lg n) | Θ(1) |
DECREASE-KEY | Θ(lg n) | Θ(lg n) | Θ(1) |
DELETE | Θ(lg n) | Θ(lg n) | O(lg n) |
Figure 19.1: Running times for operations on three implementations of mergeable heaps. The number of items in the heap(s) at the time of an operation is denoted by n.
1. Binomial trees and binomial heaps
- binomial heap is a collection of binomial trees
- binomial tree Bk is an ordered tree defined recursively.
Lemma 19.1 properties of binomial tree Bk
- there are 2k nodes,(k=lgn)*
- the height of the tree is k,
- there are exactly (ik)nodes at depth i for i = 0, 1, ..., k, and
- the root has degree k, which is greater than that of any other node; moreover if i the children of the root are numbered from left to right by k - 1, k - 2, ..., 0, child i is the root of a subtree Bi.
Corollary 19.2
- The maximum degree of any node in an n-node binomial tree is lg n.
Binomial heaps
- min-heap property
- at most one binomial tree in H whose root has degree k(at most floor(lgn)+1)
Show a bniomial heap H with 13 nodes.
13=1101, H consists of B
0,B
2,B
3
Representation binomial heaps
- p[x] to its parent
- child[x] to its leftmost child
- sibling to its right
- root list (degree of root ordered from small to big
- head[H]
2. Operations on binomial heaps
- creating a new binomial heap: 1. make a empty node
- finding the minimum key
- uniting two binomial heaps: repeatedly links binomial trees whose roots have the same degree(keep the order)
- Inserting a node:1. make a new root 2. merge
- Extracting the node with minimum key: 1. find the root x with the minimum key 2. remove x 3. reverse the order of the linked list of x's children. 4. merge
- decreasing a key: same manner as in a binary min-heap by "bubbling up" the key in the heap
- deleting a key