14
10.3 Tree Transversal

10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Embed Size (px)

Citation preview

Page 1: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

10.3 Tree Transversal

Page 2: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Pre/post fix notation and order• See

handout. a

. b c

. d e f g

h i j k

Page 3: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Preorder transversal- root first, then left to right

• a

. b c

. d e f g

h i j k

Page 4: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Inorder transversal- left tree, then root, then rest of trees- left to right

• a

b c

d e f g

h i j k•

Page 5: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Postorder transversal- left to right, then root last

• a

. b c

. d e f g

h i j k

Page 6: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Infix, prefix, and postfix• See handout• Infix order is ambiguous – so parentheses are

required• Prefix and postfix are not– so parentheses are

unnecessary• Prefix- “Polish notation”– Read from R to L– Operations are on the L

• Postfix– Read from L to R– Operations are on the R

Page 7: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Example– transverse in 3 ways, and evaluate each notation

+

*

3 2 5 2

• Preorder- prefix

• Inorder-infix

• Postorder-postfix

Page 8: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Example- prefix

• Evaluate a prefix expression+ - * 2 3 5 / 2 3 4

Page 9: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Ex- postfix

7 2 3 * - 4 9 3 / +

Page 10: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Example– transverse in 3 ways, and evaluate each notation

+

*

5 2 3 4

• Preorder- prefix

• Inorder-infix

• Postorder-postfix

-

*

Page 11: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Find the value of each prefix expression (R to L,operations on the L)

• - * 2 / 8 4 3

• - * 3 3 * 4 2 5

Page 12: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Find the value of each prefix expression (R to L,operations on the L)

• + - 3 2 2 3 / 6 – 4 2

• * + 3 + 3 3 + 3 3 3

Page 13: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Find the value of each postfix expression (L to R, operations on R)

• 5 2 1 - - 3 1 4 + + *

Page 14: 10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k

Find the value of each postfix expression (L to R, operations on R)

• 9 3 / 5 + 7 2 - *

• 3 2 * 2 5 3 – 8 4 / * -