Skip to content
Related Articles

Related Articles

Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Sep, 2022
View Discussion
Improve Article
Save Article

Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. 

Input: 

A Binary Search Tree

Output: 
Inorder Traversal: 10 20 30 100 150 200 300
Preorder Traversal: 100 20 10 30 200 150 300
Postorder Traversal: 10 30 20 150 300 200 100

Input: 

Binary Search Tree

Output: 
Inorder Traversal: 8 12 20 22 25 30 40
Preorder Traversal: 22 12 8 20 30 25 40
Postorder Traversal: 8 20 12 25 40 30 22

Inorder Traversal:

Below is the idea to solve the problem:

At first traverse left subtree then visit the root and then traverse the right subtree.

Follow the below steps to implement the idea:

  • Traverse left subtree
  • Visit the root and print the data.
  • Traverse the right subtree

The inorder traversal of the BST gives the values of the nodes in sorted order. To get the decreasing order visit the right, root, and left subtree.

Below is the implementation of the inorder traversal.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Class describing a node of tree
class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int v)
    {
        this->data = v;
        this->left = this->right = NULL;
    }
};
 
// Inorder Traversal
void printInorder(Node* node)
{
    if (node == NULL)
        return;
 
    // Traverse left subtree
    printInorder(node->left);
 
    // Visit node
    cout << node->data << " ";
 
    // Traverse right subtree
    printInorder(node->right);
}
 
// Driver code
int main()
{
    // Build the tree
    Node* root = new Node(100);
    root->left = new Node(20);
    root->right = new Node(200);
    root->left->left = new Node(10);
    root->left->right = new Node(30);
    root->right->left = new Node(150);
    root->right->right = new Node(300);
 
    // Function call
    cout << "Inorder Traversal: ";
    printInorder(root);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
// Class describing a node of tree
class Node {
 
    int data;
    Node left;
    Node right;
    Node(int v)
    {
        this.data = v;
        this.left = this.right = null;
    }
}
 
class GFG {
    // Inorder Traversal
    public static void printInorder(Node node)
    {
        if (node == null)
            return;
 
        // Traverse left subtree
        printInorder(node.left);
 
        // Visit node
        System.out.print(node.data + " ");
 
        // Traverse right subtree
        printInorder(node.right);
    }
    // Driver Code
    public static void main(String[] args)
    {
        // Build the tree
        Node root = new Node(100);
        root.left = new Node(20);
        root.right = new Node(200);
        root.left.left = new Node(10);
        root.left.right = new Node(30);
        root.right.left = new Node(150);
        root.right.right = new Node(300);
 
        // Function call
        System.out.print("Inorder Traversal: ");
        printInorder(root);
    }
}
 
// This code is contributed by Rohit Pradhan


C#




// Include namespace system
using System;
 
 
// Class describing a node of tree
public class Node
{
    public int data;
    public Node left;
    public Node right;
    public Node(int v)
    {
        this.data = v;
        this.left = this.right = null;
    }
}
public class GFG
{
    // Inorder Traversal
    public static void printInorder(Node node)
    {
        if (node == null)
        {
            return;
        }
        // Traverse left subtree
        GFG.printInorder(node.left);
        // Visit node
        Console.Write(node.data.ToString() + " ");
        // Traverse right subtree
        GFG.printInorder(node.right);
    }
    // Driver Code
    public static void Main(String[] args)
    {
        // Build the tree
        var root = new Node(100);
        root.left = new Node(20);
        root.right = new Node(200);
        root.left.left = new Node(10);
        root.left.right = new Node(30);
        root.right.left = new Node(150);
        root.right.right = new Node(300);
        // Function call
        Console.Write("Inorder Traversal: ");
        GFG.printInorder(root);
    }
}


Output

Inorder Traversal: 10 20 30 100 150 200 300 

Time complexity: O(N), Where N is the number of nodes.
Auxiliary Space: O(h), Where h is the height of tree

Preorder Traversal:

Below is the idea to solve the problem:

At first visit the root then traverse left subtree and then traverse the right subtree.

Follow the below steps to implement the idea:

  • Visit the root and print the data.
  • Traverse left subtree
  • Traverse the right subtree

Below is the implementation of the preorder traversal.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Class describing a node of tree
class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int v)
    {
        this->data = v;
        this->left = this->right = NULL;
    }
};
 
// Preorder Traversal
void printPreOrder(Node* node)
{
    if (node == NULL)
        return;
 
    // Visit Node
    cout << node->data << " ";
 
    // Traverse left subtree
    printPreOrder(node->left);
 
    // Traverse right subtree
    printPreOrder(node->right);
}
 
// Driver code
int main()
{
    // Build the tree
    Node* root = new Node(100);
    root->left = new Node(20);
    root->right = new Node(200);
    root->left->left = new Node(10);
    root->left->right = new Node(30);
    root->right->left = new Node(150);
    root->right->right = new Node(300);
 
    // Function call
    cout << "Preorder Traversal: ";
    printPreOrder(root);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
// Class describing a node of tree
class Node {
 
  int data;
  Node left;
  Node right;
  Node(int v)
  {
    this.data = v;
    this.left = this.right = null;
  }
}
 
class GFG {
 
  // Preorder Traversal
  public static void printPreorder(Node node)
  {
    if (node == null)
      return;
 
    // Visit node
    System.out.print(node.data + " ");
 
    // Traverse left subtree
    printPreorder(node.left);
 
    // Traverse right subtree
    printPreorder(node.right);
  }
 
  public static void main(String[] args)
  {
    // Build the tree
    Node root = new Node(100);
    root.left = new Node(20);
    root.right = new Node(200);
    root.left.left = new Node(10);
    root.left.right = new Node(30);
    root.right.left = new Node(150);
    root.right.right = new Node(300);
 
    // Function call
    System.out.print("Preorder Traversal: ");
    printPreorder(root);
  }
}
 
// This code is contributed by lokeshmvs21.


C#




// Include namespace system
using System;
 
 
// Class describing a node of tree
public class Node
{
    public int data;
    public Node left;
    public Node right;
    public Node(int v)
    {
        this.data = v;
        this.left = this.right = null;
    }
}
public class GFG
{
    // Preorder Traversal
    public static void printPreorder(Node node)
    {
        if (node == null)
        {
            return;
        }
        // Visit node
        Console.Write(node.data.ToString() + " ");
        // Traverse left subtree
        GFG.printPreorder(node.left);
        // Traverse right subtree
        GFG.printPreorder(node.right);
    }
    public static void Main(String[] args)
    {
        // Build the tree
        var root = new Node(100);
        root.left = new Node(20);
        root.right = new Node(200);
        root.left.left = new Node(10);
        root.left.right = new Node(30);
        root.right.left = new Node(150);
        root.right.right = new Node(300);
        // Function call
        Console.Write("Preorder Traversal: ");
        GFG.printPreorder(root);
    }
}


Output

Preorder Traversal: 100 20 10 30 200 150 300 

Time complexity: O(N), Where N is the number of nodes.
Auxiliary Space: O(H), Where H is the height of the tree

Postorder Traversal:

Below is the idea to solve the problem:

At first traverse left subtree then traverse the right subtree and then visit the root.

Follow the below steps to implement the idea:

  • Traverse left subtree
  • Traverse the right subtree
  • Visit the root and print the data.

Below is the implementation of the postorder traversal:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Class to define structure of a node
class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int v)
    {
        this->data = v;
        this->left = this->right = NULL;
    }
};
 
// PostOrder Traversal
void printPostOrder(Node* node)
{
    if (node == NULL)
        return;
 
    // Traverse left subtree
    printPostOrder(node->left);
 
    // Traverse right subtree
    printPostOrder(node->right);
 
    // Visit node
    cout << node->data << " ";
}
 
// Driver code
int main()
{
    Node* root = new Node(100);
    root->left = new Node(20);
    root->right = new Node(200);
    root->left->left = new Node(10);
    root->left->right = new Node(30);
    root->right->left = new Node(150);
    root->right->right = new Node(300);
 
    // Function call
    cout << "PostOrder Traversal: ";
    printPostOrder(root);
    cout << "\n";
 
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
// Class describing a node of tree
 
class GFG {
   
 static class Node {
 
  int data;
  Node left;
  Node right;
  Node(int v)
  {
    this.data = v;
    this.left = this.right = null;
  }
}
 
  // Preorder Traversal
  public static void printPreorder(Node node)
  {
    if (node == null)
      return;
 
    // Traverse left subtree
    printPreorder(node.left);
 
    // Traverse right subtree
    printPreorder(node.right);
     
      // Visit node
    System.out.print(node.data + " ");
  }
 
  public static void main(String[] args)
  {
    // Build the tree
    Node root = new Node(100);
    root.left = new Node(20);
    root.right = new Node(200);
    root.left.left = new Node(10);
    root.left.right = new Node(30);
    root.right.left = new Node(150);
    root.right.right = new Node(300);
 
    // Function call
    System.out.print("Preorder Traversal: ");
    printPreorder(root);
  }
}


C#




// Include namespace system
using System;
 
 
// Class describing a node of tree
public class Node
{
    public int data;
    public Node left;
    public Node right;
    public Node(int v)
    {
        this.data = v;
        this.left = this.right = null;
    }
}
public class GFG
{
    // Preorder Traversal
    public static void printPreorder(Node node)
    {
        if (node == null)
        {
            return;
        }
        // Traverse left subtree
        GFG.printPreorder(node.left);
        // Traverse right subtree
        GFG.printPreorder(node.right);
        // Visit node
        Console.Write(node.data.ToString() + " ");
    }
    public static void Main(String[] args)
    {
        // Build the tree
        var root = new Node(100);
        root.left = new Node(20);
        root.right = new Node(200);
        root.left.left = new Node(10);
        root.left.right = new Node(30);
        root.right.left = new Node(150);
        root.right.right = new Node(300);
        // Function call
        Console.Write("Preorder Traversal: ");
        GFG.printPreorder(root);
    }
}


Output

PostOrder Traversal: 10 30 20 150 300 200 100 

Time complexity: O(N), Where N is the number of nodes.
Auxiliary Space: O(H), Where H is the height of the tree


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!