Heaps and Heapsort - Computer Science and...

48
Heaps and Heapsort 6 May 2013 OSU CSE 1

Transcript of Heaps and Heapsort - Computer Science and...

Page 1: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heaps and Heapsort

6 May 2013 OSU CSE 1

Page 2: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heaps

•  A heap is a binary tree of T that satisfies two properties: – Global shape property: it is a complete

binary tree – Local ordering property: the label in each

node is “smaller than or equal to” the label in each of its child nodes

6 May 2013 OSU CSE 2

Page 3: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heaps

•  A heap is a binary tree of T that satisfies two properties: – Global shape property: it is a complete

binary tree – Local ordering property: the label in each

node is “smaller than or equal to” the label in each of its child nodes

6 May 2013 OSU CSE 3

A complete binary tree is one in which all levels are “full” except possibly the bottom level, with any nodes on the bottom level as far left as possible.

Page 4: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heaps

•  A heap is a binary tree of T that satisfies two properties: – Global shape property: it is a complete

binary tree – Local ordering property: the label in each

node is “smaller than or equal to” the label in each of its child nodes

6 May 2013 OSU CSE 4

Also in the picture is (as with BSTs, sorting, etc.) a total preorder that

makes this notion precise.

Page 5: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Simplification

•  For simplicity in the following illustrations, we use only one kind of example: – T = integer – The ordering is ≤

•  Because heaps are used in sorting, where duplicate values may be involved, we allow that multiple nodes in a heap may have the same labels (i.e., we will not assume that the labels are unique)

6 May 2013 OSU CSE 5

Page 6: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

The Big Picture

6 May 2013 OSU CSE 6

x

Page 7: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

The Big Picture

6 May 2013 OSU CSE 7

y

x

This tree’s root label y satisfies

x ≤ y

Page 8: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

The Big Picture

6 May 2013 OSU CSE 8

y

x

Observe: This tree is also a heap; and for each node in this tree with

label z, we have: x ≤ y ≤ z.

Page 9: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

The Big Picture

6 May 2013 OSU CSE 9

y

x

This tree’s root label y satisfies

x ≤ y

Page 10: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

The Big Picture

6 May 2013 OSU CSE 10

y

x

Observe: This tree is also a heap; and for each node in this tree with

label z, we have: x ≤ y ≤ z.

Page 11: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Examples of Heaps

6 May 2013 OSU CSE 11

5

2

1

8

7

8 2

1

1

8

Page 12: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Non-Examples of Heaps

6 May 2013 OSU CSE 12

8 1

2

1

5

1

2

8

2

1

1

5

Page 13: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Non-Examples of Heaps

6 May 2013 OSU CSE 13

8 1

2

1

5

1

2

8

2

1

1

5 Shape property is violated here: not all nodes at the bottom level are as

far left as possible.

Page 14: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Non-Examples of Heaps

6 May 2013 OSU CSE 14

8 1

2

1

5

1

2

8

2

1

1

5 Ordering property is violated here: value is out of order with that of its

right child.

Page 15: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Non-Examples of Heaps

6 May 2013 OSU CSE 15

8 1

2

1

5

1

2

8

2

1

1

5 Shape property is violated: two levels are not “full”.

Page 16: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heapsort

•  A heap can be used to represent the values in a SortingMachine, as follows: –  In changeToExtractionMode, arrange all

the values into a heap –  In removeFirst, remove the root, and adjust

the slightly mutilated heap to make it a heap again

6 May 2013 OSU CSE 16

Page 17: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Heapsort

•  A heap can be used to represent the values in a SortingMachine, as follows: –  In changeToExtractionMode, arrange all

the values into a heap –  In removeFirst, remove the root, and adjust

the slightly mutilated heap to make it a heap again

6 May 2013 OSU CSE 17

Why should this work?

Page 18: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

How removeFirst Can Work

•  If the root is the only node in the heap, then after removing it, what remains is already a heap; nothing left to do

•  If the root is not the only node, then removing it leaves an “opening” that must be filled by moving some other value in the heap into the opening

6 May 2013 OSU CSE 18

Page 19: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

How removeFirst Can Work

•  If the root is the only node in the heap, then after removing it, what remains is already a heap; nothing left to do

•  If the root is not the only node, then removing it leaves an “opening” that must be filled by moving some other value in the heap into the opening

6 May 2013 OSU CSE 19

The question is: which one?

Page 20: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 20

4 3

2

1

5

Page 21: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 21

4 3

2 5

If we remove the root, leaving this

opening ...

Page 22: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 22

4 3

5

... we might move up the smaller

child ...

2

Page 23: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 23

4 3

5

... now creating another

opening ...

2

Page 24: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 24

4

5

... so, we might move up the smaller child.

2

3

Page 25: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A First Attempt

6 May 2013 OSU CSE 25

4

5

Is the result necessarily a

heap?

2

3

Page 26: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 26

4 3

2

1

5

Page 27: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 27

4 3

2 5

This time, let’s maintain the

shape property ...

Page 28: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 28

4

2 5

... by promoting the last node on the bottom level.

3

Page 29: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 29

4

2 5

Now, we can “sift down” the root into its proper place ...

3

Page 30: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 30

4

3 5

... by swapping it with its smaller

child ...

2

Page 31: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 31

4

3 5

2

... and then “sifting down” the root of

that subtree.

Page 32: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example: A Second Attempt

6 May 2013 OSU CSE 32

4

3 5

2

Is the result necessarily a

heap?

Page 33: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Pseudo-Contract /** * Restores a complete binary tree to be a heap

* if only the root might be out of place. * @updates t * @requires * [t is a complete binary tree] and * [both subtrees of the root of t are heaps]

* @ensures * [t is a heap with the same values as #t] */

public static void siftDown (BinaryTree<T> t) {...}

6 May 2013 OSU CSE 33

Page 34: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Building a Heap In the First Place

•  Suppose we have n values in a complete binary tree, but they are arranged without regard to the heap ordering

•  How can we “heapify” it?

6 May 2013 OSU CSE 34

Page 35: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Pseudo-Contract /** * Makes a complete binary tree into a heap.

* @updates t * @requires * [t is a complete binary tree]

* @ensures * [t is a heap with the same values as #t]

*/ public static void heapify (BinaryTree<T> t) {...}

6 May 2013 OSU CSE 35

Page 36: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Hint

•  To see how you might implement heapify, compare the contracts of siftDown and heapify

•  The only difference: before we can call siftDown to make a heap, both subtrees of the root must already be heaps – Once they are heaps, just a call to siftDown

will finish the job

6 May 2013 OSU CSE 36

Page 37: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Hint

•  To see how you might implement heapify, compare the contracts of siftDown and heapify

•  The only difference: before we can call siftDown to make a heap, both subtrees of the root must already be heaps – Once they are heaps, just a call to siftDown

will finish the job

6 May 2013 OSU CSE 37

How do we make the subtrees into

heaps?

Page 38: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example

6 May 2013 OSU CSE 38

1 4

5

3

2

Page 39: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example

6 May 2013 OSU CSE 39

5 4

1

3

2

First, recursively “heapify” the left

subtree.

Page 40: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example

6 May 2013 OSU CSE 40

5 4

1

3

2

Then, recursively “heapify” the right

subtree.

Page 41: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Example

6 May 2013 OSU CSE 41

5 4

1

3

2

Then “sift down” the root, because now only the root might

be out of place.

Page 42: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Embedding a Heap in an Array

•  While one could represent a heap using a BinaryTree<T> (as suggested in the pseudo-contracts above), it is generally not done this way

•  Instead, a heap is usually represented “compactly” using an Array<T>

6 May 2013 OSU CSE 42

Page 43: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Interpreting an Array as a Heap

6 May 2013 OSU CSE 43

40 30

20

10

50

Page 44: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

6 May 2013 OSU CSE 44

40 30

20

10

50

4 3

1 2

0

Interpreting an Array as a Heap Because it’s a complete

binary tree, the nodes can be numbered top-to-

bottom, left-to-right.

Page 45: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

6 May 2013 OSU CSE 45

40 30

20

10

50

4 3

1 2

0

10 20 50 40 30

0 1 2 3 4

Page 46: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

6 May 2013 OSU CSE 46

40 30

20

10

50

4 3

1 2

0

10 20 50 40 30

0 1 2 3 4

At what index in the Array is the left child of the node at index i?

Page 47: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

6 May 2013 OSU CSE 47

40 30

20

10

50

4 3

1 2

0

10 20 50 40 30

0 1 2 3 4

At what index in the Array is the right child of the node at index i?

Page 48: Heaps and Heapsort - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/14... · Heaps and Heapsort 6 May 2013! OSU CSE! 1! Heaps • A heap

Resources

•  Wikipedia: Heapsort –  http://en.wikipedia.org/wiki/Heapsort

•  Wikipedia: Heap (data structure) –  http://en.wikipedia.org/wiki/Heap_(data_structure)

•  Big Java Late Objects, Section 17.6 –  http://proquest.safaribooksonline.com.proxy.lib.ohio-state.edu/book/

programming/java/9781118087886/chapter-17-tree-structures/navpoint-139

•  Big Java Late Objects, Section 17.7 –  http://proquest.safaribooksonline.com.proxy.lib.ohio-state.edu/book/

programming/java/9781118087886/chapter-17-tree-structures/navpoint-140

6 May 2013 OSU CSE 48