lec20 - CSE 12 Priority Queues, Heapsort, and Resizable...

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

View Full Document Right Arrow Icon
20 Heaps and heap implementations Priority queues and sorting The Heapsort sorting algorithm Resizeable arrays CSE 12 Priority Queues, Heapsort, and Resizable Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
Heaps “Structural property”: A heap is a complete binary tree “Ordering property”: A heap is either a minheap or a maxheap: minheap – The data element in each node is less than or equal to the elements in its descendants. maxheap – The data element in each node is greater than or equal to the elements in its descendants. A heap is a special kind of binary tree that has the following invariants A total order must apply to data elements in a heap. .. they must be comparable to each other!
Background image of page 2
Valid and Invalid Minheaps (a) A valid minheap. (b) An invalid minheap (violates the structural property: 21’s left child is missing). (c) An invalid minheap (violates the minheap ordering property: P is greater than B).
Background image of page 3

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

View Full DocumentRight Arrow Icon
Adding an Element to a MinHeap (a) A minheap prior to adding an element. The circle is where the new element will be put initially. (b) Add the element, 6, as the new rightmost leaf. This maintains a complete binary tree, but may violate the minheap ordering property. (c) “Bubble up” the new element. Starting with the new element, if the child is less than the parent, swap them. This moves the new element up the tree. (d) Repeat the step described in (c) until the parent of the new element is less than or equal to the new element. The minheap invariants have been restored.
Background image of page 4
Adding an Element to a MinHeap Pseudocode : add( element) 1. insert the new element as the new rightmost leaf on the bottom level 2. increment size by 1 3. bubbleUp(rightmost leaf) The time cost of the add() operation is bounded by the distance the new element can travel. Since the new element starts as a leaf and the highest it can climb is to the root, this bound is the height of the tree. In a complete binary tree this is O( log n ), so the time cost of add() is O( log n ) worst case Pseudocode: bubbleUp(node) // recursive version 1 . if node is root of heap, return 2 . if node's element is greater than or equal to node's parent's element, return 3. swap node and parent's elements 4. bubbleUp(parent)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Removing an Element from a MinHeap (a) Moving the rightmost leaf to the top of the heap to fill the gap created when the top element (5) was removed. This is a complete binary tree, but the minheap ordering property has been violated.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

lec20 - CSE 12 Priority Queues, Heapsort, and Resizable...

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

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