1 / 19

A New Math Type: Tree

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:

sissy
Download Presentation

A New Math Type: Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A New Math Type: Tree

  2. . . . Math Tree Continued… E H B K C G A F L D 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 bytree 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 = trueand outs.ext_name = #outs.ext_name and outs.contents = #outs.contents * OUTPUT_REP (t) !*/

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

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

  12. root of t . . . Step 1: Visualize Recursive Structure 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. 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

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

  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;

More Related