0% found this document useful (0 votes)
17 views4 pages

2 Binary Tree Traversal

The document discusses binary tree traversal methods, which are essential for displaying the nodes of a binary tree in a specific order. It outlines three types of traversals: In-Order, Pre-Order, and Post-Order, providing detailed explanations and examples for each method. The traversal orders for the given binary tree are listed as I-D-J-B-F-A-G-K-C-H for In-Order, A-B-D-I-J-F-C-G-K-H for Pre-Order, and I-J-D-F-B-K-G-H-C-A for Post-Order.

Uploaded by

banmustafa66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

2 Binary Tree Traversal

The document discusses binary tree traversal methods, which are essential for displaying the nodes of a binary tree in a specific order. It outlines three types of traversals: In-Order, Pre-Order, and Post-Order, providing detailed explanations and examples for each method. The traversal orders for the given binary tree are listed as I-D-J-B-F-A-G-K-C-H for In-Order, A-B-D-I-J-F-C-G-K-H for Pre-Order, and I-J-D-F-B-K-G-H-C-A for Post-Order.

Uploaded by

banmustafa66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture 2- Binary Tree Traversal

When we wanted to display a binary tree, we need to follow some order in which
all the nodes of that binary tree must be displayed. In any binary tree displaying
order of nodes depends on the traversal method

Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.

There are three types of binary tree traversals.

In - Order Traversal

Pre - Order Traversal

Post - Order Traversal


Consider the above binary tree...

1. In - Order Traversal ( leftChild - root - rightChild )

In In-Order traversal, the root node is visited between left child and right child. In
this traversal, the left child node is visited first, then the root node is visited and
later we go for visiting right child node. This in-order traversal is applicable for
every root node of all subtrees in the tree. This is performed recursively for all
nodes in the tree.

In the above example of binary tree, first we try to visit left child of root node 'A',
but A's left child is a root node for left subtree. so, we try to visit its (B's) left child
'D' and again D is a root for subtree with nodes D, I and J. So, we try to visit its left
child 'I' and it is the left most child. So first we visit 'I' then go for its root node 'D'
and later we visit D's right child 'J'. With this we have completed the left part of
node B. Then visit 'B' and next B's right child 'F' is visited. With this we have
completed left part of node A. Then visit root node 'A'. With this we have
completed left and root parts of node A. Then we go for right part of the node A. In
right of A again there is a subtree with root C. So, go for left child of C and again it
is a subtree with root G. But G does not have left part so we visit 'G' and then visit
G's right child K. With this we have completed the left part of node C. Then visit
root node 'C' and next visit C's right child 'H' which is the right most child in the
tree so we stop the process.

That means here we have visited in the order of I - D - J - B - F - A - G - K - C -


H using In-Order Traversal.
In-Order Traversal for above example of binary tree is

I-D-J-B-F-A-G-K-C-H

2. Pre - Order Traversal ( root - leftChild - rightChild )

In Pre-Order traversal, the root node is visited before left child and right child
nodes. In this traversal, the root node is visited first, then its left child and later its
right child. This pre-order traversal is applicable for every root node of all subtrees
in the tree.

In the above example of binary tree, first we visit root node 'A' then visit its left
child 'B' which is a root for D and F. So we visit B's left child 'D' and again D is a
root for I and J. So we visit D's left child 'I' which is the left most child. So next we
go for visiting D's right child 'J'. With this we have completed root, left and right
parts of node D and root, left parts of node B. Next visit B's right child 'F'. With
this we have completed root and left parts of node A. So we go for A's right child
'C' which is a root node for G and H. After visiting C, we go for its left child 'G'
which is a root for node K. So next we visit left of G, but it does not have left child
so we go for G's right child 'K'. With this we have completed node C's root and left
parts. Next visit C's right child 'H' which is the right most child in the tree. So we
stop the process.

That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-
Order Traversal.

Pre-Order Traversal for above example binary tree is

A-B-D-I-J-F-C-G-K-H

3-Post - Order Traversal ( leftChild - rightChild - root )


In Post-Order traversal, the root node is visited after left child and right child. In
this traversal, left child node is visited first, then its right child and then its root
node. This is recursively performed until the right most node is visited.

Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-


Order Traversal.

Post-Order Traversal for above example binary tree is

I-J-D-F-B-K-G-H-C-A

You might also like