section 9.3

25
1 Section 9.3 Tree Traversal

Upload: avye-gentry

Post on 31-Dec-2015

33 views

Category:

Documents


0 download

DESCRIPTION

Section 9.3. Tree Traversal. 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 - PowerPoint PPT Presentation

TRANSCRIPT

1

Section 9.3

Tree Traversal

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

K

I R

K W O

O D

Original tree: Results:K

IKWROOD

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

K

I R

K W O

O D

Original tree: Results:KI

W

K

RO

OD

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

I R

K W O

O D

Original tree: Results: K

W

I

O

D

O

R

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 1Subtrees of binary expression tree for (x+y)2 + (x-4)/3:

+ / \x y

- / \x 4

^ / \ + 2 / \x y

/ \ - 3 / \x 4

Complete binary expression treefor (x+y)2 + (x-4)/3:

+ / \ ^ / \ / \ + 2 - 3 / \ / \ x y x 4

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

+ * 2 3 9 // 4/2 evaluated

+ 6 9 // 2*3 evaluated

15 // 6+9 evaluated

17

Example 3

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

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

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

* - 7 5 2 // 4+3 evaluated

* 2 2 // 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 46 3 / 4 2 * + // original expression

2 4 2 * + // 6/3 evaluated

2 8 + // 4*2 evaluated

10 // 2+8 evaluated

21

Example 55 4 * 10 2 - 2 / + 3 * // original expression

20 10 2 - 2 / + 3 * // 5*4 evaluated

20 8 2 / + 3 * // 10-2 evaluated

20 4 + 3 * // 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 6Find the ordered rooted tree representing the compound proposition ((p q) (p q)

Subtrees: / \p q

| p

| q

| / \p q

/ \ | |p q

Complete binaryexpression tree:

/ \ | / \ / \ | |p q p q

24

Example 6 / \ | / \ / \ | |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