





















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!






















Priority Queues
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science โ University of San Francisco โ p.1/
Often, itโs useful to be able to enqueue items according to priority
States to search Tasks to perform Calls to answer We always want to be able to quickly dequeue the itemwith the highest (lowest) priority.
Department of Computer Science โ University of San Francisco โ p.2/
Operations
Add an element / priority pair Return (and remove) element with highest priority Implementation:
Sorted Array (this is part 4 of your project)Add Element
n)
Remove Higest Priority
Department of Computer Science โ University of San Francisco โ p.4/
Operations
Add an element / priority pair Return (and remove) element with highest priority Implementation:
Binary Search TreeAdd ElementRemove Higest Priority
Department of Computer Science โ University of San Francisco โ p.5/
Operations
Add an element / priority pair Return (and remove) element with highest priority Implementation:
Binary Search TreeAdd Element
n)
Remove Higest Priority
n)
In the worst case - tree is unbalanced.
Department of Computer Science โ University of San Francisco โ p.7/
Operations
Add an element / priority pair Return (and remove) element with highest priority Implementation:
HeapAdd ElementRemove Higest Priority
Department of Computer Science โ University of San Francisco โ p.8/
Complete Binary Tree (level
n^
is filled in before level
n
starts) Heap Property
For every subtree in a tree, each value in the subtree is<= value stored at the root of the subtree
Department of Computer Science โ University of San Francisco โ p.10/
Department of Computer Science โ University of San Francisco โ p.11/
There is only one place we can insert an element into aheap, so that the heap remains a complete binary tree Inserting an element at the โendโ of the heap might breakthe heap property
Department of Computer Science โ University of San Francisco โ p.13/
There is only one place we can insert an element into aheap, so that the heap remains a complete binary tree Inserting an element at the โendโ of the heap might breakthe heap property
Swap the inserted value with its parent Keep doing this while child is greater than parent. How many swaps will we need to do?
Department of Computer Science โ University of San Francisco โ p.14/
Removing the Root of the heap is hard Removing the element at the โendโ of the heap is easy
Swap the last element with the root May break the heap property
Department of Computer Science โ University of San Francisco โ p.16/
Removing the Root of the heap is hard Removing the element at the โendโ of the heap is easy
Move last element into root Swap the root with the largest child, until heap propertyis satisfied
Department of Computer Science โ University of San Francisco โ p.17/
A Complete Binary Tree can be stored in an array:
20
8
15
7
6
4
14
5
2
1
3
20
8 15 7
6
4
14 5
2
1
3
Department of Computer Science โ University of San Francisco โ p.19/
The root is stored at index 0 For the node stored at index
i:
Left child is stored at index
โ^ i
Right child is stored at index
โ^ i
Parent is stored at index
b(
i^ โ
c Department of Computer Science โ University of San Francisco โ p.20/