






















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
Heap Properties Heap Operations Heap Implementation Heap Application – Heapsort Priority Queue
Heap & Non-heap Examples
Heaps
(^6) Non-heaps
Heap Properties
insert (
getSmallest ( )
Heapsort Priority queue
Heap Insert Example
Insert toend of tree
Compare to parent,swap if parent key larger
Insertcomplete
Heap Insert Example
Insert toend of tree
Compare to parent,swap if parent key larger
Insertcomplete
Heap GetSmallest Example
Replace rootwith end of tree
Compare node tochildren, if larger swapwith smallest child
Repeat swap
if needed
Heap GetSmallest Example
Replace rootwith end of tree
Compare node tochildren, if larger swapwith smallest child
Repeat swap
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) = 2
i^ +
RightChild(
i) = 2
i^ +
Heap Implementation
LeftChild(
LeftChild(
LeftChild(
Heap Implementation
RightChild(
RightChild(
Heapsort Example
As tree As array
Heapsort – Insert Values