cse 2231 -...
Post on 30-Oct-2019
5 Views
Preview:
TRANSCRIPT
BinaryTree
8 February 2019 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 Set
8 February 2019 OSU CSE 2
Interfaces and Classes
BinaryTree-Kernel
extends
Standard
extends
8 February 2019 OSU CSE 3
BinaryTree
implements
BinaryTree1
Iterable
extends
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
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
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.
Mathematical Modeltype BinaryTreeKernel is modeled bybinary tree of T
8 February 2019 OSU CSE 7
No-argument Constructor
• Ensures:this = empty_tree
8 February 2019 OSU CSE 8
Example
8 February 2019 OSU CSE 9
Code State
BinaryTree<NaturalNumber> bn =new BinaryTree1<>();
Example
8 February 2019 OSU CSE 10
Code State
BinaryTree<NaturalNumber> bn =new BinaryTree1<>();
bn =
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
Example
8 February 2019 OSU CSE 12
Code Statex = 70 bn = ?lt = rt =
bn.assemble(x, lt, rt);
Example
8 February 2019 OSU CSE 13
Code Statex = 70 bn = ?lt = rt =
bn.assemble(x, lt, rt);
x = 70 bn =lt = rt =
70
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.
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
Example
8 February 2019 OSU CSE 16
Code Statelt = ? bn =rt = ?
NaturalNumber root =bn.disassemble(lt, rt);
13
Example
8 February 2019 OSU CSE 17
Code Statelt = ? bn =rt = ?
NaturalNumber root =bn.disassemble(lt, rt);
root = 13 bn =lt = rt =
13
size
int size()
• Reports the size of this.• Ensures:size = |this|
8 February 2019 OSU CSE 18
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
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
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.
• 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
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
Example
8 February 2019 OSU CSE 24
Code Statebn =
NaturalNumber k =bn.root();
13
Example
8 February 2019 OSU CSE 25
Code Statebn =
NaturalNumber k =bn.root();
k = 13 bn =
13
13
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.
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
Example
8 February 2019 OSU CSE 28
Code Staten = 4 bn =
NaturalNumber k =bn.replaceRoot(n);
13
Example
8 February 2019 OSU CSE 29
Code Staten = 4 bn =
NaturalNumber k =bn.replaceRoot(n);
n = 4 bn =k = 13
13
4
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.
Another Example
8 February 2019 OSU CSE 31
Code Staten = 4 bn =
n = bn.replaceRoot(n);
13
Another Example
8 February 2019 OSU CSE 32
Code Staten = 4 bn =
n = bn.replaceRoot(n);
n = 13 bn =
13
4
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.
height
int height()
• Reports the height of this.• Ensures:height = ht(this)
8 February 2019 OSU CSE 34
Resources
• OSU CSE Components API: BinaryTree– http://cse.osu.edu/software/common/doc/
8 February 2019 OSU CSE 35
top related