1 / 19

Trees

Trees. trees binary trees traversals of trees template method pattern data structures for trees. Trees. a tree represents a hierarchy examples : organization structure of a corporation. table of contents of a book. Another Example. ` Unix or DOS/Windows file system. TERMINOLOGY.

rowenaj
Download Presentation

Trees

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. Trees • trees • binary trees • traversals of trees • template method pattern • data structures for trees

  2. Trees • a tree represents a hierarchy • examples: • organization structure of a corporation table of contents of a book

  3. Another Example • ` Unix or DOS/Windows file system

  4. TERMINOLOGY

  5. Binary Trees

  6. Examples of Binary Trees • arithmetic expression • river

  7. Examples of Binary Trees • decision trees

  8. Properties of Binary Trees

  9. ADTs for Trees • generic container methods -size(), isEmpty(), elements() • positional container methods -positions(), swapElements(p,q), replaceElement(p,e) • query methods -isRoot(p), isInternal(p), isExternal(p) • accessor methods -root(), parent(p), children(p) • update methods -application specific

  10. ADTs for Binary Trees • accessor methods -leftChild(p), rightChild(p), sibling(p) • update methods -expandExternal(p), removeAboveExternal(p) -other application specific methods

  11. Traversing Trees • preorder traversal AlgorithmpreOrder(v) “visit” nodev for eachchild w of vdo recursively performpreOrder(w) • reading a document from beginning to end

  12. Traversing Trees • postorder traversal AlgorithmpostOrder(v) for eachchild w of vdo recursively perform postOrder(w) “visit” node v • du (disk usage) command in Unix

  13. Evaluating Arithmetic Expressions AlgorithmevaluateExpression(v) if vis an external node return the variable stored at v else let o be the operator stored atv x  evaluateExpression(leftChild(v)) y  evaluateExpression(rightChild(v)) returnx o y • specialization of a postorder traversal

  14. Traversing Trees Algorithm inOrder(v) recursively perform inOrder(leftChild(v)) “visit” node v recursively performinOrder(rightChild(v)) • inordertraversal of a binary tree • printing an arithmetic expression • specialization of an inorder traversal • print “(“ before traversing the left subtree • print “)” after traversing the right subtree

  15. Euler Tour Traversal • generic traversal of a binary tree • the preorder, inorder, and postorder traversals are special cases of the Euler tour traversal • “walk around” the tree and visit each node three times: • on the left • from below • on the right

  16. Template Method Pattern • generic computation mechanism that can be specialized by redefining certain steps • implemented by means of an abstract Java class with methods that can be redefined by its subclasses

  17. Specializing the Generic Binary Tree Traversal • printing an arithmetic expression public classPrintExpressionTraversal extends BinaryTreeTraversal { ... protected void external(Position p, TraversalResult r) { System.out.print(p.element()); } protected void left(Position p, TraversalResult r) { System.out.print("("); } protected void below(Position p, TraversalResult r) { System.out.print(p.element()); } protected void right(Position p, TraversalResult r) { System.out.print(")"); }

  18. Linked Data Structure for Binary Trees

  19. Representing General Trees tree T binary tree T' representing T

More Related