binarytree
play

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


  1. BinaryTree 1 October 2020 OSU CSE 1

  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

  3. Interfaces and Classes Standard Iterable extends extends BinaryTree- Kernel extends BinaryTree implements BinaryTree1 1 October 2020 OSU CSE 3

  4. Interfaces and Classes Standard Iterable extends extends BinaryTree- Kernel BinaryTreeKernel extends has contracts for BinaryTree three methods: assemble implements disassemble size BinaryTree1 1 October 2020 OSU CSE 4

  5. Interfaces and Classes BinaryTree has contracts for four methods (the last of Standard Iterable which we will skip): root extends extends replaceRoot BinaryTree- height Kernel inOrderAssemble extends BinaryTree implements BinaryTree1 1 October 2020 OSU CSE 5

  6. Interfaces and Classes There is really an abstract Standard Iterable class as usual in the chain here, but it is not shown extends extends because these slides describe the client view, and a BinaryTree- client needs only interface Kernel BinaryTree and class extends BinaryTree1 . BinaryTree implements BinaryTree1 1 October 2020 OSU CSE 6

  7. Mathematical Model type BinaryTreeKernel is modeled by binary tree of T 1 October 2020 OSU CSE 7

  8. No-argument Constructor • Ensures: this = empty_tree 1 October 2020 OSU CSE 8

  9. Example Code State BinaryTree<NaturalNumber> bn = new BinaryTree1<>(); 1 October 2020 OSU CSE 9

  10. Example Code State BinaryTree<NaturalNumber> bn = new BinaryTree1<>(); bn = 1 October 2020 OSU CSE 10

  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

  12. Example Code State x = 70 bn = ? lt = rt = bn.assemble(x, lt, rt); 1 October 2020 OSU CSE 12

  13. Example Code State x = 70 bn = ? lt = rt = bn.assemble(x, lt, rt); x = 70 bn = 70 lt = rt = 1 October 2020 OSU CSE 13

  14. Example Code State Note the alias created x = 70 bn = ? here, which you cannot lt = see in the tracing table. rt = bn.assemble(x, lt, rt); x = 70 bn = 70 lt = rt = 1 October 2020 OSU CSE 14

  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

  16. Example Code State lt = ? bn = 13 rt = ? NaturalNumber root = bn.disassemble(lt, rt); 1 October 2020 OSU CSE 16

  17. Example Code State lt = ? bn = 13 rt = ? NaturalNumber root = bn.disassemble(lt, rt); root = 13 bn = lt = rt = 1 October 2020 OSU CSE 17

  18. size int size() • Reports the size of this . • Ensures: size = | this | 1 October 2020 OSU CSE 18

  19. iterator Iterator<T> iterator() • Returns an iterator over a set of elements of type T . • Ensures: ~this.seen * ~this.unseen = IN_ORDER( this ) 1 October 2020 OSU CSE 19

  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

  21. Traversal Orders • There are three named traversal orders The iterator method for the nodes of a binary tree, named returns an Iterator<T> according to when the root is “visited” that visits the node labels in this order. 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

  22. 4 2 5 1 3 • 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

  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

  24. Example Code State bn = 13 NaturalNumber k = bn.root(); 1 October 2020 OSU CSE 24

  25. Example Code State bn = 13 NaturalNumber k = bn.root(); k = 13 bn = 13 1 October 2020 OSU CSE 25

  26. Example Code State Note the alias created bn = here, which you cannot 13 see in the tracing table. NaturalNumber k = bn.root(); k = 13 bn = 13 1 October 2020 OSU CSE 26

  27. replaceRoot T 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) and this = compose (x, lt, rt)) 1 October 2020 OSU CSE 27

  28. Example Code State n = 4 bn = 13 NaturalNumber k = bn.replaceRoot(n); 1 October 2020 OSU CSE 28

  29. Example Code State n = 4 bn = 13 NaturalNumber k = bn.replaceRoot(n); n = 4 bn = 4 k = 13 1 October 2020 OSU CSE 29

  30. Example Code State Note the alias created n = 4 bn = here, which you cannot 13 see in the tracing table. NaturalNumber k = bn.replaceRoot(n); n = 4 bn = 4 k = 13 1 October 2020 OSU CSE 30

  31. Another Example Code State n = 4 bn = 13 n = bn.replaceRoot(n); 1 October 2020 OSU CSE 31

  32. Another Example Code State n = 4 bn = 13 n = bn.replaceRoot(n); n = 13 bn = 4 1 October 2020 OSU CSE 32

  33. Another Example Code State This use of the method avoids creating an alias: it n = 4 bn = 13 swaps n with the old root. n = bn.replaceRoot(n); n = 13 bn = 4 1 October 2020 OSU CSE 33

  34. height int height() • Reports the height of this . • Ensures: height = ht ( this ) 1 October 2020 OSU CSE 34

  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

Recommend


More recommend