{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

18HeapsPriorityQueues

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

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

CMSC 132: Object-Oriented Programming II Department of Computer Science University of Maryland, College Park

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

View Full Document
Overview Binary trees Complete Heaps Insert getSmallest Heap applications Heapsort Priority queues
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

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

View Full Document
Complete Binary Trees Not Allowed Basic complete tree shape
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

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

View Full Document
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
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

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

View Full Document
Heap Key operations Insert ( X ) getSmallest ( ) Key applications Heapsort Priority queue
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) )

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 34

18HeapsPriorityQueues - CMSC 132 Object-Oriented...

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

View Full Document
Ask a homework question - tutors are online