1 / 31

Trees

Explore hierarchical structures, node relationships, tree applications, and traversal algorithms in computer science. Learn about tree terminology, ADT, Java interfaces, tree size analysis, and more.

vandemark
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 CSCI 3333 Data Structures byDr. Bun YueProfessor of Computer Scienceyue@uhcl.eduhttp://sce.uhcl.edu/yue/2013

  2. Acknowledgement • Mr. Charles Moen • Dr. Wei Ding • Ms. Krishani Abeysekera • Dr. Michael Goodrich

  3. Computers”R”Us Sales Manufacturing R&D US International Laptops Desktops Europe Asia Canada What is a Tree • In computer science, a tree is an abstract model of a hierarchical structure • A tree consists of nodes with a parent-child relation

  4. Trees • Each node in the tree has • 0 or 1 parent node. • 0 or more child nodes. • Only the root node has no parent. • A tree can be considered as a special case of a directed (acyclic) graph. • Example: a family tree may not necessarily be a tree in the computing sense.

  5. Trees British Royal Family Tree Queen Victoria King Edward VII Alice Albert Victor King George V King Edward VIII King George VI Queen Elizabeth II Charles Ann Andrew Edward Beatrice Eugenie William Henry Peter Zara

  6. Some Tree Applications • Applications: • Organization charts • File Folders • Email Repository • Programming environments: e.g. drop down menus.

  7. Recursive Definition of Trees • An empty tree is a tree. • A tree contains a root node connected to 0 or more child sub-trees.

  8. Trees Are these trees? 1 2 4 3

  9. A C D B E G H F K I J Tree Terminology • Subtree: tree consisting of a node and its descendants • Root: node without parent (A) • Internal node: node with at least one child (A, B, C, F) • External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, grand-grandparent, etc. • Depth of a node: number of ancestors • Height of a tree: maximum depth of any node (3) • Descendant of a node: child, grandchild, grand-grandchild, etc. subtree

  10. Tree ADT (§ 6.1.2) • Query methods: • boolean isInternal(p) • boolean isExternal(p) • boolean isRoot(p) • Update method: • object replace (p, o) • Additional update methods may be defined by data structures implementing the Tree ADT • We use positions to abstract nodes • Generic methods: • integer size() • boolean isEmpty() • Iterator elements() • Iterator positions() • Accessor methods: • position root() • position parent(p) • positionIterator children(p)

  11. Interface TreeNode in Java • Enumeration children(): Returns the children of the receiver as an Enumeration. • boolean getAllowsChildren(): Returns true if the receiver allows children. • TreeNode getChildAt(int childIndex): Returns the child TreeNode at index childIndex. • int getChildCount(): Returns the number of children TreeNodes the receiver contains. • int getIndex(TreeNode node): Returns the index of node in the receivers children. • TreeNode getParent(): Returns the parent TreeNode of the receiver. • Boolean isLeaf(): Returns true if the receiver is a leaf.

  12. Interface MutatableTreeNode • Sub-interface of TreeNode • Methods: • void insert(MutableTreeNode child, int index): Adds child to the receiver at index. • void remove(int index): Removes the child at index from the receiver. • void remove(MutableTreeNode node): Removes node from the receiver. • void removeFromParent(): Removes the receiver from its parent. • void setParent(MutableTreeNode newParent): Sets the parent of the receiver to newParent. • void setUserObject(Object object): Resets the user object of the receiver to object.

  13. Size of a tree • Size = number of nodes in the tree. • Recursion analysis: • Base case: empty root => return 0; • Recursive case: non-empty root => return 1 + sum of size of all child sub-trees.

  14. Size of a tree Algorithm size(TreeNode root) Input TreeNode root Output: size of the tree if (root = null) return 0; else return 1 + Sum of sizes of all children of root end if

  15. Implementation in Java public static int size(TreeNode root) { if (root == null) return 0; Enumeration enum = root.children(); int result = 1; // count the root. while (enum.hasMoreElement()) { result += size((TreeNode) enum.nextElement()); } return result; }

  16. Trees in languages • There are no general tree classes in the standard libraries in many languages: e.g. C++ STL, Ruby, Python, etc. • No single interface tree design satisfies all users. • There may be specialized trees, especially for GUI design. E.g. • JTree in Java • FXTreeList in Ruby

  17. Tree Traversal • Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. • Consider the three operations on a binary tree: • V: Visit a node • L: (Recursively) traverse the left subtree of a node • R: (Recursively) traverse the right subtree of a node

  18. Tree Traversing • We can traverse a tree in six different orders: LVR VLR LRV VRL RVL RLV

  19. Preorder Traversal AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) • A traversal visits the nodes of a tree in a systematic manner • In a preorder traversal, a node is visited before its descendants • Application: print a structured document 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 1.1 Greed 1.2 Avidity

  20. Java Implementation public static void preorder(TreeNode root) { if (root != null) { visit(root); // assume declared. Enumernation enum = root.children(); while (enum.hasMoreElement()) { preorder((TreeNode) enum.nextElement()); } }

  21. Postorder Traversal AlgorithmpostOrder(v) foreachchild w of v postOrder (w) visit(v) • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories 9 cs16/ 8 3 7 todo.txt1K homeworks/ programs/ 4 5 6 1 2 Robot.java20K h1c.doc3K h1nc.doc2K DDR.java10K Stocks.java25K

  22. Inorder Traversal • For binary tree, inorder traversal can also be defined: L V R.

  23. Java Implementation public static void inorder(TreeNode root) { if (root != null) { inorder(root.getChildAt(0)); visit(root); // assume declared. inorder(root.getChildAt(1)); }

  24. Expression Notation • There are three common notations for expressions: • Prefix: operator come before operands. • Postfix: operator come after operands. • Infix: • Binary operator come between operands. • Unary operator come before the operand.

  25. Examples • Infix: (a+b)*(c-d/f/(g+h)) • Used by human being. • Prefix: *+ab-c//df+gh • E.g. functional language such as Lisp: • Lisp: (* (+ a b) (- c (/ d (/ f (+ g h))))) • Postfix: ab+cdf/gh+/-* • E.g. Postfix calculator

  26. Expression Trees • An expression tree can be used to represent an expression. • Operator: root • Operands: child sub-trees • Variable and constants: leaf nodes.

  27. Traversals of Expression Trees • Preorder traversal => prefix notation. • Postorder traversal => postfix notation. • Inorder traversal with parenthesis added => infix notation.

  28. Linked Structure for Trees • A node is represented by an object storing • Element • Parent node • Sequence of children nodes • Node objects implement the Position ADT B   A D F B A D F   C E C E

  29. Tree Classes in Ruby • FXTreeItem: a tree node for GUI application. • Some methods: • Tree related: childOf?, parentOf?, numChildren, parent, etc. • Traversal of child nodes: first, next, last, etc. • GUI related: selected?, selected=, hasFocus?, etc.

  30. Tree Classes in Ruby 2 • FXTreeList: a list of FXTreeItem. • Include methods for list manipulation, such as appendItem, clearItems, etc.

  31. Questions and Comments?

More Related