This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Downheap
After replacing the root key with the key k of the last node, the
heaporder property may be violated
Algorithm downheap restores the heaporder property by
swapping key k along a downward path from the root
Note that there are, in general, many possible downward paths –
which one do we choose? ?
7
5 ?
6 w 9 CSE 2011
Prof. J. Elder  61  Last Updated: 1/7/10 10:14 AM Downheap
We select the downward path through the minimumkey nodes.
Downheap terminates when key k reaches a leaf or a node whose
children have keys greater than or equal to k
Since a heap has height O(log n), downheap runs in O(log n) time 7
5 w 5
6 7 9 w 6 9 CSE 2011
Prof. J. Elder  62  Last Updated: 1/7/10 10:14 AM Arraybased Heap Implementation
We can represent a heap with n keys
by means of an array of length n + 1
Links between nodes are not explicitly
stored 2 The cell at rank 0 is not used 5 6 The root is stored at rank 1.
9 For the node at rank i 7 the left child is at rank 2i
the right child is at rank 2i + 1
the parent is at rank floor(i/2) 2 if 2i + 1 > n, the node has no right child
if 2i > n, the node is a leaf CSE 2011
Prof. J. Elder  63  0 1 5
2 6
3 9
4 7
5 Last Updated: 1/7/10 10:14 AM Bottomup Heap Construction We can construct a heap
storing n keys using a
bottomup construction with
log n phases 2i 1 2i 1 In phase i, pairs of heaps
with 2i 1 keys are merged
into heaps with 2i+1 1 keys
2i+1 1 CSE 2011
Prof. J. Elder  64  Last Updated: 1/7/10 10:14 AM Adaptable Priority
Queues 3a 5g CSE 2011
Prof. J. Elder  65  4e Last Updated: 1/7/10 10:14 AM ...
View Full
Document
 Fall '11
 Elder
 Data Structures

Click to edit the document details