tree traversals (in-order, pre-order and post-order)

28
Traversal Techniques In Binary Tree

Upload: raj-upadhyay

Post on 15-Apr-2017

357 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Tree Traversals (In-order, Pre-order and Post-order)

Traversal Techniques In Binary Tree

Page 2: Tree Traversals (In-order, Pre-order and Post-order)

How to Find PRI-ORDER, IN-ORDER, POST-ORDER Traversal in TREE.

• As we all know there are certain rules (or manner) that we have to follow for Traversal in tree.

• Pri-Order :- Root, Left, Right. • In-Order :-Left, Root, Right.• Post-Order :-Left, Right, Root .

• Sometimes it’s confusing that what manner we have to follow for PRI-Oder, IN-Order, POSTER-Order Traversal.

• So now we see how to do In-order, Pri-order and Post-order traversal in different manner.

Page 3: Tree Traversals (In-order, Pre-order and Post-order)

Let’s learn basic things

Page 4: Tree Traversals (In-order, Pre-order and Post-order)

Left, Right, Bottom side of NODE.

50Left-hand side of Node Right-hand side of Node

Bottom side of Node

Page 5: Tree Traversals (In-order, Pre-order and Post-order)

FOR PER-ORDER TRAVERSAL YOU HAVE

TO FOLLOW THIS STEPS.

Page 6: Tree Traversals (In-order, Pre-order and Post-order)

For PRI-ORDER TRAVERSAL.IF YOU ARE IN LEFT-HAND SIDE OF NODE THEN YOU

HAVE TO WRITE THAT VALUE IN OUTPUT.

50

Pri-Order:-

50

Page 7: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN BOTTOM OF THE NODE THEN YOU DON’T HAVE TO WRITE ANYTHING IN OUTPUT.

50

Pri-Order:- NULL

PRI-ORDER TRAVERSAL

Page 8: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN RIGHT-HAND SIDE OF THE NODE THEN ALSO YOU DON’T HAVE TO WRITE ANTHING IN OUTPUT.

50

Pri-Order:- NULL

PRI-ORDER TRAVERSAL

Page 9: Tree Traversals (In-order, Pre-order and Post-order)

Now see how to follow this steps in TREE Traversal.

Page 10: Tree Traversals (In-order, Pre-order and Post-order)

50

35

20 45 7060

80

Example.

Page 11: Tree Traversals (In-order, Pre-order and Post-order)

Draw one imaginary path around given TREE

Step 1:

Page 12: Tree Traversals (In-order, Pre-order and Post-order)

35

20 45 9060

80

5050

PRI-ORDER:- .

20

35

45

80

60 90

IF YOU ARE IN LEFT HAND SIDE OF NODE THEN YOU HAVE TO WRITE THAT VALUE IN OUTPUT

Page 13: Tree Traversals (In-order, Pre-order and Post-order)

FOR IN-ORDER TRAVERSAL YOU HAVE

TO FOLLOW THIS STEPS.

Page 14: Tree Traversals (In-order, Pre-order and Post-order)

For IN-ORDER TRAVERSAL.IF YOU ARE IN LEFT-HAND SIDE OF NODE THEN YOU

DON’T HAVE TO WRITE ANYTHING IN OUTPUT.

50

IN-Order:-NULL

50

Page 15: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN BOTTOM OF THE NODE THEN YOU HAVE TO WRITE THAT VALUE IN OUTPUT.

50

IN-Order:-

50

IN-ORDER TRAVERSAL

Page 16: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN RIGHT-HAND SIDE OF THE NODE THEN YOU DON’T HAVE TO WRITE ANYTHING IN OUTPUT.

50

IN-Order:- NULL

IN-ORDER TRAVERSAL

Page 17: Tree Traversals (In-order, Pre-order and Post-order)

50

35

20 45 7060

80

Example.

Page 18: Tree Traversals (In-order, Pre-order and Post-order)

Draw one imaginary path around given TREE

Step 1:

Page 19: Tree Traversals (In-order, Pre-order and Post-order)

50

35

20 45 9060

80

Let’s see one example.IF YOU ARE IN BOTTOM SIDE OF NODE THEN YOU HAVE TO WRITE THAT VALUE IN OUTPUT

20

IN-ORDER:- .

35

45

50

60

80

90

Page 20: Tree Traversals (In-order, Pre-order and Post-order)

FOR POST-ORDER TRAVERSAL YOU

HAVE TO FOLLOW THIS STEPS.

Page 21: Tree Traversals (In-order, Pre-order and Post-order)

For POST-ORDER TRAVERSAL.IF YOU ARE IN LEFT-HAND SIDE OF NODE THEN YOU

DON’T HAVE TO WRITE THAT VALUE IN OUTPUT.

50

POST-Order:-NULL

50

Page 22: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN BOTTOM OF THE NODE THEN YOU DON’T HAVE TO WRITE ANYTING IN OUTPUT.

50

POST-Order:-NULL

POST-ORDER TRAVERSAL

Page 23: Tree Traversals (In-order, Pre-order and Post-order)

• IF YOU ARE IN RIGHT-HAND SIDE OF THE NODE THEN YOU HAVE TO WRITE THAT VALUE IN OUTPUT.

50

POST-Order:-

50

POST-ORDER TRAVERSAL

Page 24: Tree Traversals (In-order, Pre-order and Post-order)

50

35

20 45 7060

80

Example.

Page 25: Tree Traversals (In-order, Pre-order and Post-order)

Draw one imaginary path around given TREE

Step 1:

Page 26: Tree Traversals (In-order, Pre-order and Post-order)

50

35

20 45 9060

80

Let’s see one example.

Post-Order:- .

20

IF YOU ARE IN RIGHT-HAND SIDE OF NODE THEN YOU HAVE TO WRITE THAT VALUE IN OUTPUT

45

35

60 90

80

50

Page 27: Tree Traversals (In-order, Pre-order and Post-order)

Conclusion

• PRI-ORDER :- Consider the value If you are in LEFT-HAND SIDE of the NODE.

• IN-ORDER :- Consider the value if you are in BOTTOM SIDE of the NODE

• POST-ORDER:- Consider the value If you are in RIGHT-HAND SIDE of the NODE.

50

50

50

Page 28: Tree Traversals (In-order, Pre-order and Post-order)

• Still if you have any queries then ask them on. Email:[email protected]

THANK YOU

Prepared by :- RAJ UPADHYAY