18HeapsPriorityQueues

18HeapsPriorityQueues - CMSC 132: Object-Oriented...

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

View Full Document Right Arrow Icon
CMSC 132: Object-Oriented Programming II Department of Computer Science University of Maryland, College Park
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overview Binary trees Complete Heaps Insert getSmallest Heap applications Heapsort Priority queues
Background image of page 2
Complete Binary Trees An binary tree (height h) where Perfect tree to level h-1 Leaves at level h are as far left as possible h = 2 h = 3 h = 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
Complete Binary Trees Not Allowed Basic complete tree shape
Background image of page 4
Heaps Two key properties Complete binary tree Value at node Smaller than or equal to values in subtrees Example heap X Y X Z Y X Z
Background image of page 5

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

View Full DocumentRight Arrow Icon
Heaps Non-heaps 6 2 22 8 45 25 6 2 22 8 45 25 8 6 45 5 6 22 25 5 5 45 5
Background image of page 6
Heap Properties Heaps are balanced trees Height = log 2 (n) = O(log(n)) Can find smallest element easily Always at top of heap! Can organize heap to find maximum value Value at node larger than values in subtrees Heap can track either min or max, but not both
Background image of page 7

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

View Full DocumentRight Arrow Icon
Heap Key operations Insert ( X ) getSmallest ( ) Key applications Heapsort Priority queue
Background image of page 8
Heap Operations – Insert( X ) Algorithm 1. Add X to end of tree 2. While (X < parent) Swap X with parent // X bubbles up tree Complexity # of swaps proportional to height of tree O( log(n) )
Background image of page 9

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

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

Page1 / 34

18HeapsPriorityQueues - CMSC 132: Object-Oriented...

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

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