More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September...

Post on 02-Oct-2020

0 views 0 download

Transcript of More About Heapsweb.cse.ohio-state.edu/.../14a.More-About-Heaps.pdfMore About Heaps. 5 September...

More About Heaps

5 September 2020 OSU CSE 1

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

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

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?

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

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

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

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!

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

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.

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.

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.

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.

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.

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

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

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.

Commutative Diagram

5 September 2020 OSU CSE 18

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

add(7)

Commutative Diagram

5 September 2020 OSU CSE 19

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

add(7)

Does this concrete value satisfy the

representation invariant?

Commutative Diagram

5 September 2020 OSU CSE 20

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

add(7)

What abstract value does it represent?

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?

Commutative Diagram

5 September 2020 OSU CSE 22

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

add(7)

What concrete value represents that abstract

value?

Commutative Diagram

5 September 2020 OSU CSE 23

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

add(7)

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

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

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

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.

Example: |t| > 1

5 September 2020 OSU CSE 27

y

x

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.

Example: |t| > 1

5 September 2020 OSU CSE 29

y

x

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.