Open In App

Preorder Traversal of Binary Tree

Last Updated : 30 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:

  • The root node of the subtree is visited first.
  • Then the left subtree  is traversed.
  • At last, the right subtree is traversed.
How preorder traversal works

Algorithm for Preorder Traversal of Binary Tree

The algorithm for preorder traversal is shown as follows:

Preorder(root):

  1. If root is NULL then return
  2. Process root (For example, print root’s data)
  3. Preorder (root -> left)
  4. Preorder (root -> right)

Examples of Preorder Traversal

Input:

postorder_traversal_of_binary_tree

Output: ABC
Explanation: The Preorder traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node A, then visit the left node B and lastly the right node C.

Input:

postorder_traversal_of_binary_tree_1

Output: ABDEC

Input: NULL
Output:
Output is empty in this case.

Program to Implement Preorder Traversal of Binary Tree

Below is the code implementation of the preorder traversal:

C++
// C++ program for preorder traversals

#include <bits/stdc++.h>
using namespace std;

// Structure of a Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
    Node(int v)
    {
        data = v;
        left = right = nullptr;
    }
};

// Function to print preorder traversal
void printPreorder(struct Node* node)
{
    if (node == nullptr)
        return;

    // Deal with the node
    cout << node->data << " ";

    // Recur on left subtree
    printPreorder(node->left);

    // Recur on right subtree
    printPreorder(node->right);
}

// Driver code
int main()
{
    struct Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    root->right->right = new Node(6);

    // Function call
    cout << "Preorder traversal of binary tree is: \n";
    printPreorder(root);

    return 0;
}
C Java Python C# JavaScript

Output
Preorder traversal of binary tree is: 
1 2 4 5 3 6 

Complexity Analysis:

Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.
Auxiliary Space: 

  • O(1) if no recursion stack space is considered. 
  • Otherwise, O(h) where h is the height of the tree
  • In the worst case, h can be the same as N (when the tree is a skewed tree)
  • In the best case, h can be the same as logN (when the tree is a complete tree)

How does Preorder Traversal of Binary Tree work?

Consider the following tree:

Example of Binary Tree

Example of Binary Tree

If we perform a preorder traversal in this binary tree, then the traversal will be as follows:

Step 1: At first the root will be visited, i.e. node 1.

Node 1 is visited

Node 1 is visited

Step 2: After this, traverse in the left subtree. Now the root of the left subtree is visited i.e., node 2 is visited.

Node 2 is visited

Node 2 is visited

Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.

Node 4 is visited

Node 4 is visited

Step 4: There is no subtree of 4 and the left subtree of node 2 is visited. So now the right subtree of node 2 will be traversed and the root of that subtree i.e., node 5 will be visited.

Node 5 is visited

Node 5 is visited

Step 5: The left subtree of node 1 is visited. So now the right subtree of node 1 will be traversed and the root node i.e., node 3 is visited.

Node 3 is visited

Node 3 is visited

Step 6: Node 3 has no left subtree. So the right subtree will be traversed and the root of the subtree i.e., node 6 will be visited. After that there is no node that is not yet traversed. So the traversal ends.

The complete tree is visited

The complete tree is visited

So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -> 3 -> 6.

Use cases of Preorder Traversal:

Some use cases of preorder traversal are:

  • It is also useful to get the prefix expression from an expression tree.
  • Or any other case where we wish to traverse the root node first.

Related Articles:



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg