15 211 Fundamental Structures of Computer Science Binomial
15 -211 Fundamental Structures of Computer Science Binomial Heaps March 02, 2006 Ananda Guna
In this Lecture z Binomial Trees y Definition y properties z Binomial Heaps y efficient merging z Implementation z Operations z About Midterm
Binary Heaps z Binary heap is a data structure that allows y insert in O(log n) y delete. Min in O(log n) y find. Min in O(1) z How about merging two heaps y complexity is O(n) z So we discuss a data structure that allows merge in O(log n)
Applications of Heaps z Binary Heaps y efficient find. Min, delete. Min y many applications z Binomial Heaps y Efficient merge of two heaps y Merging two heap based data structures z Binomial Heap is build using a structure called Binomial Trees
Binomial Trees z A Binomial Tree Bk of order k is defined as follows y B 0 is a tree with one node y Bk is a pair of Bk-1 trees, where root of one Bk-1 becomes the left most child of the other (for all k ≥ 1) B 0 B 1 B B 2 3
Merging two binomial trees z Merging two equal binomial trees of order j + = New tree has order j + 1
Properties of Binomial trees z. The following properties hold for a binomial tree of order k y Bk has 2 k nodes y The height of Bk is k y Bk has k. Ci nodes at level i for i = 0, 1, …k y The root of Bk has k-children B 0, B 1, …Bk-1 (in that order) where the ith child is a binomial tree of order i. y If binomial tree of order k has n nodes, then k ≤ log n
Proofs Lemma 1: BK has 2 k nodes Proof: (by induction). True for k=0, assume true for k=r. Consider Br+1 has 2 r + 2 r = 2 r+1 nodes Lemma 2: Bk has height k Proof: homework Lemma 3: Bk has k. Ci nodes at level i for i = 0, 1, …k Proof: Let T(k, i) be the number of nodes at depth i. Then T(k, i) = T(k-1, i) + T(k-1, i-1) = k-1 Ci + k-1 Ci-1 = k. Ci
Binomial Heap z Binomial Heap is a collection of binomial trees that satisfies the following properties y No two binomial trees in the collection have the same size y Each node in the collection has a key y Each binomial tree in the collection satisfies the heap order property y Roots of the binomial trees are connected and are in increasing order
Example z A binomial heap of n=15 nodes containing B 0, B 1, B 2 and B 3 binomial trees z What is the connection between n and number of binary trees in the heap?
Lemma Given any integer n, there exists a binomial heap that contain n nodes Proof:
implementation
Implementation – Binary Tree Node z Fields in a binomial tree node y y y Key number of children (or degree) Left most child Right most sibling A pointer P to parent
Implementation – Binary Heap head root 1 root 2 root 3 root 4
Operations
Operations on Binomial Heaps z Merge is the key operation on binomial heaps y merge() y insert() y find. Min() x find the min of all children O(log n) y delete. Root() y delete. Node() y decrease. Key()
Merging two binomial heaps z Suppose H 1 and H 2 are two binomial heaps z Merge H 1 and H 2 into a new heap H z Algorithm: y Let A and B be pointers to H 1 and H 2 y for all orders i x If there is one order i tree, merge it to H x If there are two order i trees, merge them into a new tree of order i+1 and store them in a temp tree T x If there are three order i trees in H 1, H 2 and T, merge two of them, store as T and add the remainder to H
Example
Binary Heap Operations z Insert y make a new heap H 0 with the new node y Merge(H 0, H) z Find. Min y min is one of the children connected to the root x cost is O(log n)
Binary Heap Operations z Delete. Root() y Find the tree with the given root y Split the heap into two heaps H 1 and H 2
Binary Heap Operations z Delete. Root() ctd. . y Rearrange binomial trees in heap H 2 y Merge the two heaps v v
Example
Delete. Node() z To delete a node, decrease its key to -∞, percolate up to root, then delete the root z Decrease. Key(): Decrease the key and percolate up until heap order property is satisfied
Summary z Two heaps y Binary Heaps x delete. Min() y Binomial Heaps x merge. Heaps() z Next Week y Midterm on Tuesday y Strings and Tries on thursday
- Slides: 24