Download presentation

1
4/17/2017 Section 9.3 Tree Traversal ch9.3

2
**Universal Address System**

In ordered rooted trees, vertices may be labeled according to the following scheme: choose a root node and label it 0 each of root’s k children are labeled, left to right, as 1, 2, … , k for each vertex v at level n with label A, label its kv children left to right as A.1, A.2, … A.k

3
**Universal Address System of an Ordered Rooted Tree**

4
Traversal Algorithms Traversal: procedure for visiting each vertex in an ordered tree for data access Three most commonly used traversal algorithms are: preorder inorder postorder

5
Preorder Traversal In preorder traversal, the root vertex is visited first Then the left subtree is visited using a preorder traversal Then the right subtree is visited using a preorder traversal Gives same ordering of vertices as the universal address system

6
**Pre-order traversal in action**

Original tree: Results: K K I K I R W R K W O O O O D D

7
Inorder traversal From the root vertex, proceed to the left subtree and perform an inorder traversal Return to root and access the data there Traverse the right subtree using inorder traversal

8
**In-order traversal in action**

Original tree: Results: K K I I R W K R K W O O O O D D

9
Postorder traversal From root node, proceed to left subtree and perform postorder traversal Perform postorder traversal of right subtree Access data at root vertex

10
**Post-order traversal in action**

K Original tree: Results: W K I I R O D O K W O R O D K

11
**Infix, prefix and postfix notation**

Ordered rooted trees (especially ordered binary trees) are useful in representing complicated expressions (e.g. compound propositions, arithmetic expressions) A binary expression tree is a tree used to represent such an expression

12
Example 1 Create an ordered tree to represent the expression (x+y)2 + (x-4)/3 operands are represented as leaves operators are represented as roots of subtrees

13
**Example 1 Subtrees of binary expression tree for (x+y)2 + (x-4)/3:**

4/17/2017 Example 1 Subtrees of binary expression tree for (x+y)2 + (x-4)/3: Complete binary expression tree for (x+y)2 + (x-4)/3: + / \ x y - / \ x + / \ ^ / \ / \ / \ / \ x y x 4 ^ / \ x y / \ x ch9.3

14
**Traversing binary expression tree**

Inorder traversal of binary expression tree produces original expression (without parentheses), in infix order Preorder traversal produces a prefix expression Postorder traversal produces a postfix expression

15
**Prefix expressions The prefix version of the expression**

(x+y)2 + (x-4)/3 is: + ^ + x y 2 / - x 4 3 Evaluating prefix expressions: Read expression right to left When an operator is encountered, apply it to the previous operand (if unary) or operands (if binary) , placing the result back into the expression where the subexpression had been

16
**Example 2 + * / 4 2 3 9 // original expression**

+ * // 4/2 evaluated // 2*3 evaluated 15 // 6+9 evaluated

17
**Example 3 * - + 4 3 5 / + 2 4 3 // original expression**

* / 6 3 // 2+4 evaluated * // 6/3 evaluated * // 4+3 evaluated * // 7-5 evaluated 4 // 2*2 evaluated

18
**Postfix expressions Also known as reverse Polish expressions**

Like infix, they are evaluated left to right Like prefix, they are unambiguous, not requiring parentheses To evaluate: read expression left to right; as soon as an operator is encountered, perform the operation and place the result back in the expression

19
**Postfix expressions Simple expression: Original Expression: A + B**

Postfix Equivalent: A B + Compound expression with parentheses: original: (A + B) * (C - D) postfix: A B + C D - * Compound expression without parentheses: original: A + B * C - D postfix: A B C * + D -

20
**Example 4 6 3 / 4 2 * + // original expression**

2 4 2 * + // 6/3 evaluated // 4*2 evaluated 10 // 2+8 evaluated

21
**Example 5 5 4 * 10 2 - 2 / + 3 * // original expression**

/ + 3 * // 5*4 evaluated / + 3 * // 10-2 evaluated * // 8/2 evaluated 24 3 * // 20+4 evaluated 72 // 24*3 evaluated

22
**Using rooted trees to represent compound propositions**

Works exactly the same way as arithmetic expressions Innermost expression is bottom left subtree, with proposition(s) as leaf(s) and operator as root Root vertex is operator of outermost expression Using various traversal methods, can produce infix, prefix and postfix versions of compound proposition

23
**Example 6 Subtrees: Complete binary expression tree:**

Find the ordered rooted tree representing the compound proposition ((p q) (p q) Subtrees: Complete binary expression tree: / \ p q | p | q / \ | / \ / \ | | p q p q / \ | | p q | / \ p q

24
**Example 6 Preorder traversal yields the expression:**

/ \ | / \ / \ | | p q p q Preorder traversal yields the expression: p q p q Postorder traversal yields the expression: p q p q

25
Section 9.3 Tree Traversal

Similar presentations

© 2019 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