Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees by Dr. Bun Yue Professor of Computer Science 2013 CSCI 3333 Data Structures.

Similar presentations


Presentation on theme: "Trees by Dr. Bun Yue Professor of Computer Science 2013 CSCI 3333 Data Structures."— Presentation transcript:

1 Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu http://sce.uhcl.edu/yue/ 2013 yue@uhcl.edu http://sce.uhcl.edu/yue/ CSCI 3333 Data Structures

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

3 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 Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada

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 5 British Royal Family Tree Trees Queen Victoria King Edward VII Albert Victor Alice King George V King George VIKing Edward VIII Queen Elizabeth II CharlesAnnAndrewEdward WilliamHenryPeterZara BeatriceEugenie

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 8 Are these trees? Trees 12 3 4

9 subtree Tree Terminology  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. A B DC GH E F IJ K  Subtree: tree consisting of a node and its descendants

10 Tree ADT (§ 6.1.2)  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)  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

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  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 Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 678 9 Algorithm preOrder(v) visit(v) for each child w of v preorder (w)

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  In a postorder traversal, a node is visited after its descendants  Application: compute space used by files in a directory and its subdirectories Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 456 8

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 D A CE F B  ADF  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?


Download ppt "Trees by Dr. Bun Yue Professor of Computer Science 2013 CSCI 3333 Data Structures."

Similar presentations


Ads by Google