# A New Math Type: Tree. Math Tree Continued… E HK CGFL B AD J...

## Presentation on theme: "A New Math Type: Tree. Math Tree Continued… E HK CGFL B AD J..."— Presentation transcript:

A New Math Type: Tree

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

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.

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

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

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

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

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

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) !*/

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

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

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

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 ‘)’.

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

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

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 << ‘)’;

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

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

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;