


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Overview
Complete
Insert getSmallestgetSmallest
Heapsort Priority queues
Complete Binary Trees
Not Allowed
Basic complete tree shape
Heaps
Complete binary tree Value at node
Smaller than or equal to values in subtrees
≤≤≤≤
≤≤≤≤
Heap Properties
Height = log
2
(n) = O(log(n))
Always at top of heap!Always at top of heap!
Value at node larger than values in subtrees Heap can track either min or max, but not both
Heap
Insert (
getSmallest ( )
Heapsort Priority queue
Heap Insert Example
Insert toend of tree
Compare to parent,
swap if parent key larger
complete
Heap Insert Example
Insert toend of tree
Compare to parent,
swap if parent key larger
complete
Heap GetSmallest Example
with end of tree
children, if larger swap
with smallest child
if needed
Heap GetSmallest Example
with end of tree
children, if larger swap
with smallest child
if needed
Heap Implementation
Compact representation Edges are implicit (no storage required) Works well for complete trees (no wasted space)
Heap Implementation
Array index
i
starts at 0
Parent(
i
i
LeftChild(
i
i
RightChild(
i
i
Heap Implementation
LeftChild(
LeftChild(
LeftChild(
Heap Implementation
RightChild(
RightChild(