cs165: priority queues, heapscs165/.spring20/slides/11-prioqsheaps.pdfheap operations -heapinsert n...

Post on 04-Jun-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS165: Priority Queues, Heaps

CS165 - Priority Queues 1

Sudipto Ghosh, Wim Bohm

Priority Queues

n Characteristicsq Items are associated with a Comparable value: priorityq Provide access to one element at a time - the one with

the highest priority

n offer(E e) and add(E e) – inserts the element into the priority queue based on the priority order

n remove() and poll() – removes the head of the queue (which is the highest priority) and returns it

CS165 - Priority Queues 2

PQ – Linked List Implementation

n Reference-based implementationq Sorted in descending order

n Highest priority value is at the beginning of the linked listn remove() returns the item that pqHead references and

changes pqHead to reference the next item.n offer(E e) must traverse the list to find the correct

position for insertion.

9699.2 95.8 3

pqHead …

CS165 - Priority Queues 3

Complete tree definitionn Complete binary tree of height h

q zero or more rightmost leaves not present at level h

n A binary tree T of height h is complete ifq All nodes at level h – 2 and above have

two children each, andq When a node at level h – 1 has children,

all nodes to its left at the same level have two children each, and

q When a node at level h - 1 has one child, it is a left child

q So the leaves at level h go from left to right

4CS165 - Priority Queues

h-2:

h-1:

h:

Complete Binary Tree

5

Level-by-level numbering of a complete binary tree, NOTE 0 based!

0:Jane

1:Bob 2:Tom

3:Alan 4:Ellen 5:Nancy

What is the parentchild index relationship?

CS165 - Priority Queues

left child i: at 2*i+1

right child i: at 2*(i+1)

parent i: at (i-1)/2

There are no “holes” (missing nodes in the complete binary tree),so we can store a complete binary tree in an array!!

Heap - Definitionn A maximum heap (maxheap) is a complete

binary tree that satisfies the following:q Nodes are (key,value) pairsq It has the heap property:

n Its root contains a key greater or equal to the keys of its children

n Its left and right sub-trees are also maxheaps

n A size 1 heap is just one leaf.

q A minheap has the root less or equal children, and left and right sub trees are also minheaps

CS165 - Priority Queues 6

maxHeap Property Implications

n Implications of the heap property:q The root holds the maximum value (global property)q Values in descending order on every path from root to

leaf

n A Heap is NOT a binary search tree, as in a BST the nodes in the right sub tree of the root are larger than the root

CS165 - Priority Queues 7

Examples

Satisfies heap property AND Complete

Satisfies heap property BUTNot complete

Does not satisfy heapproperty ANDNot complete

50

2520

10 15 5

30

255

10

15

20

30

2015

10 5 25

CS165 - Priority Queues 8

Array(List) Implementation

50

2520

10 15 5

50202510155

0

1

2

3

4

5

CS165 - Priority Queues 9

Array(List) Implementation

n Traversal:q Root at position 0q Left child of position i at position 2*i+1q Right child of position i at position 2*(i+1)q Parent of position i at position (i-1)/2

(don’t forget: int arithmetic truncates)

CS165 - Priority Queues 10

Heap Operations - heapInsertn Step 1: put a new value into first open position

(maintaining completeness), i.e. at the end

n But now we potentially violated the heap property, so:

n Step 2: bubble values up

q Re-enforcing the heap property

q Swap with parent, if key of new value > key of parent, until in the right place.

q The heap property holds for the tree below the new value, when swapping up

CS165 - Priority Queues 11

Swapping up

n Swapping up enforces heap property for sub tree below the new, inserted value:

n if (new > x) swap(x,new)x>y, thereforenew > y

CS165 - Priority Queues 12

xnewy

newx y

Insertion into a heap (Insert 15)

9

65

3 2 15

Insert 15

CS165 - Priority Queues 13

bubble up

Insertion into a heap (Insert 15)

9

6

5

3 2

CS165 - Priority Queues 14

15

bubble up

Insertion into a heap (Insert 15)

15

6

5

3 2

CS165 - Priority Queues 15

9

Heap operations – heapDelete

n Step 1: remove value at root (Why?)n Step 2: substitute with rightmost leaf of bottom level

(Why?)n Step 3: bubble down

q Swap with maximum child as necessary, until in placeq each bubble down restores the heap property at the

swapped node

q this is called HEAPIFY

CS165 - Priority Queues 16

Swapping down

n Swapping down enforces heap property at the swap location:

n new<x and y<x:swap(x,new)

x>y and x>new

CS165 - Priority Queues 17

new

xyx

new y

Deletion from a heap

5

9

3 2

10

6

Delete 10Place last node in root

CS165 - Priority Queues 18

9

5

3 2

6

CS165 - Priority Queues 19

bubble downheapifydraw the heap

5

9

3 2

6

CS165 - Priority Queues 20

delete againdraw the heap

CS165 - Priority Queues 21

5

6

3

25

2

3

6

HeapSort

n Algorithmq Insert all elements (one at a time) to a heapq Iteratively delete them

n Removes minimum/maximum value at each step

CS165 - Priority Queues 22

HeapSort

n Alternative method (in-place):q buildHeap: create a heap out of the input array:

n Consider the input array as a complete binary treen Create a heap by iteratively expanding the portion of the

tree that is a heap q Leaves are already heapsq Start at last internal nodeq Go backwards calling heapify with each internal node

q Iteratively swap the root item with last item in unsorted portion and rebuild

CS165 - Priority Queues 23

Building the heap

n WHY start at (n-2)/2?n WHY go backwards?

n The whole method is called buildHeapn One bubble down is called heapify

buildheap(n){for (i = (n-2)/2 down to 0) //pre: the tree rooted at index is a semiheap//i.e., the sub trees are heapsheapify(i); // bubble down//post: the tree rooted at index is a heap

}

CS165 - Priority Queues 24

CS165 - Priority Queues 25

6

3 7

109 2

6 3 7 9 2 10

Draw as a Complete Binary Tree:

Repeatedly heapify, starting at last internal node, going backwards

CS165 - Priority Queues 26

6

3 10

79 2

CS165 - Priority Queues 27

6

9 10

73 2

CS165 - Priority Queues 28

10

9 7

63 2

10 9 7 3 2 6

In place heapsort using an arrayn First build a heap out of an input array using

buildHeap(). See previous slides.n Then partition the array into two regions; starting

with the full heap and an empty sorted and stepwise growing sorted and shrinking heap.

29

HEAPSorted (Largest elements in array)

CS165 - Priority Queues

30

10 9 6 3 2 59 5 6 3 2 10

5 3 2 1096

6 5 2 3 9 10

3 2 10965

2 3 10965

2 3 10965

HEAP

SORTED

Do it, do it

CS165 - Priority Queues

top related