190 likes | 327 Views
A New Math Type: Tree. Math Tree Continued…. E. H. B. K. C. G. A. F. L. D. 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:
E N D
. . . Math Tree Continued… E H B K C G A F L D 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 bytree 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 = trueand outs.ext_name = #outs.ext_name and outs.contents = #outs.contents * OUTPUT_REP (t) !*/
E H B K C G A F L D K J F A What’s OUTPUT_REP (t)? X X() E(H(C()G())B()K(A()F(J())L()D())) K(F()A())
Q X Z D Y A W F M H S You Give It a Try! Q(F()) X(M(S())) Z(D()Y()A(H())W())
root of t . . . Step 1: Visualize Recursive Structure 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 ‘)’.
Processing non-smallest incoming values of t: 1: display root and ‘(‘ ( . . . ) repeat steps 2-4 for each of the subtrees of t . . . 5: display ‘)‘ 2: remove subtree from t 3: display subtree by calling Display_Tree 4: place subtree back in t Processing smallest incoming values of t: Step 3: Visualize Recursive Process #t = . . . 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: 1: display root and ‘(‘ ( . . . ) 5: display ‘)‘ #t = repeat steps 2-4 for each of the subtrees of t . . . = tmp 3: display subtree by calling Display_Tree . . . 2: remove subtree from t 4: place subtree in tmp 6: swap roots 7: swap t and tmp Processing smallest incoming values of t: No special handling of smallest incoming values
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;