1 section 9.3 tree traversal. 2 universal address system in ordered rooted trees, vertices may be...

25
1 Section 9.3 Tree Traversal

Post on 21-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

1

Section 9.3

Tree Traversal

Page 2: 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

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

Page 3: 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

3

Universal Address System of an Ordered Rooted Tree

Page 4: 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

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

Page 5: 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

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

Page 6: 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

6

Pre-order traversal in action

K

I R

K W O

O D

Original tree: Results:K

IKWROOD

Page 7: 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

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

Page 8: 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

8

In-order traversal in action

K

I R

K W O

O D

Original tree: Results:KI

W

K

RO

OD

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

Page 16: 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

16

Example 2

+ * / 4 2 3 9 // original expression

+ * 2 3 9 // 4/2 evaluated

+ 6 9 // 2*3 evaluated

15 // 6+9 evaluated

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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 -

Page 20: 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

20

Example 46 3 / 4 2 * + // original expression

2 4 2 * + // 6/3 evaluated

2 8 + // 4*2 evaluated

10 // 2+8 evaluated

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

25

Section 9.3

Tree Traversal