Priority Queues & Heaps
CS16: Introduction to Data Structures & Algorithms Spring 2020
Priority Queues & Heaps CS16: Introduction to Data Structures - - PowerPoint PPT Presentation
Priority Queues & Heaps CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Priority Queues Motivation ADT Implementation Heaps insert( ) and upheap( ) removeMin( ) and downheap( )
Priority Queues & Heaps
CS16: Introduction to Data Structures & Algorithms Spring 2020
Outline
Motivation
transmitted first
Priority Queue ADT
returns element
Priority Queue Implementations
5Activity #1
Priority Queue Implementations
6Activity #1
Priority Queue Implementations
7Activity #1
Priority Queue Implementations
8Activity #1
Priority Queue Implementation
9Implementation insert removeMin
Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n)What is a Heap?
2 6 5 7 9
Heap Properties
Heap Properties
(3, Danzo)
(9, Naruto)(7, Sakura)
Heap: insert
2 6 5 7 9 insertion node
Heap: insert
2 6 5 7 9
1
Heap order violated!
Heap: upheap
2 1 5 7 9
6
Heap: upheap
1 2 5 7 9
6
Heap: insert
17Activity #1
Heap: insert
18Activity #2
Heap: insert
19Activity #1
Heap: insert
20Activity #1
Heap: upheap Summary
Heap: removeMin
Heap destroyed!
Heap: removeMin
Order destroyed!
6 2 5 7 9Heap: removeMin
Heap is in
Heap: downheap Summary
Heap: removeMin
26Activity #1
Heap: removeMin
27Activity #1
Heap: removeMin
28Activity #1
Heap: removeMin
29Activity #1
Summary of Heap
Array-based Heap
w/ array of size n+1
2 5 6 9 7
1 2 3 4 5Finding Insertion Node
Priority Queue Implementation
33Implementation insert removeMin
Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n)References