Download presentation

Presentation is loading. Please wait.

Published byNatalie Fletcher Modified over 2 years ago

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, They are intended for classroom use only and are not a substitute for reading the textbook.

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.

3
Universal Address Systems

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

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

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

7
Preorder Traversal

8
Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

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.

10
The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved Preorder: Visit root, then visit subtrees left to right.

11
The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

12
The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

13
The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

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

15
Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

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.

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.

18
The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

19
The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

20
The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

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

22
Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

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.

24
The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved Postorder: Visit subtrees left to right, then visit root.

25
The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

26
The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

27
The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

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

29
Example (x+y) 2 + (x-3)/(y+2) + x y 2 – x 3 + y 2 / +

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() () ()

31
Prefix Notation (Polish Notation) Traverse in preorder: x + y 2 + x – 3 / y – + / + 2 x y x 3 y 2

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

33
Example + / / – / / – / / / /

34
Postfix Notation (Reverse Polish) Traverse in postorder x + y 2 + x – 3 / y – + / + 2 x y x 3 y 2

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

36
Example / 3 2 – / / 3 2 – / – / / /

37
Homework Exercises Do questions : 7, 9, 10, 12, 13, 15, 16, 17, 18, 23, 24

38
Conclusion In this chapter we have studied: –Trees –Applications of Trees –Tree Traversal

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google