CSE 2231 -...

35
BinaryTree 8 February 2019 OSU CSE 1

Transcript of CSE 2231 -...

Page 1: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

BinaryTree

8 February 2019 OSU CSE 1

Page 2: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

BinaryTree

• The BinaryTree component family allows you to manipulate values modeled as mathematical binary trees with any label type T (i.e., binary tree of T)– Another generic type like Sequence and Set

8 February 2019 OSU CSE 2

Page 3: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Interfaces and Classes

BinaryTree-Kernel

extends

Standard

extends

8 February 2019 OSU CSE 3

BinaryTree

implements

BinaryTree1

Iterable

extends

Page 4: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Interfaces and Classes

BinaryTree-Kernel

extends

Standard

extends

8 February 2019 OSU CSE 4

BinaryTree

implements

BinaryTree1

Iterable

extends

BinaryTreeKernelhas contracts for three methods:assemble

disassemblesize

Page 5: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Interfaces and Classes

BinaryTree-Kernel

extends

Standard

extends

8 February 2019 OSU CSE 5

BinaryTree

implements

BinaryTree1

Iterable

extends

BinaryTree has contracts for four

methods (the last of which we will skip):

rootreplaceRoot

heightinOrderAssemble

Page 6: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Interfaces and Classes

BinaryTree-Kernel

extends

Standard

extends

8 February 2019 OSU CSE 6

BinaryTree

implements

BinaryTree1

Iterable

extends

There is really an abstract class as usual in the chain

here, but it is not shown because these slides

describe the client view, and a client needs only interface BinaryTree and class

BinaryTree1.

Page 7: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Mathematical Modeltype BinaryTreeKernel is modeled bybinary tree of T

8 February 2019 OSU CSE 7

Page 8: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

No-argument Constructor

• Ensures:this = empty_tree

8 February 2019 OSU CSE 8

Page 9: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 9

Code State

BinaryTree<NaturalNumber> bn =new BinaryTree1<>();

Page 10: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 10

Code State

BinaryTree<NaturalNumber> bn =new BinaryTree1<>();

bn =

Page 11: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

assemblevoid assemble(T root, BinaryTree<T> left,

BinaryTree<T> right)

• Assembles in this a binary tree with root label rootand subtrees left and right; the declaration notwithstanding, the dynamic type of left and rightmust be the same as the dynamic type of this.

• Aliases: reference root• Replaces: this• Clears: left, right• Ensures:

this = compose(root, #left, #right)

8 February 2019 OSU CSE 11

Page 12: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 12

Code Statex = 70 bn = ?lt = rt =

bn.assemble(x, lt, rt);

Page 13: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 13

Code Statex = 70 bn = ?lt = rt =

bn.assemble(x, lt, rt);

x = 70 bn =lt = rt =

70

Page 14: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 14

Code Statex = 70 bn = ?lt = rt =

bn.assemble(x, lt, rt);

x = 70 bn =lt = rt =

70

Note the alias created here, which you cannot see in the tracing table.

Page 15: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

disassembleT disassemble(BinaryTree<T> left,

BinaryTree<T> right)• Disassembles this into its root label, which is returned as

the value of the function, and subtrees left and right; the declaration notwithstanding, the dynamic type of left and right must be the same as the dynamic type of this.

• Replaces: left, right• Clears: this• Requires:

this /= empty_tree

• Ensures:#this = compose(disassemble, left, right)

8 February 2019 OSU CSE 15

Page 16: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 16

Code Statelt = ? bn =rt = ?

NaturalNumber root =bn.disassemble(lt, rt);

13

Page 17: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 17

Code Statelt = ? bn =rt = ?

NaturalNumber root =bn.disassemble(lt, rt);

root = 13 bn =lt = rt =

13

Page 18: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

size

int size()

• Reports the size of this.• Ensures:size = |this|

8 February 2019 OSU CSE 18

Page 19: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

iterator

Iterator<T> iterator()

• Returns an iterator over a set of elements of type T.

• Ensures:~this.seen * ~this.unseen =

IN_ORDER(this)

8 February 2019 OSU CSE 19

Page 20: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Traversal Orders

• There are three named traversal orders for the nodes of a binary tree, named according to when the root is “visited” relative to the (recursive) traversals of the left and right subtrees– Pre-order: root is visited before left and right– In-order: root is visited between left and right– Post-order: root is visited after left and right

8 February 2019 OSU CSE 20

Page 21: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Traversal Orders

• There are three named traversal orders for the nodes of a binary tree, named according to when the root is “visited” relative to the (recursive) traversals of the left and right subtrees– Pre-order: root is visited before left and right– In-order: root is visited between left and right– Post-order: root is visited after left and right

8 February 2019 OSU CSE 21

The iterator method returns an Iterator<T>that visits the node labels

in this order.

Page 22: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

• Pre-order traversal: <4, 2, 1, 3, 5>• In-order traversal: <1, 2, 3, 4, 5>• Post-order traversal: <1, 3, 2, 5, 4>8 February 2019 OSU CSE 22

1 3

2

4

5

Page 23: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

root

T root()

• Reports the root of this.• Aliases: reference returned by root• Requires:

this /= empty_tree

• Ensures:there exists lt, rt: binary tree of T

(this = compose(root, lt, rt))

8 February 2019 OSU CSE 23

Page 24: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 24

Code Statebn =

NaturalNumber k =bn.root();

13

Page 25: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 25

Code Statebn =

NaturalNumber k =bn.root();

k = 13 bn =

13

13

Page 26: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 26

Code Statebn =

NaturalNumber k =bn.root();

k = 13 bn =

13

13

Note the alias created here, which you cannot see in the tracing table.

Page 27: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

replaceRootT replaceRoot(T x)• Replaces the root of this with x, and returns the

old root.• Aliases: reference x• Requires:

this /= empty_tree

• Ensures:there exists lt, rt: binary tree of T(#this = compose(replaceRoot, lt, rt) andthis = compose(x, lt, rt))

8 February 2019 OSU CSE 27

Page 28: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 28

Code Staten = 4 bn =

NaturalNumber k =bn.replaceRoot(n);

13

Page 29: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 29

Code Staten = 4 bn =

NaturalNumber k =bn.replaceRoot(n);

n = 4 bn =k = 13

13

4

Page 30: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Example

8 February 2019 OSU CSE 30

Code Staten = 4 bn =

NaturalNumber k =bn.replaceRoot(n);

n = 4 bn =k = 13

13

4

Note the alias created here, which you cannot see in the tracing table.

Page 31: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Another Example

8 February 2019 OSU CSE 31

Code Staten = 4 bn =

n = bn.replaceRoot(n);

13

Page 32: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Another Example

8 February 2019 OSU CSE 32

Code Staten = 4 bn =

n = bn.replaceRoot(n);

n = 13 bn =

13

4

Page 33: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Another Example

8 February 2019 OSU CSE 33

Code Staten = 4 bn =

n = bn.replaceRoot(n);

n = 13 bn =

13

4

This use of the method avoids creating an alias: it swaps n with the old root.

Page 34: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

height

int height()

• Reports the height of this.• Ensures:height = ht(this)

8 February 2019 OSU CSE 34

Page 35: CSE 2231 - BinaryTreeweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/11.BinaryTree.pdf · BinaryTree • The . BinaryTree. component family allows you to manipulate values

Resources

• OSU CSE Components API: BinaryTree– http://cse.osu.edu/software/common/doc/

8 February 2019 OSU CSE 35