BinaryTree 1 October 2020 OSU CSE 1 BinaryTree The BinaryTree - - PowerPoint PPT Presentation

binarytree
SMART_READER_LITE
LIVE PREVIEW

BinaryTree 1 October 2020 OSU CSE 1 BinaryTree The BinaryTree - - PowerPoint PPT Presentation

BinaryTree 1 October 2020 OSU CSE 1 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


slide-1
SLIDE 1

BinaryTree

1 October 2020 OSU CSE 1

slide-2
SLIDE 2

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

1 October 2020 OSU CSE 2

slide-3
SLIDE 3

Interfaces and Classes

BinaryTree- Kernel extends Standard extends

1 October 2020 OSU CSE 3

BinaryTree implements BinaryTree1 Iterable extends

slide-4
SLIDE 4

Interfaces and Classes

BinaryTree- Kernel extends Standard extends

1 October 2020 OSU CSE 4

BinaryTree implements BinaryTree1 Iterable extends BinaryTreeKernel has contracts for three methods: assemble disassemble size

slide-5
SLIDE 5

Interfaces and Classes

BinaryTree- Kernel extends Standard extends

1 October 2020 OSU CSE 5

BinaryTree implements BinaryTree1 Iterable extends BinaryTree has contracts for four methods (the last of which we will skip): root replaceRoot height inOrderAssemble

slide-6
SLIDE 6

Interfaces and Classes

BinaryTree- Kernel extends Standard extends

1 October 2020 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.

slide-7
SLIDE 7

Mathematical Model

type BinaryTreeKernel is modeled by binary tree of T

1 October 2020 OSU CSE 7

slide-8
SLIDE 8

No-argument Constructor

  • Ensures:

this = empty_tree

1 October 2020 OSU CSE 8

slide-9
SLIDE 9

Example

1 October 2020 OSU CSE 9

Code State

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

slide-10
SLIDE 10

Example

1 October 2020 OSU CSE 10

Code State

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

bn =

slide-11
SLIDE 11

assemble

void assemble(T root, BinaryTree<T> left, BinaryTree<T> right)

  • Assembles in this a binary tree with root label root

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.

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

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

1 October 2020 OSU CSE 11

slide-12
SLIDE 12

Example

1 October 2020 OSU CSE 12

Code State

x = 70 bn = ? lt = rt = bn.assemble(x, lt, rt);

slide-13
SLIDE 13

Example

1 October 2020 OSU CSE 13

Code State

x = 70 bn = ? lt = rt = bn.assemble(x, lt, rt); x = 70 bn = lt = rt = 70

slide-14
SLIDE 14

Example

1 October 2020 OSU CSE 14

Code State

x = 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.

slide-15
SLIDE 15

disassemble

T 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)

1 October 2020 OSU CSE 15

slide-16
SLIDE 16

Example

1 October 2020 OSU CSE 16

Code State

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

slide-17
SLIDE 17

Example

1 October 2020 OSU CSE 17

Code State

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

slide-18
SLIDE 18

size

int size()

  • Reports the size of this.
  • Ensures:

size = |this|

1 October 2020 OSU CSE 18

slide-19
SLIDE 19

iterator

Iterator<T> iterator()

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

~this.seen * ~this.unseen = IN_ORDER(this)

1 October 2020 OSU CSE 19

slide-20
SLIDE 20

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

1 October 2020 OSU CSE 20

slide-21
SLIDE 21

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

1 October 2020 OSU CSE 21

The iterator method returns an Iterator<T> that visits the node labels in this order.

slide-22
SLIDE 22
  • Pre-order traversal: <4, 2, 1, 3, 5>
  • In-order traversal: <1, 2, 3, 4, 5>
  • Post-order traversal: <1, 3, 2, 5, 4>

1 October 2020 OSU CSE 22

1 3 2 4 5

slide-23
SLIDE 23

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))

1 October 2020 OSU CSE 23

slide-24
SLIDE 24

Example

1 October 2020 OSU CSE 24

Code State

bn =

NaturalNumber k = bn.root();

13

slide-25
SLIDE 25

Example

1 October 2020 OSU CSE 25

Code State

bn =

NaturalNumber k = bn.root();

k = 13 bn = 13 13

slide-26
SLIDE 26

Example

1 October 2020 OSU CSE 26

Code State

bn =

NaturalNumber k = bn.root();

k = 13 bn = 13 13

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

slide-27
SLIDE 27

replaceRoot

T replaceRoot(T x)

  • Replaces the root of this with x, and returns the
  • ld root.
  • Aliases: reference x
  • Requires:

this /= empty_tree

  • Ensures:

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

1 October 2020 OSU CSE 27

slide-28
SLIDE 28

Example

1 October 2020 OSU CSE 28

Code State

n = 4 bn =

NaturalNumber k = bn.replaceRoot(n);

13

slide-29
SLIDE 29

Example

1 October 2020 OSU CSE 29

Code State

n = 4 bn =

NaturalNumber k = bn.replaceRoot(n);

n = 4 bn = k = 13 13 4

slide-30
SLIDE 30

Example

1 October 2020 OSU CSE 30

Code State

n = 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.

slide-31
SLIDE 31

Another Example

1 October 2020 OSU CSE 31

Code State

n = 4 bn =

n = bn.replaceRoot(n);

13

slide-32
SLIDE 32

Another Example

1 October 2020 OSU CSE 32

Code State

n = 4 bn =

n = bn.replaceRoot(n);

n = 13 bn = 13 4

slide-33
SLIDE 33

Another Example

1 October 2020 OSU CSE 33

Code State

n = 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.

slide-34
SLIDE 34

height

int height()

  • Reports the height of this.
  • Ensures:

height = ht(this)

1 October 2020 OSU CSE 34

slide-35
SLIDE 35

Resources

  • OSU CSE Components API: BinaryTree

– http://cse.osu.edu/software/common/doc/

  • Big Java (4th ed), Sections 16.6

– https://library.ohio-state.edu/record=b8540788~S7

1 October 2020 OSU CSE 35