Download presentation

Presentation is loading. Please wait.

Published byShayne Voyles Modified over 2 years ago

1
A New Math Type: Tree

2
Math Tree Continued… E HK CGFL B AD J...

3
Math Definition for Tree of A Base case: If x is an element of A, then compose (x, empty_string) is an element of tree of A. Inductive case: If T1,T2,…,Tk are elements of tree of A and if x is an element of A, then compose (x, T1, T2,…,Tk ) is an element of tree of A.

4
Tree vs. Binary Tree Obvious: binary trees have max 2 children restriction, trees don’t Less obvious: there is no empty tree (as opposed to empty binary tree) Smallest tree has size 1 and no children

5
Math Operations Let T = compose (x, T1, T2, …, Tk ) size (T) |T| = |T1|+|T2|+…+|Tk|+1 root (T) = x children (T) = T1, T2, …, Tk Let s = x1, x2, …, xk first (s) = x1 last (s) = xk

6
Tree Component Type Tree_Kernel is modeled by tree of Item Initial Value there exists x: Item (is_initial (x) and self = compose (x, empty_string))

7
Tree Continued… Operations t.Add (pos, subtree) t.Remove (pos, subtree) t.Number_Of_Children () t.Size () t[current] (accessor)

8
Practice Operation Most operations on Tree have to be recursive Use 5 step process to recursion: 0.State the problem 1.Visualize recursive structure 2.Verify that visualized recursive structure can be leveraged into an implementation 3.Visualize a recursive implementation 4.Write a skeleton for the operation body 5.Refine the skeleton into an operation body

9
Step 0: State the Problem procedure Display_Tree ( preserves Tree_Of_Integer& t, alters Character_OStream& outs ); /*! requires outs.is_open = true ensures outs.is_open = true and outs.ext_name = #outs.ext_name and outs.contents = #outs.contents * OUTPUT_REP (t) !*/

10
What’s OUTPUT_REP (t)? E HK CGFL B AD J E(H(C()G())B()K(A()F(J())L()D())) X X() K FA K(F()A())

11
You Give It a Try! Z(D()Y()A(H())W())Q(F()) Q F X M S Z YADW H X(M(S()))

12
Step 1: Visualize Recursive Structure t = root of t subtrees...

13
Step 2: Verify That Leveraging Works Ask yourself: If Display_Tree could get a helper to display the subtrees, could it take advantage of this generous offer? Yes! Once you know how to display the subtrees, you can just display the root followed by the subtrees between ‘(‘ and ‘)’.

14
Step 3: Visualize Recursive Process Processing non-smallest incoming values of t: Processing smallest incoming values of t: #t =... 1: display root and ‘(‘ ( 2: remove subtree from t 3: display subtree by calling Display_Tree 4: place subtree back in t... repeat steps 2-4 for each of the subtrees of t ) 5: display ‘)‘ No special handling of smallest incoming values

15
Step 4: Write a Skeleton procedure_body Display_Tree ( preserves Tree_Of_Integer& t, alters Character_OStream& outs ) { } display root and ‘(‘ for each of the subtrees of t { remove subtree from t display subtree by calling Display_Tree place subtree back in t } display ‘)’

16
Step 5: Refine the Skeleton procedure_body Display_Tree ( preserves Tree_Of_Integer& t, alters Character_OStream& outs ) { } object Integer pos; outs << t[current] << ‘(‘; while (pos < t.Number_Of_Children ()) { object Tree_Of_Integer subtree; t.Remove (pos, subtree); Display_Tree (subtree, outs); t.Add (pos, subtree); pos++; } outs << ‘)’;

17
Step 3 (alternative): Visualize Recursive Process Processing non-smallest incoming values of t: Processing smallest incoming values of t: #t =... 1: display root and ‘(‘ ( 2: remove subtree from t 3: display subtree by calling Display_Tree 4: place subtree in tmp... repeat steps 2-4 for each of the subtrees of t ) 5: display ‘)‘ No special handling of smallest incoming values = tmp... 6: swap roots 7: swap t and tmp

18
Step 4 (alternative): Write a Skeleton procedure_body Display_Tree ( preserves Tree_Of_Integer& t, alters Character_OStream& outs ) { } display root and ‘(‘ for each of the subtrees of t { remove subtree from t display subtree by calling Display_Tree place subtree in tmp } display ‘)’ swap roots of t and tmp // MUST DO THIS!! swap t and tmp

19
Step 5 (alternative): Refine the Skeleton procedure_body Display_Tree ( preserves Tree_Of_Integer& t, alters Character_OStream& outs ) { } object Tree_Of_Integer tmp; outs << t[current] << ‘(‘; while (t.Number_Of_Children () > 0) { object Tree_Of_Integer subtree; t.Remove (0, subtree); Display_Tree (subtree, outs); tmp.Add (tmp.Number_Of_Children (), subtree); } outs << ‘)’; t[current] &= tmp[current]; // MUST DO THIS!! t &= tmp;

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google