Tree Traversal

28
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden

description

Tree Traversal. Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden. Tree Anatomy. The children of a node are, themselves, trees, called subtrees. Root. Level 0. R. Level 1. S. T. Internal Node. Level 2. X. U. V. W. - PowerPoint PPT Presentation

Transcript of Tree Traversal

Page 1: Tree Traversal

1

Tree Traversal

Section 9.3Longin Jan LateckiTemple University

Based on slides byPaul Tymann, Andrew Watkins,

and J. van Helden

Page 2: Tree Traversal

2

Tree Anatomy

R

S

Y Z

X

T

U V W

Root

Internal Node

Leaf

Subtree

Level 0

Level 1

Level 2

Level 3

Child of X

Parent of Z and Y

The children of a node are, themselves, trees, called subtrees.

Page 3: Tree Traversal

3

Tree Traversals

• One of the most common operations performed on trees, are a tree traversals

• A traversal starts at the root of the tree and visits every node in the tree exactly once– visit means to process the data in the node

• Traversals are either depth-first or breadth-first

Page 4: Tree Traversal

4

Breadth First Traversals

• All the nodes in one level are visited

• Followed by the nodes the at next level

• Beginning at the root• For the sample tree

– 7, 6, 10, 4, 8, 13, 3, 5

7

6

3 5

4

10

8 13

Page 5: Tree Traversal

5

Queue and stack

• A queue is a sequence of elements such that each new element is added (enqueued) to one end, called the back of the queue, and an element is removed (dequeued) from the other end, called the front

• A stack is a sequence of elements such that each new element is added (or pushed) onto one end, called the top, and an element is removed (popped) from the same end

Page 6: Tree Traversal

6

Breadth first tree traversal with a queue• Enqueue root

• While queue is not empty

– Dequeue a vertex and write it to the output list

– Enqueue its children left-to-right

Step Output Queue

Page 7: Tree Traversal

7

Depth-First Traversals

• There are 6 different depth-first traversals– VLR (pre-order traversal)– VRL– LVR (in-order traversal)– RVL– RLV– LRV (post-order traversal)

Page 8: Tree Traversal

8

Pre-order Traversal: VLR

• Visit the node• Do a pre-order

traversal of the left subtree

• Finish with a pre-order traversal of the right subtree

• For the sample tree– 7, 6, 4, 3, 5, 10, 8, 13

7

6

3 5

4

10

8 13

Page 9: Tree Traversal

9

Pre-order tree traversal with a stack• Push root onto the stack

• While stack is not empty

– Pop a vertex off stack, and write it to the output list

– Push its children right-to-left onto stack

Step Output Stack

Page 10: Tree Traversal

10

Preorder Traversal

r

T1 T2 Tn

Step 1: Visit rStep 2: Visit T1 in preorder

Step n+1: Visit Tn in preorder

Step 3: Visit T2 in preorder

Page 11: Tree Traversal

11

Example

A R EY PM HJ Q T

A

R

EY

P

M

HJ

Q T

Page 12: Tree Traversal

12

Ordering of the preorder traversal is the same a the

Universal Address System with lexicographic ordering.

A R EY PM HJ Q T

A

R

EY

P

M

HJ

Q T

0

1 2 3

1.12.1

2.2

2.2.1 2.2.2 2.2.3

Page 13: Tree Traversal

13

In-order Traversal: LVR

• Do an in-order traversal of the left subtree

• Visit the node• Finish with an in-order

traversal of the right subtree

• For the sample tree– 3, 4, 5, 6, 7, 8, 10, 13

7

6

3 5

4

10

8 13

Page 14: Tree Traversal

14

Inorder Traversal

Step 1: Visit T1 in inorderStep 2: Visit r

Step n+1: Visit Tn in inorder

Step 3: Visit T2 in inorder

r

T1 T2 Tn

Page 15: Tree Traversal

15

Example

A R EY PM HJ Q T

A

R

EY

P

M

HJ

Q T

Page 16: Tree Traversal

16

inorder (t) if t != NIL: {

inorder (left[t]);write (label[t]);

inorder (right[t]); }

Inorder Traversal on a binary search tree.

Page 17: Tree Traversal

17

Post-order Traversal: LRV

7

6

3 5

4

10

8 13

• Do a post-order traversal of the left subtree

• Followed by a post-order traversal of the right subtree

• Visit the node• For the sample tree

– 3, 5, 4, 6, 8, 13, 10, 7

Page 18: Tree Traversal

18

Postorder Traversal

Step 1: Visit T1 in postorderStep 2: Visit T2 in postorder

Step n+1: Visit r

Step n: Visit Tn in postorder

r

T1 T2 Tn

Page 19: Tree Traversal

19

Example

A R EYP MHJ Q T

A

R

EY

P

M

HJ

Q T

Page 20: Tree Traversal

20

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

Page 21: Tree Traversal

21

Example

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

+

y 2

/

+

Page 22: Tree Traversal

22

Infix Notation

+

– +

/

+ 2

x y x 3 y 2

• Traverse in inorder (LVR) adding parentheses for each operation

x + y( ) 2( )+ x – 3( ) / y+2( )( )( )

Page 23: Tree Traversal

23

Prefix Notation(Polish Notation)

• Traverse in preorder (VLR)

x+ y 2+ x– 3/ y+ 2

+

– +

/

+ 2

x y x 3 y 2

Page 24: Tree Traversal

24

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

Page 25: Tree Traversal

25

Example

+ / + 2 2 2 / – 3 2 + 1 0+ / + 2 2 2 / – 3 2 1+ / + 2 2 2 / 1 1+ / + 2 2 2 1+ / 4 2 1

+ 2 1

3

Page 26: Tree Traversal

26

Postfix Notation(Reverse Polish)

• Traverse in postorder (LRV)

x +y 2 +x –3 /y +2

+

– +

/

+ 2

x y x 3 y 2

Page 27: Tree Traversal

27

• 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

Page 28: Tree Traversal

28

Example

3

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

2 1 +