Chapter 10, Section 10.3 Tree Traversal These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed.,

Embed Size (px)

Citation preview

  • Slide 1

Chapter 10, Section 10.3 Tree Traversal These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth H. Rosen, published by McGraw Hill, Boston, MA, 2006. They are intended for classroom use only and are not a substitute for reading the textbook. Slide 2 Universal Address Systems To totally order the vertices of on orered rooted tree: 1.Label the root with the integer 0. 2.Label its k children (at level 1) from left to right with 1, 2, 3, , k. 3.For each vertex v at level n with label A, label its k v children, from left to right, with A.1, A.2, A.3, , A.k v. This labeling is called the universal address system of the ordered rooted tree. Slide 3 Universal Address Systems Slide 4 Traversal Algorithms A traversal algorithm is a procedure for systematically visiting every vertex of an ordered rooted tree An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered The three common traversals are: Preorder traversal Inorder traversal Postorder traversal Slide 5 Traversal Let T be an ordered rooted tree with root r. Suppose T 1, T 2, ,T n are the subtrees at r from left to right in T. r T1T1 T2T2 TnTn Slide 6 Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder Slide 7 Preorder Traversal Slide 8 Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: Slide 9 The Preorder Traversal of T In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown to the left? Preorder: Visit root, then visit subtrees left to right. Slide 10 The Preorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Preorder: Visit root, then visit subtrees left to right. Slide 11 The Preorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 12 The Preorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 13 The Preorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 14 Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn Slide 15 Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: Slide 16 The Inorder Traversal of T In which order does an inorder traversal visit the vertices in the ordered rooted tree T shown to the left? Inorder: Visit leftmost tree, visit root, visit other subtrees left to right. Slide 17 The Inorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Inorder: Visit leftmost tree, visit root, visit other subtrees left to right. Slide 18 The Inorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 19 The Inorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 20 The Inorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 21 Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn Slide 22 Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence: Slide 23 The Postorder Traversal of T In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown to the left? Postorder: Visit subtrees left to right, then visit root. Slide 24 The Postorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Postorder: Visit subtrees left to right, then visit root. Slide 25 The Postorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 26 The Postorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 27 The Postorder Traversal of T The McGraw-Hill Companies, Inc. all rights reserved Slide 28 Representing Arithmetic Expressions Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves represent operands Build the tree bottom-up Construct smaller subtrees Incorporate the smaller subtrees as part of larger subtrees Slide 29 Example (x+y) 2 + (x-3)/(y+2) + x y 2 x 3 + y 2 / + Slide 30 Infix Notation + + / + 2 x y x 3 y 2 Traverse in inorder adding parentheses for each operation x + y () 2 () + x 3() / y + 2() () () Slide 31 Prefix Notation (Polish Notation) Traverse in preorder: x + y 2 + x 3 / y + 2 + + / + 2 x y x 3 y 2 Slide 32 Evaluating Prefix Notation In an prefix expression, a binary operator precedes its two operands The expression is evaluated right-left Look for the first operator from the right Evaluate the operator with the two operands immediately to its right Slide 33 Example + / + 2 2 2 / 3 2 + 1 0 + / + 2 2 2 / 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3 Slide 34 Postfix Notation (Reverse Polish) Traverse in postorder x + y 2 + x 3 / y + 2 + + / + 2 x y x 3 y 2 Slide 35 In an postfix expression, a binary operator follows its two operands The expression is evaluated left-right Look for the first operator from the left Evaluate the operator with the two operands immediately to its left Evaluating Postfix Notation Slide 36 Example 3 2 2 + 2 / 3 2 1 0 + / + 4 2 / 3 2 1 0 + / + 2 3 2 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 + Slide 37 Homework Exercises Do questions : 7, 9, 10, 12, 13, 15, 16, 17, 18, 23, 24 Slide 38 Conclusion In this chapter we have studied: Trees Applications of Trees Tree Traversal