2. INTRODUCTION
• In computer science, tree traversal (also
known as tree search) is a form of graph
traversal and refers to the process of visiting
(checking and/or updating) each node in
a tree data structure, exactly once.
• Such traversals are classified by the order in
which the nodes are visited. The following
algorithms are described for a binary tree, but
they may be generalized to other trees as well.
3. TYPES OF TECHNIQUES
PRE ORDER (Root, Left, Right)
• Check if the current node is empty / null.
• Display the data part of the root (or current
node).
• Traverse the left subtree by recursively calling
the pre-order function.
• Traverse the right subtree by recursively
calling the pre-order function.
4. Until all nodes are traversed −
• Step 1 − Visit root node.
• Step 2 − Recursively traverse left subtree.
• Step 3 − Recursively traverse right subtree.
5. EXAMPLE:
• We start from A, and following pre-order traversal, we
first visit A itself and then move to its left subtree B.
• B is also traversed pre-order. The process goes on until all
the nodes are visited. The output of pre-order traversal
of this tree will be −
A → B → D → E → C → F → G
6. INORDER(Left, root, right)
• Check if the current node is empty / null.
• Traverse the left subtree by recursively calling
the in-order function.
• Display the data part of the root (or current
node).
• Traverse the right subtree by recursively
calling the in-order function.
7. Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
8. • EXAMPLE:
• We start from A, and following in-order traversal,
we move to its left subtree B. B is also traversed
in-order.
• The process goes on until all the nodes are
visited. The output of inorder traversal of this
tree will be −
D → B → E → A → F → C → G
9. POSTORDER(Left, right, root)
• Check if the current node is empty / null.
• Traverse the left subtree by recursively calling
the post-order function.
• Traverse the right subtree by recursively
calling the post-order function.
• Display the data part of the root (or current
node).
10. Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
• Step 3 − Visit root node.
11. EXAMPLE:
• We start from A, and following Post-order
traversal, we first visit the left subtree B. B is also
traversed post-order.
• The process goes on until all the nodes are
visited. The output of post-order traversal of this
tree will be −
D → E → B → F → G → C → A