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
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++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int v)
{
this ->data = v;
this ->left = this ->right = NULL;
}
};
void printInorder(Node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
cout << node->data << " " ;
printInorder(node->right);
}
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);
cout << "Inorder Traversal: " ;
printInorder(root);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node left;
Node right;
Node( int v)
{
this .data = v;
this .left = this .right = null ;
}
}
class GFG {
public static void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String[] args)
{
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 );
System.out.print( "Inorder Traversal: " );
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , v):
self .left = None
self .right = None
self .data = v
def printInorder(root):
if root:
printInorder(root.left)
print (root.data,end = " " )
printInorder(root.right)
if __name__ = = "__main__" :
root = Node( 100 )
root.left = Node( 20 )
root.right = Node( 200 )
root.left.left = Node( 10 )
root.left.right = Node( 30 )
root.right.left = Node( 150 )
root.right.right = Node( 300 )
print ( "Inorder Traversal:" ,end = " " )
printInorder(root)
|
C#
using System;
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
{
public static void printInorder(Node node)
{
if (node == null )
{
return ;
}
GFG.printInorder(node.left);
Console.Write(node.data.ToString() + " " );
GFG.printInorder(node.right);
}
public static void Main(String[] args)
{
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);
Console.Write( "Inorder Traversal: " );
GFG.printInorder(root);
}
}
|
Javascript
class Node {
constructor(v) {
this .left = null ;
this .right = null ;
this .data = v;
}
}
function printInorder(root)
{
if (root)
{
printInorder(root.left);
console.log(root.data);
printInorder(root.right);
}
}
if ( true )
{
let 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);
console.log( "Inorder Traversal:" );
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
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++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int v)
{
this ->data = v;
this ->left = this ->right = NULL;
}
};
void printPreOrder(Node* node)
{
if (node == NULL)
return ;
cout << node->data << " " ;
printPreOrder(node->left);
printPreOrder(node->right);
}
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);
cout << "Preorder Traversal: " ;
printPreOrder(root);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node left;
Node right;
Node( int v)
{
this .data = v;
this .left = this .right = null ;
}
}
class GFG {
public static void printPreorder(Node node)
{
if (node == null )
return ;
System.out.print(node.data + " " );
printPreorder(node.left);
printPreorder(node.right);
}
public static void main(String[] args)
{
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 );
System.out.print( "Preorder Traversal: " );
printPreorder(root);
}
}
|
Python3
class Node:
def __init__( self , v):
self .data = v
self .left = None
self .right = None
def printPreOrder(node):
if node is None :
return
print (node.data, end = " " )
printPreOrder(node.left)
printPreOrder(node.right)
if __name__ = = "__main__" :
root = Node( 100 )
root.left = Node( 20 )
root.right = Node( 200 )
root.left.left = Node( 10 )
root.left.right = Node( 30 )
root.right.left = Node( 150 )
root.right.right = Node( 300 )
print ( "Preorder Traversal: " , end = "")
printPreOrder(root)
|
C#
using System;
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
{
public static void printPreorder(Node node)
{
if (node == null )
{
return ;
}
Console.Write(node.data.ToString() + " " );
GFG.printPreorder(node.left);
GFG.printPreorder(node.right);
}
public static void Main(String[] args)
{
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);
Console.Write( "Preorder Traversal: " );
GFG.printPreorder(root);
}
}
|
Javascript
class Node {
constructor(v) {
this .data = v;
this .left = this .right = null ;
}
}
function printPreOrder(node) {
if (node == null ) return ;
console.log(node.data + " " );
printPreOrder(node.left);
printPreOrder(node.right);
}
let 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);
console.log( "Preorder Traversal: " );
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
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++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int v)
{
this ->data = v;
this ->left = this ->right = NULL;
}
};
void printPostOrder(Node* node)
{
if (node == NULL)
return ;
printPostOrder(node->left);
printPostOrder(node->right);
cout << node->data << " " ;
}
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);
cout << "PostOrder Traversal: " ;
printPostOrder(root);
cout << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static class Node {
int data;
Node left;
Node right;
Node( int v)
{
this .data = v;
this .left = this .right = null ;
}
}
public static void printPreorder(Node node)
{
if (node == null )
return ;
printPreorder(node.left);
printPreorder(node.right);
System.out.print(node.data + " " );
}
public static void main(String[] args)
{
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 );
System.out.print( "Preorder Traversal: " );
printPreorder(root);
}
}
|
C#
using System;
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
{
public static void printPreorder(Node node)
{
if (node == null )
{
return ;
}
GFG.printPreorder(node.left);
GFG.printPreorder(node.right);
Console.Write(node.data.ToString() + " " );
}
public static void Main(String[] args)
{
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);
Console.Write( "Preorder Traversal: " );
GFG.printPreorder(root);
}
}
|
Python3
class Node:
def __init__( self , v):
self .data = v
self .left = None
self .right = None
def printPostOrder(node):
if node is None :
return
printPostOrder(node.left)
printPostOrder(node.right)
print (node.data, end = " " )
if __name__ = = "__main__" :
root = Node( 100 )
root.left = Node( 20 )
root.right = Node( 200 )
root.left.left = Node( 10 )
root.left.right = Node( 30 )
root.right.left = Node( 150 )
root.right.right = Node( 300 )
print ( "Postorder Traversal: " , end = "")
printPostOrder(root)
|
Javascript
class Node {
constructor(v) {
this .data = v;
this .left = null ;
this .right = null ;
}
}
function printPostOrder(node) {
if (node === null ) {
return ;
}
printPostOrder(node.left);
printPostOrder(node.right);
console.log(node.data, end = " " );
}
let 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);
console.log( "Postorder Traversal: " , end = "" );
printPostOrder(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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Feb, 2023
Like Article
Save Article