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>usingnamespacestd;// Class describing a node of treeclassNode{public:intdata;Node*left;Node*right;Node(intv){this->data=v;this->left=this->right=NULL;}};// Inorder TraversalvoidprintInorder(Node*node){if(node==NULL)return;// Traverse left subtreeprintInorder(node->left);// Visit nodecout<<node->data<<" ";// Traverse right subtreeprintInorder(node->right);}// Driver codeintmain(){// Build the treeNode*root=newNode(100);root->left=newNode(20);root->right=newNode(200);root->left->left=newNode(10);root->left->right=newNode(30);root->right->left=newNode(150);root->right->right=newNode(300);// Function callcout<<"Inorder Traversal: ";printInorder(root);return0;}
Java
// Java code to implement the approachimportjava.io.*;// Class describing a node of treeclassNode{intdata;Nodeleft;Noderight;Node(intv){this.data=v;this.left=this.right=null;}}classGFG{// Inorder TraversalpublicstaticvoidprintInorder(Nodenode){if(node==null)return;// Traverse left subtreeprintInorder(node.left);// Visit nodeSystem.out.print(node.data+" ");// Traverse right subtreeprintInorder(node.right);}// Driver Codepublicstaticvoidmain(String[]args){// Build the treeNoderoot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callSystem.out.print("Inorder Traversal: ");printInorder(root);}}// This code is contributed by Rohit Pradhan
Python
# Python3 code to implement the approach# Class describing a node of treeclassNode:def__init__(self,v):self.left=Noneself.right=Noneself.data=v# Inorder TraversaldefprintInorder(root):ifroot:# Traverse left subtreeprintInorder(root.left)# Visit nodeprint(root.data,end=" ")# Traverse right subtreeprintInorder(root.right)# Driver codeif__name__=="__main__":# Build the treeroot=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)# Function callprint("Inorder Traversal:",end=" ")printInorder(root)# This code is contributed by ajaymakvana.
C#
// Include namespace systemusingSystem;// Class describing a node of treepublicclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intv){this.data=v;this.left=this.right=null;}}publicclassGFG{// Inorder TraversalpublicstaticvoidprintInorder(Nodenode){if(node==null){return;}// Traverse left subtreeGFG.printInorder(node.left);// Visit nodeConsole.Write(node.data.ToString()+" ");// Traverse right subtreeGFG.printInorder(node.right);}// Driver CodepublicstaticvoidMain(String[]args){// Build the treevarroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callConsole.Write("Inorder Traversal: ");GFG.printInorder(root);}}
JavaScript
// JavaScript code to implement the approachclassNode{constructor(v){this.left=null;this.right=null;this.data=v;}}// Inorder TraversalfunctionprintInorder(root){if(root){// Traverse left subtreeprintInorder(root.left);// Visit nodeconsole.log(root.data);// Traverse right subtreeprintInorder(root.right);}}// Driver codeif(true){// Build the treeletroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callconsole.log("Inorder Traversal:");printInorder(root);}// This code is contributed by akashish__
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
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>usingnamespacestd;// Class describing a node of treeclassNode{public:intdata;Node*left;Node*right;Node(intv){this->data=v;this->left=this->right=NULL;}};// Preorder TraversalvoidprintPreOrder(Node*node){if(node==NULL)return;// Visit Nodecout<<node->data<<" ";// Traverse left subtreeprintPreOrder(node->left);// Traverse right subtreeprintPreOrder(node->right);}// Driver codeintmain(){// Build the treeNode*root=newNode(100);root->left=newNode(20);root->right=newNode(200);root->left->left=newNode(10);root->left->right=newNode(30);root->right->left=newNode(150);root->right->right=newNode(300);// Function callcout<<"Preorder Traversal: ";printPreOrder(root);return0;}
Java
// Java code to implement the approachimportjava.io.*;// Class describing a node of treeclassNode{intdata;Nodeleft;Noderight;Node(intv){this.data=v;this.left=this.right=null;}}classGFG{// Preorder TraversalpublicstaticvoidprintPreorder(Nodenode){if(node==null)return;// Visit nodeSystem.out.print(node.data+" ");// Traverse left subtreeprintPreorder(node.left);// Traverse right subtreeprintPreorder(node.right);}publicstaticvoidmain(String[]args){// Build the treeNoderoot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callSystem.out.print("Preorder Traversal: ");printPreorder(root);}}// This code is contributed by lokeshmvs21.
Python
classNode:def__init__(self,v):self.data=vself.left=Noneself.right=None# Preorder TraversaldefprintPreOrder(node):ifnodeisNone:return# Visit Nodeprint(node.data,end=" ")# Traverse left subtreeprintPreOrder(node.left)# Traverse right subtreeprintPreOrder(node.right)# Driver codeif__name__=="__main__":# Build the treeroot=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)# Function callprint("Preorder Traversal: ",end="")printPreOrder(root)
C#
// Include namespace systemusingSystem;// Class describing a node of treepublicclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intv){this.data=v;this.left=this.right=null;}}publicclassGFG{// Preorder TraversalpublicstaticvoidprintPreorder(Nodenode){if(node==null){return;}// Visit nodeConsole.Write(node.data.ToString()+" ");// Traverse left subtreeGFG.printPreorder(node.left);// Traverse right subtreeGFG.printPreorder(node.right);}publicstaticvoidMain(String[]args){// Build the treevarroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callConsole.Write("Preorder Traversal: ");GFG.printPreorder(root);}}
JavaScript
classNode{constructor(v){this.data=v;this.left=this.right=null;}}functionprintPreOrder(node){if(node==null)return;console.log(node.data+" ");printPreOrder(node.left);printPreOrder(node.right);}// Build the treeletroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);console.log("Preorder Traversal: ");printPreOrder(root);// This code is contributed by akashish__
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
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>usingnamespacestd;// Class to define structure of a nodeclassNode{public:intdata;Node*left;Node*right;Node(intv){this->data=v;this->left=this->right=NULL;}};// PostOrder TraversalvoidprintPostOrder(Node*node){if(node==NULL)return;// Traverse left subtreeprintPostOrder(node->left);// Traverse right subtreeprintPostOrder(node->right);// Visit nodecout<<node->data<<" ";}// Driver codeintmain(){Node*root=newNode(100);root->left=newNode(20);root->right=newNode(200);root->left->left=newNode(10);root->left->right=newNode(30);root->right->left=newNode(150);root->right->right=newNode(300);// Function callcout<<"PostOrder Traversal: ";printPostOrder(root);cout<<"\n";return0;}
Java
// Java code to implement the approachimportjava.io.*;// Class describing a node of treeclassGFG{staticclassNode{intdata;Nodeleft;Noderight;Node(intv){this.data=v;this.left=this.right=null;}}// Preorder TraversalpublicstaticvoidprintPostOrder(Nodenode){if(node==null)return;// Traverse left subtreeprintPostOrder(node.left);// Traverse right subtreeprintPostOrder(node.right);// Visit nodeSystem.out.print(node.data+" ");}publicstaticvoidmain(String[]args){// Build the treeNoderoot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callSystem.out.print("PostOrder Traversal: ");printPostOrder(root);}}
Python
classNode:def__init__(self,v):self.data=vself.left=Noneself.right=None# Preorder TraversaldefprintPostOrder(node):ifnodeisNone:return# Traverse left subtreeprintPostOrder(node.left)# Traverse right subtreeprintPostOrder(node.right)# Visit Nodeprint(node.data,end=" ")# Driver codeif__name__=="__main__":# Build the treeroot=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)# Function callprint("Postorder Traversal: ",end="")printPostOrder(root)
C#
// Include namespace systemusingSystem;// Class describing a node of treepublicclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intv){this.data=v;this.left=this.right=null;}}publicclassGFG{// Preorder TraversalpublicstaticvoidprintPostOrder(Nodenode){if(node==null){return;}// Traverse left subtreeGFG.printPostOrder(node.left);// Traverse right subtreeGFG.printPostOrder(node.right);// Visit nodeConsole.Write(node.data.ToString()+" ");}publicstaticvoidMain(String[]args){// Build the treevarroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callConsole.Write("PostOrder Traversal: ");GFG.printPostOrder(root);}}
JavaScript
classNode{constructor(v){this.data=v;this.left=null;this.right=null;}}// Preorder TraversalfunctionprintPostOrder(node){if(node===null){return;}// Traverse left subtreeprintPostOrder(node.left);// Traverse right subtreeprintPostOrder(node.right);// Visit Nodeconsole.log(node.data,end=" ");}// Driver code// Build the treeletroot=newNode(100);root.left=newNode(20);root.right=newNode(200);root.left.left=newNode(10);root.left.right=newNode(30);root.right.left=newNode(150);root.right.right=newNode(300);// Function callconsole.log("Postorder Traversal: ",end="");printPostOrder(root);// This code is contributed by akashish__
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