Download presentation

1
**Chapter 10, Section 10.3 Tree Traversal**

These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6th 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: Label the root with the integer 0. Label its k children (at level 1) from left to right with 1, 2, 3, …, k. For each vertex v at level n with label A, label its kv children, from left to right, with A.1, A.2, A.3, …, A.kv. 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 T1, T2, …,Tn are the subtrees at r from left to right in T. r T1 T2 Tn

6
**Preorder Traversal Step 1: Visit r Step 2: Visit T1 in preorder**

. Step n+1: Visit Tn in preorder r T1 T2 Tn

7
Preorder Traversal

8
**Example Tree: Visiting sequence: A R E Y P M H J Q T M A J Y R H P Q T**

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 T1 in inorder Step 2: Visit r**

. Step n+1: Visit Tn in inorder r T1 T2 Tn

15
**Example Tree: Visiting sequence: A R E Y P M H J Q T J A M R Y P H Q T**

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 T1 in postorder**

. Step n: Visit Tn in postorder Step n+1: Visit r r T1 T2 Tn

22
**Example Tree: Visiting sequence: A R E Y P M H J Q T J A R P Q T H Y E**

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) + 2 / + x y – x 3 + y 2

30
Infix Notation Traverse in inorder adding parentheses for each operation + – / 2 x y 3 ( ) ( ) ( ) x + y 2 + ( ) ( ) x – 3 / ( ) y + 2

31
**Prefix Notation (Polish Notation)**

Traverse in preorder: + – / 2 x y 3 + + x y 2 / – 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 + / / – + / / – + / / + / + / 3

34
**Postfix Notation (Reverse Polish)**

Traverse in postorder + – / 2 x y 3 x y + 2 x 3 – y 2 + / +

35
**Evaluating Postfix Notation**

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

36
Example / – / + / – / + – / + / + / + 3

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

OK

1 CS308 Data Structures An application of binary trees: Binary Expression Trees.

1 CS308 Data Structures An application of binary trees: Binary Expression Trees.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google