lecture 3: priority queues, and a tree grows in 247lecture 3: priority queues, and a tree grows in...

440
Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Upload: others

Post on 07-Jan-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Lecture 3:

Priority Queues, and

a Tree Grows in 247

1These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Page 2: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Announcements● Lab 1 due Friday 2/8 at 11:59 PM

○ Turn in via Gradescope

○ Math hint:

○ Lab 3 out this Wednesday

○ Has three parts: pre-lab (due 2/12), coding, and write-up parts (due 2/15)

2

Page 3: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Overview

● What is a Priority Queue

○ ADT

○ Applications

● Some not so great implementations (which you’ll explore in Studio 3)

○ Lists

○ Arrays

3

Page 4: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Overview

● What is a Priority Queue

○ ADT

○ Applications

● Some not so great implementations (which you’ll explore in Studio 3)

○ Lists

○ Arrays

● Trees

● Priority Queue using trees

● Using arrays to simulate trees

○ Implementation of this is Lab 3

4

Page 5: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

ADTs from Last Time

● The data structures we reviewed last time (queues,

stacks) track the positions of their elements.

○ “Add to the tail”/ “Remove from the head” [queues]

○ “Add to the top”/ “Remove from the top” [stacks]

● Elements themselves were completely generic – we

neither knew nor cared about their properties.

5

Page 6: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Working With Ordered Data

● But let’s suppose we have data that is ordered (e.g.

integers).

● Given a collection of such data, we may want to ask

questions that depend on the order of the elements.

6

Page 7: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Working With Ordered Data

● But let’s suppose we have data that is ordered (e.g.

integers).

● Given a collection of such data, we may want to ask

questions that depend on the order of the elements.

● Challenge: can we efficiently answer these questions

when the collection is changing dynamically?

7

Page 8: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example: Auto Repair

● Garage receives a stream of cars needing repairs.

● Each repair job comes with a deadline.

● Cars may not show up in order of deadlines.

8

Page 9: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example: Auto Repair

● Garage receives a stream of cars needing repairs.

● Each repair job comes with a deadline.

91:00 PM

Page 10: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example: Auto Repair

● Garage receives a stream of cars needing repairs.

● Each repair job comes with a deadline.

101:00 PM 5:00 PM

Page 11: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example: Auto Repair

● Garage receives a stream of cars needing repairs.

● Each repair job comes with a deadline.

111:00 PM 5:00 PM 11:00 AM!!!

Page 12: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

What Do We Want?

● Query: at any time, which car needs to be ready first?

(earliest deadline)

● Insertion: new cars can show up at any time, with any

deadline.

● Update: when we repair the car with the earliest deadline,

which car has next earliest deadline?

12

Page 13: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

More Abstractly…

● Maintain a collection of ordered values [e.g. deadlines]

● Values can be inserted in any order

● At any time, may remove smallest value

● Want to maintain O(1) query time for smallest value

13

Page 14: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

More Abstractly…

● Maintain a “min-first priority queue” PQ

● PQ.insert(v) – insert element v

● PQ.extractMin() – extract and return minimum element

● PQ.peekMin() – must be constant-time

14

Page 15: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A Few More Details

● PQ has a fixed maximum size [e.g. size of garage]

● An item’s value might decrease while it is in PQ

[e.g. a customer now wants their car back sooner]

Assumptions:

1. Items are not known until they are inserted

2. An item’s value cannot increase while it is in PQ

15

Page 16: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

What PQ Methods Might Look Like in Java

● instantiation: PriorityQueue<T>(int size)

○ The queue has a bounded size that is specified upon creation

● insertion into the PQ: Decreaser<T> insert(T thing)

○ The returned “Decreaser” object is often called a handle

○ Decreaser<T> allows outside activity to decrease the value of inserted thing

● is the PQ empty? boolean isEmpty()

● remove and return the currently smallest T: extractMin()

● inspect but do not remove the currently smallest T: peekMin()

16

Page 17: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

What PQ Methods Might Look Like in Java

● instantiation: PriorityQueue<T>(int size)

○ The queue has a bounded size that is specified upon creation

● insertion into the PQ: Decreaser<T> insert(T thing)

○ The returned “Decreaser” object is often called a handle

○ Decreaser<T> allows outside activity to decrease the value of inserted thing

● is the PQ empty? boolean isEmpty()

● remove and return the currently smallest T: extractMin()

● inspect but do not remove the currently smallest T: peekMin()

17

We could just as well define a max-first PQ that maintains the largest

element. The book makes this choice.

Page 18: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Further Notes on Java Impl (See Lab 3)

● What about Decreaser<T>?

○ T getValue() to get at the current value of this thing

○ void decrease(T newvalue)

○ We require that newvalue be no greater than the current value for the affected item

○ Why is this an operation on an object (Decreaser) outside of the PQ,

instead of, say, PQ.decreaseItem(T which, T newvalue) ?

18

Page 19: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Further Notes on Java Impl (See Lab 3)

● What about Decreaser<T>?

○ T getValue() to get at the current value of this thing

○ void decrease(T newvalue)

○ We require that newvalue be no greater than the current value for the affected item

○ Why is this an operation on an object (Decreaser) outside of the PQ,

instead of, say, PQ.decreaseItem(T which, T newvalue) ?

19

In the alternative, how long could it take to locate “which”?

Is the entry “which” unique?

Page 20: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

20

new PriorityQueue(5)

Page 21: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

21

new PriorityQueue(5)

● Can hold up to 5

elements

Page 22: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

22

new PriorityQueue(5)

● Can hold up to 5

elements● Is initially empty

Page 23: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

23

84

insert(84)

Page 24: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

24

92

84

insert(92)

Page 25: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

25

92

46

84

insert(46)

Page 26: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

26

5392

46

84

insert(53)

Page 27: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

27

5392

46

84

extractMin()

Page 28: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

28

5392

46

84

extractMin()

Page 29: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

29

5392

84

insert(247)

247

Page 30: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

30

5392

84

extractMin()

247

Page 31: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

31

53

92

84

extractMin()

247

Page 32: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

32

92

84

247 decrease(131)

Page 33: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

33

92

84

247 decrease(131)

We decrease 247 using its handle, the Decreaser object, which has a direct

reference to the 247 entry.

Why do we need the Decreaser?

It references 247 directly, so we can decrease the value in the Priority Queue without having to find

247 first in the Priority Queue.

This avoids a search for 247, which might require looking at every entry, taking O(n) time for a Priority Queue of

n elements.

Page 34: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

34

92

84

247 decrease(131)

We have not yet considered the

implementation, but we will soon see that the 247 entry may move around in the data structure we use.

Page 35: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

35

92

84

247

decrease(131)

We have not yet considered the

implementation, but we will soon see that the 247 entry may move around in the data structure we use.

As it does, the Decreaser continues

to follow it, no matter where it goes.

Page 36: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

36

92

84

247

decrease(131)

We have not yet considered the

implementation, but we will soon see that the 247 entry may move around in the data structure we use.

As it does, the Decreaser continues

to follow it, no matter where it goes.

The data structure returns an entry’s unique Decreaser object as the result

of insertion.

This provides a fast method for decreasing the value, as shown on the

next slides.

Page 37: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

37

92

84

247 decrease(131)

Page 38: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

38

92

84

131 decrease(131)

Page 39: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

39

92

84

131

Page 40: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

40

92

84

insert(347)

131

Page 41: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

41

92

84

insert(347)

131

347

Page 42: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

42

92

84

131

decrease(77)347

Page 43: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

43

92

84

131

decrease(77)347

Page 44: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

44

77

84

131

decrease(77)347

Page 45: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

45

84

13177

347

Page 46: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

46

84

extractMin()

13177

347

Page 47: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

47

84

extractMin()

131

77

347

Page 48: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Example of a Priority Queue

48

84

131

347

Page 49: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Applications

● Scheduling Tasks with Priorities

○ Find/Handle highest-priority task first

○ E.g. in computer operating systems

○ Searching for the Best Solution

○ Add solutions to PQ as they are found

○ At any time, can query/remove the optimum

○ Cost of solutions may decrease over time

○ (e.g. shortest path to each node in a graph)

49

84

131

347

Page 50: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Applications

● Scheduling Tasks with Priorities

○ Find/Handle highest-priority task first

○ E.g. in computer operating systems

○ Searching for the Best Solution

○ Add solutions to PQ as they are found

○ At any time, can query/remove the optimum

○ Cost of solutions may decrease over time

○ (e.g. shortest path to each node in a graph)

● Can you think of others?

50

84

131

347

Page 51: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Performance Goals for Priority Queue

● Let n be the size of the queue at a given time.

● peekMin() should be constant-time

● Want “nice” complexity for insert, decrease, extractMin as fcn of n.

● Ideally, all these operations should take time sub-linear in n (i.e.

o(n))

51

Page 52: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Ideas? (Analyzed in Studio 3)

52

PQ ops needed:

insert(v)

decrease(item, k)

extractMin()

peekMin()

o(n)

Θ(1)● Unsorted linked list?

Page 53: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Ideas?

● Unsorted linked list?

○ [high cost to find new min on extractMin()]

● Sorted linked list?

53

PQ ops needed:

insert(v)

decrease(item, k)

extractMin()

peekMin()

o(n)

Θ(1)

Page 54: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Ideas?

● Unsorted linked list?

○ [high cost to find new min on extractMin()]

● Sorted linked list?

○ [high cost to insert]

● Unsorted array?

54

PQ ops needed:

insert(v)

decrease(item, k)

extractMin()

peekMin()

o(n)

Θ(1)

Page 55: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Ideas?

● Unsorted linked list?

○ [high cost to find new min on extractMin()]

● Sorted linked list?

○ [high cost to insert]

● Unsorted array?

○ [just as bad as unsorted list]

55

PQ ops needed:

insert(v)

decrease(item, k)

extractMin()

peekMin()

o(n)

Θ(1)

Page 56: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Ideas?

● Unsorted linked list?

● Sorted linked list?

● Unsorted array?

Argh – we need a new data structure to meet

better-than-Θ(n) performance goals for both

insertion and extraction!

56

PQ ops needed:

insert(v)

decrease(item, k)

extractMin()

peekMin()

o(n)

Θ(1)

Page 57: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A Brief Diversion: Trees

● Lists are a one-dimensional data structure○ One-dimensional connections (forward, back)

● We can use them to implement other data structures○ Queue

○ Stack

● We now consider trees○ These are two-dimensional

○ Movement up or down

○ Movement left or right

● We will use trees to implement several interesting data structures

57

Page 58: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example:

58Text p. 1088 (Appendix B)

Page 59: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Trees have

■ Nodes

59Text p. 1088 (Appendix B)

Page 60: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Trees have

■ Nodes

■ Edges

60Text p. 1088 (Appendix B)

Page 61: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Trees have

■ Nodes

■ Edges

○ The edges are undirected

61Text p. 1088 (Appendix B)

Page 62: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Tree is upside down!

62Text p. 1088 (Appendix B)

Page 63: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Tree is upside down!

63Text p. 1088 (Appendix B)

Page 64: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Tree is upside down!

○ We speak of a parent

64Text p. 1088 (Appendix B)

Page 65: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Tree is upside down!

○ We speak of a parent

■ And its children

■ They are siblings

65Text p. 1088 (Appendix B)

Page 66: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Many definitions

● Here’s an example

● Some notes:○ Tree is upside down!

○ We speak of a parent

■ And its children

● For now○ A tree is rooted

■ Root is orphan node

66Text p. 1088 (Appendix B)

Page 67: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Each node occurs at

some depth from the root○ The root is at depth 0

67Text p. 1088 (Appendix B)

Page 68: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Each node occurs at

some depth from the root○ The root is at depth 0

● The height of a tree is

the maximum depth

among all of the tree’s

nodes

68Text p. 1088 (Appendix B)

Page 69: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Each node occurs at

some depth from the root○ The root is at depth 0

● The height of a tree is

the maximum depth

among all of the tree’s

nodes

● Some nodes are leaves

69Text p. 1088 (Appendix B)

Page 70: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● Each node occurs at

some depth from the root○ The root is at depth 0

● The height of a tree is

the maximum depth

among all of the tree’s

nodes

● Some nodes are leaves

● Others are

internal nodes

70Text p. 1088 (Appendix B)

Page 71: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● If the left-to-right

orientation of the

children matters,

tree is ordered

71Text p. 1088 (Appendix B)

?

Page 72: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Trees

● If the left-to-right

orientation of the

children matters,

tree is ordered

● The degree of a node is

the count of its children

72Text p. 1088 (Appendix B)

3

2 2

2

1

1

00

0

0

0 0

Page 73: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Back to Priority

Queues…

73

Page 74: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Specializing Trees for Our Needs

● We’ll focus on binary trees – every node has at most

two children.

● We’ll focus on compact binary trees – nodes are

always added top-to-bottom and left-to-right

74

Page 75: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Specializing Trees for Our Needs

● We’ll focus on binary trees – every node has at most

two children.

● We’ll focus on compact binary trees – nodes are

always added top-to-bottom and left-to-right

● (There is a unique compact binary tree with n nodes)

75

Page 76: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree Initially the tree is empty

Page 77: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree Initially the tree is empty

● unoccupied● occupied

size = 0

Page 78: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree The tree fills in from

● left to right● top to bottom

size = 1

Page 79: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree The tree fills in from

● left to right● top to bottom

size = 1

This node

currently has no children

Page 80: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree ● All levels are complete up

to the filling level.● The filling level is compact

from left to rightsize = 2

Now it has

one child

Page 81: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree ● All levels are complete up

to the filling level.● The filling level is compact

from left to rightsize = 3

Now it has two

children

Page 82: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 4

Page 83: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 5

Page 84: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 6

Page 85: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 7

Page 86: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 8

Page 87: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Compact binary tree

size = 9

Page 88: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

This tree is not compact!

Page 89: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A binary heap is a

compact binary tree

with an ordering

invariant – the heap

property.

89

Page 90: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Heap property: a special relationship between

each parent and its children

90

p

a b

● p.value ≤ min(a.value, b.value)

● Says nothing about how a.value and b.value compare

property for

min-first heaps!

Page 91: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Examples

● Has heap property

91

3

7 3

Page 92: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Examples

● Has heap property

● Lacks heap property (what do you see that is wrong?)

92

3

7 3

5

5 3

Page 93: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Examples

● Has heap property

● Lacks heap property

93

3

7 3

5

5 3

Page 94: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Heap Property Implies Fast peekMin()

● In a heap, the heap property applies between every

node and its children (if any).

● So where is the smallest element in a binary heap?

94

Page 95: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Heap Property Implies Fast peekMin()

● In a heap, the heap property applies between every

node and its children (if any).

● So where is the smallest element in a binary heap?

● At the root, of course…

95

Page 96: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Heap Property Implies Fast peekMin()

● In a heap, the heap property applies between every

node and its children (if any).

● So where is the smallest element in a binary heap?

● At the root, of course?

● Better prove it…

96

Page 97: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

97

Page 98: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):

98

Page 99: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

99

Page 100: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

100

p

a

1

a

n

Page 101: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

101

p

a

1

a

n

Nodes above p in

the tree → proper ancestors of p

Page 102: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

102

p

a

1

a

n

Nodes above p in

the tree → proper ancestors of p

Nodes below p in the tree →

proper descendants

Page 103: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

○ Here let’s say the name and value of a node are synonymous

■ so p contains value p, a1 contains value a1 , and so on

103

p

a

1

a

n

Page 104: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

○ Here let’s say the name and value of a node are synonymous

■ so p contains value p, a1 contains value a1 , and so on

○ Consider the sequence of proper ancestors of p

■ a1 a2 ….an

104

p

a

1

a

n

Page 105: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

○ Here let’s say the name and value of a node are synonymous

■ so p contains value p, a1 contains value a1 , and so on

○ Consider the sequence of proper ancestors of p

■ a1 a2 ….an

○ Applying the heap property we obtain:

105

p

a

1

a

n

Page 106: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

○ Here let’s say the name and value of a node are synonymous

■ so p contains value p, a1 contains value a1 , and so on

○ Consider the sequence of proper ancestors of p

■ a1 a2 ….an

○ Applying the heap property we obtain:

○ This contradicts the claim that an is not a minimal element

106

p

a

1

a

n

Page 107: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● If a heap is not empty → a minimum element is found at its root

● Proof (by contradiction):○ Suppose we have a nonempty heap and the root node is not a minimal element

○ Then a minimal element must exist somewhere else, say at node p

○ Here let’s say the name and value of a node are synonymous

■ so p contains value p, a1 contains value a1 , and so on

○ Consider the sequence of proper ancestors of p

■ a1 a2 ….an

○ Applying the heap property we obtain:

○ This contradicts the claim that an is not a minimal element

107

p

a

1

a

n

QED

Page 108: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Binary Heap Operations

● How do we implement

● insert

● extractMin

● decrease

108

Page 109: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Binary Heap Operations

● How do we implement

● insert

● extractMin

● decrease We’ll do this one first

109

Page 110: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

87 91 31 17 46 77 79 4 58

110

Page 111: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

87 91 31 17 46 77 79 4 58

111

Page 112: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

87 91 31 17 46 77 79 4 58

112

91 58

31 46 87 79

17 77

4

Page 113: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

113

91 58

31 46 87 79

17 77

4

decrease(32)

Page 114: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

114

91 32

31 46 87 79

17 77

4

decrease(32)

Page 115: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

115

91 32

31 46 87 79

17 77

4

decrease(32)

Heap property maintained

So no action is necessary

Page 116: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

116

91 32

31 46 87 79

17 77

4

Page 117: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

117

91 32

31 46 87 79

17 77

4

decrease(14)

Page 118: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

118

91 14

31 46 87 79

17 77

4

decrease(14)

Page 119: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

119

91 14

31 46 87 79

17 77

4

Page 120: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

120

91 14

31 46 87 79

17 77

4

Heap property broken!

What do we do?

Page 121: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

121

91 14

31 46 87 79

17 77

4● If a1> p

○ then heap property is broken○ ...but swapping them makes those two values OK,○ so we can keep swapping up the tree until the heap property

is restored.

p

a

1

a

n

Page 122: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

122

91 14

31 46 87 79

17 77

4

Page 123: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

123

91 14

31 46 87 79

17 77

4

Page 124: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

124

91 31

14 46 87 79

17 77

4

Page 125: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

125

91 31

14 46 87 79

17 77

4

Page 126: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

126

91 31

14 46 87 79

17 77

4

Page 127: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

127

91 31

17 46 87 79

14 77

4

Page 128: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

128

91 31

17 46 87 79

14 77

4

Page 129: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

129

91 31

17 46 87 79

14 77

4

Page 130: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

130

Page 131: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

131

Algorithm for

insert?

Page 132: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

132

Algorithm for

insert?

Algorithm for

decrease

Page 133: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

133

Algorithm for

insert!

Algorithm for

decrease

Page 134: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

● Will argue that insert() reduces to decrease()

134

Algorithm for

insert!

Algorithm for

decrease

Page 135: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

OK, Now For Insertion

● Claim: if you can do decrease(), you can do insert()!

● Will argue that insert() reduces to decrease()

● [“If you can do decrease, here’s how to use it for insert”] 135

Algorithm for

insert!

Algorithm for

decrease

Page 136: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

136

91 31

17 46 87 79

14 77

4

Page 137: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

137

91 31

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value

Page 138: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

138

91 31

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value

Page 139: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

139

91 31

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

Page 140: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

140

91 31

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

decrease(12)

Page 141: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

141

91 31 12

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

decrease(12)

Page 142: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

142

91 31 12

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

decrease(12)

Page 143: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

143

91 31 12

17 46 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 144: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

144

91 31 46

17 12 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 145: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

145

91 31 46

17 12 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 146: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

146

91 31 46

17 12 87 79

14 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 147: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

147

91 31 46

17 14 87 79

12 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 148: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

148

91 31 46

17 14 87 79

12 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 149: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

149

91 31 46

17 14 87 79

12 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 150: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

150

91 31 46

17 14 87 79

12 77

4

● The empty nodes are not really there yet in this heap

● But we could regard them as having infinite value● Upon insertion

○ value decreases from to inserted element’s value

● But we know how to handle this already○ So insert is reduced to decrease

Page 151: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

151

91 31 46

17 14 87 79

12 77

4

Page 152: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

One More Operation

● extractMin – remove smallest element of heap

● We know where the smallest element is… [root]

● But once we remove it, tree is no longer compact!

152

Page 153: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

153

91 31 46

17 14 87 79

12 77

4We will remove and return

this value

Page 154: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

154

91 31 46

17 14 87 79

12 77

What about this hole?

Page 155: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

155

91 31 46

17 14 87 79

12 77

● Move the last value to the root

Page 156: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

156

91 31 46

17 14 87 79

12 77

● Move the last value to the root

Page 157: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

157

91 31

17 14 87 79

12 77

46

● Move the last value to the root

Page 158: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Wait, what?????

● The tree is compact again – hooray!

● But heap property at root may now be violated – boo!

● How can we we fix up the tree to be a heap again?

● Will use another swapping procedure: heapify

158

Page 159: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

159

91 31

17 14 87 79

12 77

46

● Move the last value to the root

● Heapify at that node○ Exchange it with the lesser of

its children if necessary

Page 160: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

160

91 31

17 14 87 79

12 77

46

● Move the last value to the root

● Heapify at that node○ Exchange it with the lesser of

its children if necessary

12 < 77 so we exchange 46 with 12

Page 161: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

161

91 31

17 14 87 79

46 77

12

● Move the last value to the root

● Heapify at that node○ Exchange it with the lesser of

its children if necessary

● Recursively Heapify

Page 162: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

162

91 31

17 14 87 79

46 77

12

● Move the last value to the root

● Heapify at that node○ Exchange it with the lesser of

its children if necessary

● Recursively Heapify

14 wins here

Page 163: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

163

91 31

17 46 87 79

14 77

12

● Move the last value to the root

● Heapify at that node○ Exchange it with the lesser of

its children if necessary

● Recursively Heapify

Page 164: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

164

91 31

17 46 87 79

14 77

12

● Done!

● But let’s do that again

Page 165: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

165

91 31

17 46 87 79

14 77

12

● We will return 12

Page 166: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

166

91 31

17 46 87 79

14 77

● We will return 12

● Creates a hole at root

Page 167: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

167

91 31

17 46 87 79

14 77

● We will return 12

● Creates a hole at root● Move last element to root

Page 168: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

168

91

17 46 87 79

14 77

31

● Heapify

Page 169: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

169

91

17 46 87 79

14 77

31

● Heapify

14 < 77 so we exchange 31 with 14

Page 170: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

170

91

17 46 87 79

31 77

14

● Heapify

Page 171: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

171

91

17 46 87 79

31 77

14

● Heapify

17 wins here

Page 172: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

172

91

31 46 87 79

17 77

14

● Heapify

Page 173: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

173

91

31 46 87 79

17 77

14

● Heapify

● Done

Page 174: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

174

91

31 46 87 79

17 77

14

● Again

Page 175: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

175

91

31 46 87 79

17 77

14

● Again

● 14 will be returned

Page 176: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

176

91

31 46 87 79

17 77

● Again

● 14 will be returned

Page 177: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

177

91

31 46 87 79

17 77

● Again

● 14 will be returned

Page 178: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

178

31 46 87 79

17 77

91

● Move last element to root

Page 179: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

179

31 46 87 79

17 77

91

● Heapify

Page 180: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

180

31 46 87 79

17 77

91

● Heapify

17 < 77 so we exchange 91 with 17

Page 181: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

181

31 46 87 79

91 77

17

● Heapify

Page 182: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

182

31 46 87 79

91 77

17

● Heapify

31 wins here

Page 183: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

183

91 46 87 79

31 77

17

● Heapify

Page 184: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

184

91 46 87 79

31 77

17

● Again, 17 will be returned

Page 185: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Pause… you try it

Take a minute to work through the next couple of extractions yourself…

185

Page 186: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

186

91 46 87 79

31 77

● Again, 17 will be returned

Page 187: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

187

91 46 87 79

31 77

● Move last element to root

Page 188: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

188

91 46 87

31 77

79

● Move last element to root

Page 189: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

189

91 46 87

31 77

79

● Heapify

Page 190: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

190

91 46 87

31 77

79

● Heapify

31 < 77 so we exchange 79 with 31

Page 191: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

191

91 46 87

79 77

31

● Heapify

Page 192: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

192

91 46 87

79 77

31

● Heapify

46 wins here

Page 193: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

193

91 79 87

46 77

31

● Heapify

Page 194: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

194

91 79 87

46 77

31

● Done

Page 195: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

195

91 79 87

46 77

31

● Again, 31 will be returned

Page 196: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

196

91 79 87

46 77

● Again, 31 will be returned

● Tell me what to do !!

Page 197: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

197

91 79 87

46 77

● Tell me what to do

Page 198: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

198

91 79

46 77

87

● Tell me what to do

Page 199: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

199

91 79

46 77

87

● Tell me what to do

????

Page 200: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

200

91 79

87 77

46

● Tell me what to do

????

Page 201: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

201

91

79 77

46

● Tell me what to do

87

Page 202: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

202

91

79 77

46

● Tell me what to do

87

Page 203: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

203

91

79 77

46

● Again, 46 will be returned

87

Page 204: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

204

91

79 77

● Again, 46 will be returned

87

Page 205: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

205

91

79 77

● What should I do?

87

Page 206: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

206

91

79 77

● What should I do?

87

Page 207: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

207

91

79 77

87

● What should I do?

Page 208: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

208

91

79 77

87

● What should I do?

????

Page 209: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

209

91

79 77

87

● What should I do?

Page 210: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

210

91

79 87

77

● What should I do?

Page 211: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

211

91

79 87

77

● What should I do?

Page 212: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

212

91

79 87

77

● Again, 77 will be returned

Page 213: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

213

91

79 87

● Again, 77 will be returned

Page 214: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

214

91

79 87

● What to do?

Page 215: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

215

91

79 87

● What to do?

Page 216: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

216

79 87

91

● What to do?

Page 217: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

217

79 87

91

● What to do?

????

Page 218: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

218

91 87

79

● What to do?

Page 219: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

219

91 87

79

● What to do?

Page 220: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

220

91 87

79

● 79 is returned next

Page 221: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

221

91 87

● 79 is returned next

Page 222: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

222

91 87

● And…?

Page 223: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

223

91 87

● And…?

Page 224: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

224

91

87

● And…?

Page 225: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

225

91

87

● And…?

????

Page 226: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

226

91

87

● And…?

Page 227: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

227

91

87

● 87 is returned next

Page 228: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

228

91

● You know….

Page 229: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

229

91

● You know….

Page 230: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

230

91

● You know….

Page 231: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

231

91

● You know….

Page 232: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

232

91

● Finally 91 is returned

Page 233: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Extracting the min

233

● Finally 91 is returned

● And the heap is empty

Page 234: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Time For Performance Analysis

● We now have correct procedures for the binary heap operations

234

Page 235: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Time For Performance Analysis

● We now have correct? procedures for the binary heap operations

● (Should really write proofs that heap property is restored… later)

● Right now, we ask: just how fast are these operations?

235

Page 236: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Intuition

● We want to give the cost of operations on a heap of size n.

236

Page 237: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Intuition

● We want to give the cost of operations on a heap of size n.

● An insert or decrease might move a value from the bottom of the tree

up to the root.

● An extractMin might move a value (the new root) from the root of the

tree down to the bottom.

237

Page 238: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Intuition

● We want to give the cost of operations on a heap of size n.

● An insert or decrease might move a value from the bottom of the tree

up to the root.

● An extractMin might move a value (the new root) from the root of the

tree down to the bottom.

● So we need to reason about how tall a heap with n elements is.

238

Page 239: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Height vs # Nodes

239

For a complete binary tree, how does

the height of tree affect the number of nodes?

Height 1? #nodes= 3

Page 240: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Height vs # Nodes

240

For a complete binary tree, how does the

height of tree affect the number of nodes?

Height 2? #nodes= 7

Page 241: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Height vs # Nodes

241

For a complete binary tree, how does the

height of tree affect the number of nodes?

Height 3? #nodes= 15

Page 242: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Height vs # Nodes

242

For a complete binary tree, how does the

height of tree affect the number of nodes?

Height 3? #nodes= 15

height k #nodes = ???

0 1

1 3

2 7

3 15

Page 243: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Height vs # Nodes

243

For a complete binary tree, how does the

height of tree affect the number of nodes?

Height 3? #nodes= 15

height k #nodes = 2k+1-1

0 1

1 3

2 7

3 15

Page 244: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Theorem

● A complete binary tree (all non-leaves have two children) of

height k has 2k+1 -1 nodes.

244

Page 245: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

But First…

● Lemma: a complete binary tree of height k has 2k leaves.

245

Page 246: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

But First…

● Lemma: a complete binary tree of height k has 2k leaves.

● Pf: By induction on k.

246

Page 247: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

But First…

● Lemma: a complete binary tree of height k has 2k leaves.

● Pf: By induction on k.

● Base: k = 0 → tree is a single node → 20 = 1 leaf.

247

Page 248: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

But First…

● Lemma: a complete binary tree of height k has 2k leaves.

● Pf: By induction on k.

● Base: k = 0 → tree is a single node → 20 = 1 leaf.

● Ind: Suppose true for tree T of height k.

248

Page 249: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

But First…

● Lemma: a complete binary tree of height k has 2k leaves.

● Pf: By induction on k.

● Base: k = 0 → tree is a single node → 20 = 1 leaf.

● Ind: Suppose true for tree T of height k.

● We extend T by one level, adding two leaves below each node at

the bottom of T. By IH, T has 2k leaves, so extension has 2k+1.249QED

Page 250: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 - 1 nodes.

● Pf: By induction on k.

250

Page 251: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 – 1 nodes.

● Pf: By induction on k.

● Bas: k = 0 → single node → 20+1 – 1 = 1 nodes.

251

Page 252: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 – 1 nodes.

● Pf: By induction on k.

● Bas: k = 0 → single node → 20+1 – 1 = 1 nodes.

● Ind: Suppose true for tree T of height k.

252

Page 253: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 – 1 nodes.

● Pf: By induction on k.

● Bas: k = 0 → single node → 20+1 – 1 = 1 nodes.

● Ind: Suppose true for tree T of height k.

● By IH, T has 2k+1 – 1 nodes. Adding k+1st level adds 2k+1 leaves.

253

Page 254: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 – 1 nodes.

● Pf: By induction on k.

● Bas: k = 0 → single node → 20+1 – 1 = 1 nodes.

● Ind: Suppose true for tree T of height k.

● By IH, T has 2k+1 – 1 nodes. Adding k+1st level adds 2k+1 leaves.

254

by Lemma

Page 255: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back To Theorem…

● Thm: a complete binary tree of height k has 2k+1 – 1 nodes.

● Pf: By induction on k.

● Bas: k = 0 → single node → 20+1 – 1 = 1 nodes.

● Ind: Suppose true for tree T of height k.

● By IH, T has 2k+1 – 1 nodes. Adding k+1st level adds 2k+1 leaves.

● Extended tree has 2(2k+1) – 1 = 2k+2 – 1 nodes. QED255

by Lemma

Page 256: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

So What?

● Complete binary tree of height k has Θ(2k) nodes.

● Hence, complete binary tree with n nodes has height Θ(log n).

256

Page 257: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

So What?

● Complete binary tree of height k has Θ(2k) nodes.

● Hence, complete binary tree with n nodes has height Θ(log n).

● What about compact but not complete trees?

257

Page 258: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

So What?

● Complete binary tree of height k has Θ(2k) nodes.

● Hence, complete binary tree with n nodes has height Θ(log n).

● What about compact but not complete trees?

● All levels except the bottom are full → can show tree of height k has

at least 2k nodes.

● Conclude that a compact tree with n nodes still has height Θ(log n).258

Page 259: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Conclusions About Running Time

● decrease/insert/heapify may move an element from the bottom to top

or top to bottom of a compact tree – Θ(log n) levels.

● Time to move is O(1) per level of tree.

● Conclude that these operations take worst-case time Θ(log n).

259

Page 260: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Another Way to Analyze Complexity

● Heapify is often written as a recursive procedure.

260

Page 261: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Another Way to Analyze Complexity

● Heapify is often written as a recursive procedure.

261

Heapify(tree rooted at v)

if (v is bigger than its smallest child c)

swap values of nodes v and c

Heapify(tree rooted at c)

Page 262: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Another Way to Analyze Complexity

● Heapify is often written as a recursive procedure.

● How can we analyze complexity of code like this?

262

Heapify(tree rooted at v)

if (v is bigger than its smallest child c)

swap values of nodes v and c

Heapify(tree rooted at c)

Page 263: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Basic Approach

● Suppose a recursive procedure runs in time T(n) on inputs

of size n.

● Procedure does work f(n), plus a recursive call on input of

size g(n) < n.

● Then we can write T(n) = T(g(n)) + f(n)

263

Page 264: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Basic Approach

● Suppose a recursive procedure runs in time T(n) on inputs

of size n.

● Procedure does work f(n), plus a recursive call on input of

size g(n) < n.

● Then we can write T(n) = T(g(n)) + f(n)

264

recurrence

for T(n)

Page 265: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Basic Approach

● Suppose a recursive procedure runs in time T(n) on inputs

of size n.

● Procedure does work f(n), plus a recursive call on input of

size g(n) < n.

● Then we can write T(n) = T(g(n)) + f(n)

265

recurrence

for T(n)

Let’s apply this approach to the analysis of heapify

Page 266: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider Heapify(r) on a tree of n nodes

266

r

Page 267: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

267

r

Heapify first

spends a constant amount of time arranging

a swap among these nodes

Consider Heapify(r) on a tree of n nodes

Page 268: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

268

r

And then acts

recursively on one subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

Consider Heapify(r) on a tree of n nodes

Page 269: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

269

r

And then acts

recursively on one subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

Consider Heapify(r) on a tree of n nodes

Worst-case assumption:

it’s the larger subtree

Page 270: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

270

r

And then acts

recursively on larger subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

Consider Heapify(r) on a tree of n nodes

k

Page 271: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

271

r

And then acts

recursively on larger subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

How many nodes?

Consider Heapify(r) on a tree of n nodes

k

Page 272: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

272

r

And then acts

recursively on larger subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

How many nodes?

Consider Heapify(r) on a tree of n nodes

Time to process n nodes:

T(n) = T(?) + k

k

Page 273: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

273

r

And then acts

recursively on larger subtree

Heapify first

spends a constant amount of time arranging

a swap among these nodes

How many nodes?

Consider Heapify(r) on a tree of n nodes

Time to process n nodes:

T(n) = T(?) + k

k

Page 274: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

274

r

And then acts

recursively on larger subtree

How many nodes?

Consider Heapify(r) on a tree of n nodes

Time to process n nodes:

T(n) = T(?) + k

Page 275: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

275

r

How many nodes?

Consider Heapify(r) on a tree of n nodes

How big could the shaded

tree be compared to the original one?

Page 276: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

276

r

How many nodes?

Consider Heapify(r) on a tree of n nodes

How big could the shaded

tree be compared to the original one?

CLRS: ≤ 2n/3

Page 277: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

277

r

How many nodes?

Consider Heapify(r) on a tree of n nodes

Let’s prove it….

(proof is not in our text)

How big could the shaded

tree be compared to the original one?

CLRS: ≤ 2n/3

Page 278: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

278

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Page 279: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

279

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Page 280: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

280

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Complete binary tree

of height h has2h+1-1nodes

(Theorem from earlier)

Page 281: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

281

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Complete binary tree

of height h has2h+1-1nodes

(Theorem)

And it has 2h leaves

(Lemma)

Page 282: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

282

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Complete binary tree

of height h has2h+1-1nodes

(Theorem)

And it has 2h leaves

(Lemma)

So this is a complete

tree except for 2h/2 = 2h-1 nodes

Page 283: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

283

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Complete binary tree

of height h has2h+1-1nodes

(Theorem)

And it has 2h leaves

(Lemma)

So this is a complete

tree except for 2h/2 = 2h-1 nodes

So this tree has

2h+1- 1 - 2h-1 nodes

Page 284: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

284

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

Complete binary tree

of height h has2h+1-1nodes

(Theorem)

And it has 2h leaves

(Lemma)

So this is a complete

tree except for 2h/2 = 2h-1 nodes

So this tree has

2h+1- 1 - 2h-1 nodes

#nodes in this tree:

Page 285: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

285

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h

#nodes in this tree:

Page 286: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

286

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h-1

#nodes in this tree:

How many nodes?

Page 287: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

287

r

Consider Heapify(r) on a tree of n nodes

Tree of

height h-1

#nodes in this tree:

How many nodes?

#nodes in

shaded tree

Page 288: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

288

#nodes in this tree:

#nodes in

shaded tree

Page 289: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

289

#nodes in this tree:

#nodes in

shaded tree

Page 290: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

290

#nodes in this tree:

#nodes in

shaded tree

Page 291: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

291

#nodes in this tree:

#nodes in

shaded tree

Page 292: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

292

#nodes in this tree:

#nodes in

shaded tree

Page 293: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

293

#nodes in this tree:

#nodes in

shaded tree

Page 294: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

294

#nodes in this tree:

#nodes in

shaded tree

Page 295: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

295

#nodes in this tree:

#nodes in

shaded tree

Page 296: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

296

#nodes in this tree:

#nodes in

shaded tree

Page 297: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Let’s compute the ratio

297

#nodes in this tree:

#nodes in

shaded tree

#nodes in

shaded tree#nodes in the larger tree

Page 298: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

298

r

Consider Heapify(r) on a tree of n nodesT(n) = time spent on n nodes

Page 299: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

299

r

Consider Heapify(r) on a tree of n nodes

some constant time k spent on these 3 nodes

T(n) = time spent on n nodes

=

Page 300: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

300

r

How many nodes?

Consider Heapify(r) on a tree of n nodes

+ time spent

on this subtree

some constant time k spent on these 3 nodes

T(n) = time spent on n nodes

=

Page 301: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

301

r

How many nodes?

Consider Heapify(r) on a tree of n nodes

+ T(2n/3)

some constant time k spent on these 3 nodes

T(n) = time spent on n nodes

=(worst case)

Page 302: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● T(n) =○ k constant time spent on the top 3 nodes

○ + T(2n/3)

● T(n) = T(2n/3) + k

302

Consider Heapify(r) on a tree of n nodes

Page 303: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● T(n) =○ k constant time spent on the top 3 nodes

○ + T(2n/3)

● T(n) = T(2n/3) + k

303

Consider Heapify(r) on a tree of n nodes

T(100) = T(66) + k

= T(44) + k + k= T(29) + k + k + k= T(19) + k + k + k + k

= T(12) + k + k + k + k + k= T(8) + k + k + k + k + k + k

= T(5) + k + k + k + k + k + k + k= T(3) + k + k + k + k + k + k + k + k= T(2) + k + k + k + k + k + k + k + k + k

= T(1) + k + k + k + k + k + k + k + k + k + k= 0 + k + k + k + k + k + k + k + k + k + k = 10 k

Page 304: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● T(n) =○ k constant time spent on the top 3 nodes

○ + T(2n/3)

● T(n) = T(2n/3) + k

304

Consider Heapify(r) on a tree of n nodes

T(100) = T(66) + k

= T(44) + k + k= T(29) + k + k + k= T(19) + k + k + k + k

= T(12) + k + k + k + k + k= T(8) + k + k + k + k + k + k

= T(5) + k + k + k + k + k + k + k= T(3) + k + k + k + k + k + k + k + k= T(2) + k + k + k + k + k + k + k + k + k

= T(1) + k + k + k + k + k + k + k + k + k + k= 0 + k + k + k + k + k + k + k + k + k + k = 10 k

If the size of our

problem is multiplied by 1.5, to get T(150), it takes just one more

step, so T(150) = 11 k

Page 305: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● T(n) =○ k constant time spent on the top 3 nodes

○ + T(2n/3)

● T(n) = T(2n/3) + k

305

Consider Heapify(r) on a tree of n nodes

T(100) = T(66) + k

= T(44) + k + k= T(29) + k + k + k= T(19) + k + k + k + k

= T(12) + k + k + k + k + k= T(8) + k + k + k + k + k + k

= T(5) + k + k + k + k + k + k + k= T(3) + k + k + k + k + k + k + k + k= T(2) + k + k + k + k + k + k + k + k + k

= T(1) + k + k + k + k + k + k + k + k + k + k= 0 + k + k + k + k + k + k + k + k + k + k = 10 k

If the size of our

problem is multiplied by 1.5, to get T(150), it takes just one more

step, so T(150) = 11 k

We will be able to show soon that this T(n) = Θ(log n)

Page 306: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● T(n) =○ k constant time spent on the top 3 nodes

○ + T(2n/3)

● T(n) = T(2n/3) + k

● [ magic we have not yet studied but will do so next week ]

● T(n) = Θ(log n)

● Same asymptotic result as we got the other way

● Approach applies to many recursive procedures, as we’ll see

306

Consider Heapify(r) on a tree of n nodes

Page 307: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Summary of Binary Heap Performance

● Decrease: worst-case Θ(log n)

● Insert: worst-case Θ(log n) [reduction from decrease]

● ExtractMin: worst-case Θ(log n)

307

Page 308: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Summary of Binary Heap Performance

● Decrease: worst-case Θ(log n)

● Insert: worst-case Θ(log n) [reduction from decrease]

● ExtractMin: worst-case Θ(log n)

308

Moral: we can dynamically maintain the minimum

of a binary heap in time Θ(log n) per operation.

Page 309: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Follow-up: Time To Build Heap

● What does it cost to do n successive insertions into an empty heap?

309

Page 310: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Follow-up: Time To Build Heap

● What does it cost to do n successive insertions into an empty heap?

● k log(1) + k log(2) + …. + k log(n)

310

Page 311: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Follow-up: Time To Build Heap

● What does it cost to do n successive insertions into an empty heap?

● k log(1) + k log(2) + …. + k log(n)

● ≤ k log(n) + k log(n) + …. + k log(n) = kn log(n)

311

Page 312: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Follow-up: Time To Build Heap

● What does it cost to do n successive insertions into an empty heap?

● k log(1) + k log(2) + …. + k log(n)

● ≤ k log(n) + k log(n) + …. + k log(n) = kn log(n)

● So building a heap takes worst-case time O(n log n)

312

Page 313: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Follow-up: Time To Build Heap

● What does it cost to do n successive insertions into an empty heap?

● k log(1) + k log(2) + …. + k log(n)

● ≤ k log(n) + k log(n) + …. + k log(n) = kn log(n)

● So building a heap takes worst-case time O(n log n)

● (But in fact, this O is not a Θ – see text for better bound!)

313

Page 314: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Practical advice: do

not actually store your

binary heap as a tree!

314

Page 315: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Efficient representation of binary heap

● Binary heaps have so far been depicted as trees○ And we could implement them that way

○ But there is a more efficient treatment

○ Motivated by

■ Max size is known a priori

■ Elements are always added to the end for insert(T thing)

■ In response to extractMin(), heapify() removes the last element

● So an array is actually a good way to store a tree○ But how do we keep track of

■ parents

■ children

● Easy solution to that for a binary tree

315

Page 316: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● An important implementation note○ Java arrays

■ Start at 0

316

Page 317: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● An important implementation note○ Java arrays

■ Start at 0

■ So new int[10]

317

Page 318: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● An important implementation note○ Java arrays

■ Start at 0

■ So new int[10]

● Provides for 10 integer locations

● Numbered 0….9

● We could start filling in the array at 0

● But for the purposes of the binary heap we will start at 1

○ The text does it this way, so we’ll be consistent with it.

○ The math that follows is very slightly easier starting with 1

○ Older programming languages started arrays at 1 (some, like Matlab, still do)

318

0 1 2 3 5 6 7 84 9

Page 319: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● An important implementation note○ Java arrays

■ Start at 0

■ So new int[10]

● Provides for 10 integer locations

● Numbered 0….9

● We could start filling in the array at 0

● But for the purposes of the binary heap we will start at 1

○ The text does it this way, so we’ll be consistent with it.

○ The math that follows is very slightly easier starting with 1

○ Older programming languages started arrays at 1 (some, like Matlab, still do)

319

0 1 2 3 5 6 7 84 9

We ignore location 0

even though it’s there

Page 320: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● It’s a binary tree○ So each node has at most 2 children

● It’s compact○ So it’s predictable where childless parents will appear

■ Near the end

320

1 2 3 5 6 7 84 9

Page 321: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

321

1 2 3 5 6 7 84 9

Page 322: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

322

1 2 3 5 6 7 84 9

r

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

Page 323: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

323

1 2 3 5 6 7 84 9

r

p

a b

Page 324: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i

324

1 2 3 5 6 7 84 9

r

p

a b

p

Page 325: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

325

1 2 3 5 6 7 84 9

r

p

a b

p

Page 326: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

326

1 2 3 5 6 7 84 9

r

p

a b

p a

Page 327: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

○ The right child is stored at 2 x i + 1

327

1 2 3 5 6 7 84 9

r

p

a b

p a

Page 328: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

○ The right child is stored at 2 x i + 1

328

1 2 3 5 6 7 84 9

r

p

a b

p ba

Page 329: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

○ The right child is stored at 2 x i + 1

● For every node n except the root○ The parent of n is at

329

1 2 3 5 6 7 84 9

r

p

a b

p ba

Page 330: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree implemented as an array

● So we will store the tree in an array

● How do we infer the relationship○ Between a parent and its children

○ Between a child and its parent

● The root will always be stored at 1

● Given a parent node p○ Left child a

○ Right child b

● If p is stored at index i○ The left child is stored at 2 x i

○ The right child is stored at 2 x i + 1

● For every node n except the root○ The parent of n is at

330

1 2 3 5 6 7 84 9

r

p

a b

p b

This is the

result you get from normal int

division:

6/2 = 37/2 = 3

a

Page 331: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Back to our example, but using an array

331

Page 332: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Consider the following values in a heap

87 91 31 17 46 77 79 4 58

332

Page 333: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

333

1 2 3 5 6 7 84 9

Page 334: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

334

4

1 2 3 5 6 7 84 9

4

Page 335: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

335

17

4

1 2 3 5 6 7 84 9

4 17

Page 336: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

336

17 77

4

1 2 3 5 6 7 84 9

4 17 77

Page 337: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

337

31

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31

Page 338: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

338

31 46

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31 46

Page 339: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

339

31 46 87

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31 46 87

Page 340: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

340

31 46 87 79

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31 46 87 79

Page 341: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

341

91

31 46 87 79

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31 46 87 79 91

Page 342: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Tree stored as an array

342

91 58

31 46 87 79

17 77

4

1 2 3 5 6 7 84 9

4 17 77 31 46 87 79 91 58

Page 343: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Lab 3: Implement Heap

343

Page 344: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

344

Page 345: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

For studio

345

Page 346: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Some possible implementations

● Let’s think through some implementation possibilities○ Using data structures we already know

○ Reasoning about their complexity

● The “n” here○ Means the current size of our priority queue

● Given a priority queue of n items○ How expensive is each of the methods we have described so far

○ For a particular implementation

■ Binary heap

■ List

● Links vs. array

● Ordered vs. not ordered

346

Page 347: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

● Table will track complexity

● We are interested in worst-case times○ We’ll come back to this shortly

347

Implementation insert extractMin

Unordered list

Ordered list

Unordered array

Ordered array

Page 348: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

348

53 92 46this

Implementation insert extractMin

Unordered list

Ordered list

Unordered array

Ordered array

Page 349: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

349

84 53 92 46this

Implementation insert extractMin

Unordered list

Ordered list

Unordered array

Ordered array

Page 350: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

350

84 53 92 46this

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 351: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 84

351

84 53 92 46this

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 352: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 84

352

84 53 92 46this

p

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 353: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 53

353

84 53 92 46this

p

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 354: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 53

354

84 53 92 46this

p

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 355: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

355

84 53 92 46this

p

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

Page 356: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

356

84 53 92 46this

Implementation insert extractMin

Unordered list Θ(1)

Ordered list

Unordered array

Ordered array

p

Page 357: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

357

84 53 92 46this

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list

Unordered array

Ordered array

p

Page 358: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

358

46 53 92this

p

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list

Unordered array

Ordered array

Page 359: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

359

46 53 92this

p

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list

Unordered array

Ordered array

Page 360: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

360

46 53 92this

p

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Page 361: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

361

46 53 92this

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

84

Wait! Is this right? Look

at what happens next if we wanted to insert “1” into the ordered list

Page 362: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

362

46 53 92this

84

It would go here,

seemingly taking constant time!

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Wait! Is this right? Look

at what happens next if we wanted to insert “1” into the ordered list...

Page 363: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

363

46 53 92this

84

Wait! Is this right? Look

at what happens next if we wanted to insert “1” into the ordered list...

It would go here,

seemingly taking constant time!

Remember we set out to analyze

the complexity of the worst-case. That complexity is as shown here, bound above and below by n

Page 364: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

● Many of us confuse○ Best vs. worst case

○ vs.

364

Page 365: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

● Many of us confuse○ Best vs. worst case

○ vs.

● To avoid this○ First think about the function f(n) that characterizes the property of interest

■ Worst-case

■ Best-case

■ Average-case

365

Page 366: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

● Many of us confuse○ Best vs. worst case

○ vs.

● To avoid this○ First think about the function f(n) that characterizes the property of interest

■ Worst-case

■ Best-case

■ Average-case

○ Then think about whether that f(n) is bounded

366

Page 367: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

● Many of us confuse○ Best vs. worst case

○ vs.

● To avoid this○ First think about the function f(n) that characterizes the property of interest

■ Worst-case

■ Best-case

■ Average-case

○ Then think about whether that f(n) is bounded

■ From above

367

Page 368: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

368

● Many of us confuse○ Best vs. worst case

○ vs.

● To avoid this○ First think about the function f(n) that characterizes the property of interest

■ Worst-case

■ Best-case

■ Average-case

○ Then think about whether that f(n) is bounded

■ From above

■ From below

Page 369: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

A common source of confusion

● Many of us confuse○ Best vs. worst case

○ vs.

● To avoid this○ First think about the function f(n) that characterizes the property of interest

■ Worst-case

■ Best-case

■ Average-case

○ Then think about whether that f(n) is bounded

■ From above

■ From below

■ Both

369

Page 370: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

370

46 53 92this

84

Wait! Is this right? Look

at what happens next if we wanted to insert “1” into the ordered list…

It would go here,

seemingly taking constant time!

Remember we set out to analyze

the complexity of the worst-case. That complexity is as shown here, bound above and below by n

Page 371: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

371

46 53 92this

84

Just be sure, why is

this right?

Page 372: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

372

46 53 92this

84

Just be sure, why is

this right?

And this?

Page 373: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

373

46 53 92this

84

Page 374: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

extractMin()

374

46 53 92this

84

Page 375: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

extractMin() 46

375

46 53 92this

84

Page 376: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n)

Unordered array

Ordered array

Running example

PQ contains 53, 92, 46

extractMin() 46

376

53 9284this

Page 377: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

PQ contains 53, 92, 46

extractMin() 46

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Running example

377

53 92this

84

Page 378: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

What about arrays?

378

Page 379: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

379

size = 0

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Page 380: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

380

53 92 46

size = 3

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Page 381: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

381

53 92 46

size = 3

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Page 382: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

382

53 92 46

size = 3

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Page 383: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

383

53 92 46

size = 4

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array

Ordered array

Page 384: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

384

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 385: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin()

385

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 386: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin()

386

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 387: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 53

387

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 388: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 53

388

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 389: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

389

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 390: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

390

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 391: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

391

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1)

Ordered array

Page 392: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

392

53 92 46 84

size = 4

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 393: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

393

size = 0

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 394: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

394

46 53 92

size = 3

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 395: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

395

46 53 92

size = 3

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 396: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

396

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 397: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

397

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 398: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

398

46 53 92

size = 3

1. Find where 84

should go2. Move elements

to make room for

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 399: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

399

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 400: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

400

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 401: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

401

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 402: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

402

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

84

????

Page 403: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Running example

PQ contains 53, 92, 46

insert(84)

403

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

84

?????

Let’s look at this one step at a time

Page 404: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

404

46 53 92

size = 3

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 405: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

405

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 406: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

406

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 407: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

407

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

What say you?

Page 408: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

408

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

What say you? We could

look through the entire array O(n)

Page 409: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

409

46 53 92

size = 3

1. Find where 84

should go

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

What say you? We could

look through the entire array O(n) but there is a faster way, do you see?

Page 410: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

410

46 53 92

size = 3

1. Find where 84

should go

Remember phone books? To find somebody,

would you start at the first page and keep looking?

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

What say you? We could

look through the entire array O(n) but there is a faster way, do you see?

Page 411: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

411

46 53 92

size = 3

1. Find where 84

should go

Remember phone books? To find somebody,

would you start at the first page and keep looking? Or is there a faster way?

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

What say you? We could

look through the entire array O(n) but there is a faster way, do you see?

Page 412: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

One method for searching a phone book efficiently

● Look in the middle○ Is the target of your search later or earlier than what you find?

○ Throw away the half of the phone book that cannot contain your target

■ Really, throw it away, or shred it, or burn it

○ Repeat this procedure (recursively!) on the half you did not throw away

● We will soon study a general method to reason about this procedure’s

complexity

● But do you see what it is?○ Think about the size of what remains to be searched

○ When it reaches 1 you are done

● 8 → 4 → 2 → 1 3 steps log28

412

Page 413: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

One method for searching a phone book efficiently

● Look in the middle○ Is the target of your search later or earlier than what you find?

○ Throw away the half of the phone book that cannot contain your target

■ Really, throw it away, or shred it, or burn it

○ Repeat this procedure (recursively!) on the half you did not throw away

● We will soon study a general method to reason about this procedure’s

complexity

● But do you see what it is?○ Think about the size of what remains to be searched

○ When it reaches 1 you are done

● 8 → 4 → 2 → 1 3 steps log28

● Because these logarithms are so common, we abbreviate them using “lg”

413

Page 414: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

One method for searching a phone book efficiently

● Look in the middle○ Is the target of your search later or earlier than what you find?

○ Throw away the half of the phone book that cannot contain your target

■ Really, throw it away, or shred it, or burn it

○ Repeat this procedure (recursively!) on the half you did not throw away

● We will soon study a general method to reason about this procedure’s

complexity

● But do you see what it is?○ Think about the size of what remains to be searched

○ When it reaches 1 you are done

● 8 → 4 → 2 → 1 3 steps log28

● Because these logarithms are so common, we abbreviate them using “lg”

414

However, asymptotically the

base doesn’t matter, so we write O(log(n))

Page 415: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

415

46 53 92

size = 3

1. Find where 84

should go

Remember phone books? To find somebody,

would you start at the first page and keep looking? Or is there a faster way?

O(log(n))

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 416: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

416

46 53 92

size = 3

1. Find where 84

should go2. Move elements

to make room for

84

O(log(n))

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 417: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

417

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

84

O(log(n))

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 418: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

418

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

84

O(log(n))

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Here we only had to move one

element, but worst case is that they all have to shift right by one cell.

Page 419: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

419

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

84

O(log(n))

O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Here we only had to move one

element, but worst case is that they all have to shift right by one cell.

Page 420: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

420

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

O(log(n))

O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 421: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

421

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

O(log(n))

O(n)84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 422: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

422

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

O(log(n))

O(n)84

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 423: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

423

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

O(log(n))

O(n)84

O(1)

O(log(n)) + O(n) + O(1) = O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array

Page 424: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

insert(84)

424

46 53 92

size = 4

1. Find where 84

should go2. Move elements

to make room for

843. Insert 84

O(log(n))

O(n)84

O(1)

O(log(n)) + O(n) + O(1) = O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 425: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

425

46 53 92

size = 4

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 426: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin()

426

46 53 92

size = 4

84

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 427: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin()

427

46 53 92

size = 4

84

1. Capture the

array’s first element

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 428: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

428

46 53 92

size = 4

84

1. Capture the

array’s first element

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 429: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

429

46 53 92

size = 4

84

1. Capture the

array’s first element

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 430: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

430

46 53 92

size = 4

84

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 431: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

431

53 53 92

size = 4

84

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 432: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

432

53 84 92

size = 4

84

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 433: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

433

53 84 92

size = 4

92

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 434: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

434

53 84

size = 3

92

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 435: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

435

53 84

size = 3

92

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 436: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

436

53 84

size = 3

92

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

O(n)

O(n) + O(1) = O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n)

Page 437: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

PQ contains 53, 92, 46

extractMin() 46

437

53 84

size = 3

92

1. Capture the

array’s first element

2. Move all

elements one cell to the left

O(1)

O(n)

O(n) + O(1) = O(n)

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n) Θ(n)

Page 438: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Running example

438

53 84

size = 3

92

Do you see how to modify

Ordered array so that extractMin() can be done in constant time?

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n) Θ(n)

Page 439: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

● Not so great○ Lists

○ Arrays

● Much better○ Use a heap

○ A kind of a tree

○ Implemented using an array

○ Provides O(log(n)) time bound on all operations

■ O(1) peek at minimum element

Running example

439

Implementation insert extractMin

Unordered list Θ(1) Θ(n)

Ordered list Θ(n) Θ(1)

Unordered array Θ(1) Θ(n)

Ordered array Θ(n) Θ(n)

Page 440: Lecture 3: Priority Queues, and a Tree Grows in 247Lecture 3: Priority Queues, and a Tree Grows in 247 1 These slides include material originally prepared by Dr.RonCytron, Dr. Jeremy

Enrichment

https://xkcd.com/835/

● Don’t forget to read the mouseover text

440