more about heapsweb.cse.ohio-state.edu/.../14a.more-about-heaps.pdfmore about heaps. 5 september...

30
More About Heaps 5 September 2020 OSU CSE 1

Upload: others

Post on 02-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

More About Heaps

5 September 2020 OSU CSE 1

Page 2: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Complete Binary Tree As Array• To use an array to represent a complete

binary tree, you need more information:– An int index of the root of the tree of interest

• There might be many subtrees inside the tree rooted at index 0, and you might want to do something to these subtrees as well as to the whole tree

– An int index of the last node of the whole tree• You need to know which entries in the array contain

tree node labels; entries at index 0 through the last index are useful, but others are “junk”

5 September 2020 OSU CSE 2

Page 3: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 3

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

Page 4: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 4

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

? ? ?

5 6 7

Note that the array might be larger than the tree

now, even if at one point they were the same size. How could this happen?

Page 5: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 5

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

? ? ?

5 6 7

For the gray tree:

top = 0last = 4

Page 6: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 6

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

? ? ?

5 6 7

For the pink tree:

top = 1last = 4

Page 7: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 7

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

? ? ?

5 6 7

For the blue tree:

top = 2last = 4

Page 8: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

5 September 2020 OSU CSE 8

40 30

20

10

50

43

1 2

0

10 20 50 40 30

0 1 2 3 4

? ? ?

5 6 7

Note that all the entries in a given subtree (e.g., blue here) are in positions top through last of the array—but not all

entries of the array in that range are in the subtree!

Page 9: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 9

Page 10: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 10

This instance variable is set by the constructor,

and does not change; it determines the ordering

of T values.

Page 11: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 11

This instance variable records whether this is

in insertion mode.

Page 12: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 12

This instance variable holds the entries of this while it is in insertion mode.

Page 13: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 13

This instance variable holds the entries of this while it is in extraction mode.

Page 14: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Representation

private Comparator<T> machineOrder;

private boolean insertionMode;

private Queue<T> entries;

private T[] heap;

private int heapSize;

5 September 2020 OSU CSE 14

This instance variable is 0 while this is in

insertion mode; it holds the number of entries of

this while it is in extraction mode.

Page 15: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Correspondence

/**

* @correspondence* if $this.insertionMode then* this = (true, $this.machineOrder,* multiset_entries($this.entries))* else* this = (false, $this.machineOrder,* multiset_entries(* $this.heap[0, $this.heapSize)))*/

5 September 2020 OSU CSE 15

Page 16: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Convention/*** @convention* if $this.insertionMode then* $this.heapSize = 0* else* $this.entries = < > and* for all i: integer* where (0 <= i and i < |$this.heap|)* ([entry at position i in $this.heap* is not null]) and* SUBTREE_IS_HEAP($this.heap, 0,* $this.heapSize – 1, [relation computed by* $this.machineOrder.compare method]) and* 0 <= $this.heapSize <= |$this.heap|*/

5 September 2020 OSU CSE 16

Page 17: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

SortingMachine5a Convention/*** @convention* if $this.insertionMode then* $this.heapSize = 0* else* $this.entries = < > and* for all i: integer* where (0 <= i and i < |$this.heap|)* ([entry at position i in $this.heap* is not null]) and* SUBTREE_IS_HEAP($this.heap, 0,* $this.heapSize – 1, [relation computed by* $this.machineOrder.compare method]) and* 0 <= $this.heapSize <= |$this.heap|*/

5 September 2020 OSU CSE 17

This mathematical definition (not shown on slides) means what you

should think it does.

Page 18: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 18

(≤, true, <3, 1>, ?, 0)

add(7)

Page 19: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 19

(≤, true, <3, 1>, ?, 0)

add(7)

Does this concrete value satisfy the

representation invariant?

Page 20: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 20

(≤, true, <3, 1>, ?, 0)

add(7)

What abstract value does it represent?

Page 21: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 21

(≤, true, <3, 1>, ?, 0)

add(7)

What does the contract say the abstract result of this method call will be?

Page 22: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 22

(≤, true, <3, 1>, ?, 0)

add(7)

What concrete value represents that abstract

value?

Page 23: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Commutative Diagram

5 September 2020 OSU CSE 23

(≤, true, <3, 1>, ?, 0)

add(7)

What method body would do this (for anyvalid starting point)?

Page 24: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Another Useful Pseudo-Contract/**

* Reports whether a complete binary tree is a

* heap.

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

* @ensures* isHeap = [t is a heap]

*/

public static boolean isHeap(BinaryTree<T> t) {...

}

5 September 2020 OSU CSE 24

Page 25: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Implementing isHeap

• If |t| ≤ 1 then t is a heap• If |t| > 1 then t is a heap iff:

– The root is ≤ the root of the left subtree– The left subtree is a heap– The root is ≤ the root of the right subtree (if

any)– The right subtree (if any) is a heap

5 September 2020 OSU CSE 25

Page 26: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Implementing isHeap

• If |t| ≤ 1 then t is a heap• If |t| > 1 then t is a heap iff:

– The root is ≤ the root of the left subtree– The left subtree is a heap– The root is ≤ the root of the right subtree (if

any)– The right subtree (if any) is a heap

5 September 2020 OSU CSE 26

A smaller subproblem of the same kind; a hint to

use recursion.

Page 27: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Example: |t| > 1

5 September 2020 OSU CSE 27

y

x

Page 28: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Example: |t| > 1

5 September 2020 OSU CSE 28

y

x

Is x ≤ y?If not, then t is not

a heap.If so, then check

whether this subtree is a heap.

Page 29: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Example: |t| > 1

5 September 2020 OSU CSE 29

y

x

Page 30: More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September 2020 OSU CSE 1. Complete Binary Tree As Array • To use an array to represent a complete

Example: |t| > 1

5 September 2020 OSU CSE 30

y

x

Is x ≤ y?If not, then t is not

a heap.If so, then check

whether this subtree is a heap.