Docsity

Priority Queues and Heap Implementation, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Priority Queues and Heap Implementation and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Priority Queues

Chris Brooks

Department of Computer Science

University of San Francisco

Department of Computer Science โ€” University of San Francisco โ€“ p.1/

Priority Queues

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/

Priority Queue ADT

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

O(

n)

Remove Higest Priority

O(1)

Department of Computer Science โ€” University of San Francisco โ€“ p.4/

Priority Queue ADT

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/

Priority Queue ADT

Operations

Add an element / priority pair Return (and remove) element with highest priority Implementation:

Binary Search TreeAdd Element

O(

n)

Remove Higest Priority

O(

n)

In the worst case - tree is unbalanced.

Department of Computer Science โ€” University of San Francisco โ€“ p.7/

Priority Queue ADT

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/

Heap Definition

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/

Heap Examples

3 Valid Heap

Department of Computer Science โ€” University of San Francisco โ€“ p.11/

Heap Insert

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/

Heap Insert

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/

Heap Remove Largest

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/

Heap Remove Largest

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/

Representing Heaps

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/

CBTs as Arrays

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^ โˆ’

/^2

c Department of Computer Science โ€” University of San Francisco โ€“ p.20/