Preorder traversal is a type of depth-first traversal used in binary trees which starts from root node and process the all nodes of the tree. In preorder traversal, the nodes are recursively visited and processed in the following order:
- Visit and process the data stored in the current node( In starting it is root node).
- Traverse the left subtree in preorder- After processing the data at the current node go to the left child node if it is present, apply the preorder traversal on the left child node.
- Traverse the right subtree in preorder. - Once data at the current node and whole left subtree of current node is processed go to the right node and apply preorder traversal on the right node.
1. Here node 1 is the root node, so traversal will start from node 1. According to ROOT->LEFT->RIGHT formula data at node 1 will be processed first and then preorder will be applied on the left subtree of node 1 which is subtree started with node 2 which is represented in the below figure.
2. Next data at node 2 will be processed and there is no left child for node 2, so according to RLR formula preorder will be applied on right subtree which is subtree with root node 4.
4. Next data at node 6 will be processed and since node 6 is leaf node(no left and right child) processing will backtrack to its immediate parent node which is node 4 by marking node 6 as processed . Here according to RLR formula RL part has been processed but last R part is remaining, so preorder will be applied on the right subtree of node 4 which is subtree started with node 7.
6. According to above mentioned steps all remaining nodes will be processed as given in the below figures.
Method 1: Using STACK
preorderByStack(root)
- Let ST is a stack
- ST.Push(root)
- While ST is not empty:
- {
-
item=ST.pop()
-
Process item.data(print item.data)
-
If item.right is not null then ST.Push(item.right)
-
If item.left is not null then ST.Push(item.left)
- }
Method 2: Using Recursion
From the above figure we can easily see preorder traversal can be implemented recursively. As we know from Recursion chapter, while designing recursive solution there are 3 steps to follow which are given below: Hypothesis - In designing recursive solution we should define a function and its necessary parameter which will solve the given problem. In our case while applying preorder traversal to any node we need the node data, its left child and right child node which will be accessed from given node itself. So only current node is sufficient input parameter where processing will start from root node. We are defining below function which will solve our problem.
Preorder(root)
Inductive Step - Here we need to define the problem in terms of subproblems of itself. Here we know from the above mentioned formula preorder works on formula RLR(process data at root, apply preorder on left subtree, apply preorder on right subtree). So we can define preorder traversal as below in inductive step.
Preorder(root)=(process data at root)=>Preorder(root.left)=>Preorder(root.right)
or
Preorder(root)=
(Process data at root)
Preorder(root.left)
Preorder(root.right)
Base Condition - Function body defined in the Inductive step will not work as after leaf node left or right child will be null so there must be some condition to handle this boundary which is called Base Condition. Here if current node is null means no subtree to process. So there must not be any processing when current node is null as given below:
Preorder(root)=
if root is null do nothing and return
(Process data at root)
Preorder(root.left)
Preorder(root.right)
So below will be preorder tree traversal algorithm for a given binary tree using recursion
{
- If root is null then do nothing and return
- Process root.data(print root.data)
- Preorder(root.left)
- Preorder(root.right)