1 Trees Trees 9/21/2007 8:11 AM
Outline and Reading Tree ADT (§6.1) Preorder and postorder traversals (§6.2.3) BinaryTree ADT (§6.3.1) Inorder traversal (§6.3.4) Euler Tour traversal (§6.3.4) Template method pattern (§6.3.5) Data structures for trees (§6.4) 9/21/2007 8:11 AM Trees 2
What is a Tree In computer science, a tree is an abstract model Computers”R”Us of a hierarchical structure Sales Manufacturing R&D A tree consists of nodes with a parent-child relation US International Laptops Desktops Applications: Organization charts � File systems � Europe Asia Canada Programming � environments 9/21/2007 8:11 AM Trees 3
Tree Terminology Root: node without parent (A) Subtree: tree consisting of a node and its Internal node: node with at least descendants one child (A, B, C, F) External node (a.k.a. leaf ): node A without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, B C D etc. Depth of a node: number of ancestors E F G H Height of a tree: maximum depth of any node (3) subtree Descendant of a node: child, I J K grandchild, grand-grandchild, etc. 9/21/2007 8:11 AM Trees 4
Tree ADT We use positions to abstract Query methods: nodes boolean isInternal(p) � Generic methods: boolean isExternal(p) � boolean isRoot(p) integer size() � � boolean isEmpty() Update methods: � objectIterator elements() � swapElements(p, q) � positionIterator positions() � object replaceElement(p, o) � Accessor methods: Additional update methods may be defined by data position root() � structures implementing the position parent(p) � Tree ADT positionIterator children(p) � 9/21/2007 8:11 AM Trees 5
Preorder Traversal A traversal visits the nodes of a Algorithm preOrder ( v ) tree in a systematic manner visit ( v ) In a preorder traversal, a node is for each child w of v visited before its descendants preorder ( w ) Application: print a structured document 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.3 Bank 2.1 Stock 2.2 Ponzi 1.1 Greed 1.2 Avidity Fraud Scheme Robbery 9/21/2007 8:11 AM Trees 6
Postorder Traversal In a postorder traversal, a Algorithm postOrder ( v ) node is visited after its for each child w of v descendants postOrder ( w ) Application: compute space visit ( v ) used by files in a directory and its subdirectories 9 cs16/ 8 3 7 todo.txt homeworks/ programs/ 1K 4 5 6 1 2 h1c.doc h1nc.doc DDR.java Stocks.java Robot.java 3K 2K 10K 25K 20K 9/21/2007 8:11 AM Trees 7
Binary Tree A binary tree is a tree with the Applications: following properties: arithmetic expressions � Each internal node has two � decision processes � children searching � The children of a node are an � ordered pair A We call the children of an internal node left child and right child Alternative recursive definition: a B C binary tree is either a tree consisting of a single node, � or D E F G a tree whose root has an ordered � pair of children, each of which is a binary tree H I 9/21/2007 8:11 AM Trees 8
Arithmetic Expression Tree Binary tree associated with an arithmetic expression � internal nodes: operators � external nodes: operands Example: arithmetic expression tree for the expression (2 × ( a − 1) + (3 × b)) + × × − 2 3 b a 1 9/21/2007 8:11 AM Trees 9
Decision Tree Binary tree associated with a decision process � internal nodes: questions with yes/no answer � external nodes: decisions Example: dining decision Want a fast meal? Yes No How about coffee? On expense account? Yes No Yes No Starbucks Spike’s Al Forno Café Paragon 9/21/2007 8:11 AM Trees 10
Properties of Binary Trees Notation Properties: � e = i + 1 n number of nodes e number of � n = 2 e − 1 external nodes � h ≤ i i number of internal � h ≤ ( n − 1) / 2 nodes � e ≤ 2 h h height � h ≥ log 2 e � h ≥ log 2 ( n + 1) − 1 9/21/2007 8:11 AM Trees 11
BinaryTree ADT The BinaryTree ADT Update methods extends the Tree may be defined by ADT, i.e., it inherits data structures all the methods of implementing the the Tree ADT BinaryTree ADT Additional methods: � position leftChild(p) � position rightChild(p) � position sibling(p) 9/21/2007 8:11 AM Trees 12
Inorder Traversal In an inorder traversal a Algorithm inOrder ( v ) node is visited after its left if isInternal ( v ) subtree and before its right inOrder ( leftChild ( v )) subtree Application: draw a binary visit ( v ) tree if isInternal ( v ) x(v) = inorder rank of v � inOrder ( rightChild ( v )) y(v) = depth of v � 6 2 8 1 4 7 9 3 5 9/21/2007 8:11 AM Trees 13
Print Arithmetic Expressions Algorithm printExpression ( v ) Specialization of an inorder traversal if isInternal ( v ) print operand or operator print ( “(’’ ) � when visiting node printExpression ( leftChild ( v )) print “(“ before traversing left � print ( v.element ()) subtree print “)“ after traversing right if isInternal ( v ) � subtree printExpression ( rightChild ( v )) + print ( “)’’ ) × × − 2 3 b ((2 × ( a − 1)) + (3 × b)) a 1 9/21/2007 8:11 AM Trees 14
Evaluate Arithmetic Expressions Algorithm evalExpr ( v ) Specialization of a postorder traversal if isExternal ( v ) return v.element () recursive method returning � the value of a subtree else x ← evalExpr ( leftChild ( v )) when visiting an internal � node, combine the values y ← evalExpr ( rightChild ( v )) of the subtrees ◊ ← operator stored at v return x ◊ y + × × − 2 3 2 5 1 9/21/2007 8:11 AM Trees 15
Euler Tour Traversal Generic traversal of a binary tree Includes as special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) � from below (inorder) � on the right (postorder) � + × × L R B − 2 3 2 5 1 9/21/2007 8:11 AM Trees 16
Data Structure for Trees A node is represented by ∅ an object storing Element � Parent node � B Sequence of children � nodes ∅ ∅ Node objects implement the Position ADT A D F B D A F ∅ ∅ C E C E 9/21/2007 8:11 AM Trees 17
Data Structure for Binary Trees A node is represented ∅ by an object storing Element � Parent node � B Left child node � Right child node � Node objects implement ∅ ∅ the Position ADT B A D A D ∅ ∅ ∅ ∅ C E C E 9/21/2007 8:11 AM Trees 18
Recommend
More recommend