Tree Traversal

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

Binary Tree Traversal – Preorder, Inorder and

Postorder
Binary Tree

• A tree in which every node can have a maximum of two children is called as Binary
Tree.
Binary tree traversal

• Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one
logical way to traverse them, trees can be traversed in different ways. Following are the
generally used ways for traversing trees.
Inorder Traversal

Using recursion, the function will call itself on the left sub-tree, then print the current value, and
then call itself on the right sub-tree.
The result is that it prints the tree's values.... in order.

Inorder (Left, Root, Right)

Uses of Inorder
• In case of binary search trees (BST), Inorder traversal gives nodes in ascending order.
Inorder Traversal Example

Inorder (Left, Root, Right)


8

4
10

2 6 9 11

0 1 2 3 4 5 6
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6 8
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6 8
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6 8 9
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6 8 9 10
Inorder (Left, Root, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 4 6 8 9 10 11
Preorder Traversal

Using recursion, the function will print the current value, call itself on the left sub-tree, and
then call itself on the right sub-tree.
The result is that it prints the tree's values.... pre order.

Preorder (Root, Left, Right)

Uses of preorder
• Preorder traversal is used to get prefix expression on of an expression tree.
Preorder Traversal Example

Preorder (Root, Left, Right)


8

4
10

2 6 9 11

0 1 2 3 4 5 6
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6 10
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6 10 9
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6 10 9
Preorder (Root, Left, Right)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
8 4 2 6 10 9 11
Postorder Traversal

Using recursion, the function will call itself on the left sub-tree, then call itself on the right sub-
tree and print the current value.
The result is that it prints the tree's values.... post order.

Postorder (Left, Right, Root)

Uses of preorder
• Postorder traversal is also useful to get the postfix expression of an expression tree.
Postorder Traversal Example

Postorder (Left, Right, Root)


8

4
10

2 6 9 11

0 1 2 3 4 5 6
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4 9
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4 9
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4 9 11
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4 9 11 10
Postorder (Left, Right, Root)
8

4
10

2 6 9 11

0 1 2 3 4 5 6
2 6 4 9 11 10 8
Binary tree using the preorder and inorder traversal

Given:
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

QBKCFA PEDHR
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q KCFA
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q KCFA
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q A

KCF
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q A

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q A

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B PEDHR

Q A

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B P

Q A EDHR

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B P

Q A
D

C E HR

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B P

Q A
D

C E HR

K F
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B P

Q A
D

C E R

K F
H
Pre-Order Traversal : G B Q A C K F P D E R H
In-Order Traversal : Q B K C F A G P E D H R

B P

Q A
D

C E R

K F
H
Example

Inorder: H D I B J E A K F L C G
Preorder: A B D H I E J C F K L G

Inorder: H D I B J E A K F L C G
Postorder: H I D J E B K L F C G A
Example

Inorder: 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90
Preorder: 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90

Inorder: 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90
Postorder: 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25
Binary tree using the given postorder and inorder
traversal
Given:
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1

Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1

4, 8, 2, 5 6, 3, 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

4, 8, 2, 5 3

6 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

4, 8, 2, 5 3

6 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

4, 8, 2, 5 3

6 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

2 3

4, 8 5 6 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

2 3

4, 8 5 6 7
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

2 3

4 5 6 7

8
Inorder: 4, 8, 2, 5, 1, 6, 3, 7
Postorder: 8, 4, 5, 2, 6, 7, 3, 1
1

2 3

4 5 6 7

8
THANK YOU

You might also like