Ads Unit-3
Ads Unit-3
Height of a Heap
Theorem: A heap storing n keys has height O(log n)
Proof: (we apply the complete binary tree property)
Let h be the height of a heap storing n keys
Since there are i keys at depth i = , … , h - 1 and at least one key at
depth h, we have n + + 4 + … + h-1 + 1
Thus, n h , i.e., h log n
Can be stored using the array representation (just add at the end of the array)
Heap Example
Binary Heaps
• A binary heap is a binary tree NOT a BST that is:
› Complete: the tree is completely filled except possibly the bottom level, which is
filled from left to right
Satisfies the heap order property
• every node is less than or equal to its children or every node is greater than
or equal to its children
• The root node is always the smallest node or the largest, depending on the
heap order
Heap order property
• A heap provides limited ordering information
• Each path is sorted, but the subtrees are not sorted relative to each other
Structure property
• A binary heap is a complete tree
› All nodes are in use except for possibly the right end of the bottom row
• Root node = A[ ]
Binomial Queues
2. A binomial tree, Bk, of height k is formed by attaching a binomial tree Bk− to the
root of another binomial tree Bk− .
1. Use a k-ary tree to represent each binomial tree – sibling and child pointers
2. Use a Vector to hold references to the root node of each binomial tree
3. Keep a reference to smallest root for past find min (e.g. a Heap on positions).