Sudo Placement[1.4] | BST Traversal

Given N elements to be inserted into Binary Search Tree. The task is to construct a binary search tree with only insert operation and finally print the elements in the postorder traversal. The BST is constructed according to the arrival order of elements.

Examples:

Input: N elements =  {8, 5, 10, 3, 4, 9, 7}
Output: 4 3 7 5 9 10 8

For the above input, the BST is: 
               8
             /    \
          5         10
        /   \      /
       3     7    9
       \
        4
The post-order traversal of the above BST is 4 3 7 5 9 10 8

Approach: The approach to solve this problem is to construct the BST using insertion method in BST. Once all the nodes are inserted, print the postorder traversal of the tree.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to insert nodes
// and print the postorder traversal
#include <bits/stdc++.h>
using namespace std;
  
// structure to store the BST
struct Node {
    int data;
    Node* left = NULL;
    Node* right = NULL;
};
  
// locates the memory space
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
  
// inserts node in the BST
Node* insertNode(Node* head, int key)
{
    // if first node
    if (head == NULL)
        head = newNode(key);
    else {
        // move to left
        if (key < head->data)
            head->left = insertNode(head->left, key);
        // move to right
        else
            head->right = insertNode(head->right, key);
    }
    return head;
}
  
// print the postorder traversal
void posOrder(Node* head)
{
    // leaf node is null
    if (head == NULL)
        return;
  
    // left
    posOrder(head->left);
  
    // right
    posOrder(head->right);
  
    // data
    cout << head->data << " ";
}
  
// Driver Code
int main()
{
  
    Node* root = NULL;
    root = insertNode(root, 8);
    root = insertNode(root, 5);
    root = insertNode(root, 10);
    root = insertNode(root, 3);
    root = insertNode(root, 4);
    root = insertNode(root, 9);
    root = insertNode(root, 7);
  
    // prints the postorder traversal of
    // the tree
    posOrder(root);
    cout << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to insert nodes
// and print the postorder traversal
class GFG {
  
    // structure to store the BST
    static class Node {
        int data;
        Node left = null;
        Node right = null;
    };
  
    // locates the memory space
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = null;
        temp.right = null;
        return temp;
    }
  
    // inserts node in the BST
    static Node insertNode(Node head, int key)
    {
        // if first node
        if (head == null)
            head = newNode(key);
        else {
            // move to left
            if (key < head.data)
                head.left = insertNode(head.left, key);
            // move to right
            else
                head.right = insertNode(head.right, key);
        }
        return head;
    }
  
    // print the postorder traversal
    static void posOrder(Node head)
    {
        // leaf node is null
        if (head == null)
            return;
  
        // left
        posOrder(head.left);
  
        // right
        posOrder(head.right);
  
        // data
        System.out.print(head.data + " ");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        Node root = new Node();
        root = insertNode(root, 8);
        root = insertNode(root, 5);
        root = insertNode(root, 10);
        root = insertNode(root, 3);
        root = insertNode(root, 4);
        root = insertNode(root, 9);
        root = insertNode(root, 7);
  
        // prints the postorder traversal of
        // the tree
        posOrder(root);
        System.out.println("");
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to insert nodes
// and print the postorder traversal
using System;
  
class GFG {
  
    // structure to store the BST
    public class Node {
        public int data;
        public Node left = null;
        public Node right = null;
    };
  
    // locates the memory space
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = null;
        temp.right = null;
        return temp;
    }
  
    // inserts node in the BST
    static Node insertNode(Node head, int key)
    {
        // if first node
        if (head == null)
            head = newNode(key);
        else {
            // move to left
            if (key < head.data)
                head.left = insertNode(head.left, key);
            // move to right
            else
                head.right = insertNode(head.right, key);
        }
        return head;
    }
  
    // print the postorder traversal
    static void posOrder(Node head)
    {
        // leaf node is null
        if (head == null)
            return;
  
        // left
        posOrder(head.left);
  
        // right
        posOrder(head.right);
  
        // data
        Console.Write(head.data + " ");
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        Node root = new Node();
        root = insertNode(root, 8);
        root = insertNode(root, 5);
        root = insertNode(root, 10);
        root = insertNode(root, 3);
        root = insertNode(root, 4);
        root = insertNode(root, 9);
        root = insertNode(root, 7);
  
        // prints the postorder traversal of
        // the tree
        posOrder(root);
        Console.WriteLine("");
    }
}
  
// This code contributed by Rajput-Ji

chevron_right



Output:

4 3 7 5 9 10 8


My Personal Notes arrow_drop_up

This article is contributed by Team Geeksforgeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.