Midterm Review2.3

Midterm Review2.3 - Downheap After replacing the root key...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Downheap After replacing the root key with the key k of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order 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 minimum-key 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 Array-based 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 Bottom-up Heap Construction We can construct a heap storing n keys using a bottom-up 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

Page1 / 5

Midterm Review2.3 - Downheap After replacing the root key...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online