Insertion in binary search tree

Insertion in binary search tree

Here given code implementation process.

Iterative implementation

//C Program 
//Insert node in binary search tree
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Search Tree node
struct Node

{
  int data;
  struct Node *left,*right; 
};

//Adding a new node in binary search tree
void add( struct Node **root, int data)

{
  //Create a dynamic node of binary search tree 
  struct Node *new_node = (struct Node *)malloc(sizeof(struct Node ));

  if(new_node!=NULL)

  {
    //Set data and pointer values
    new_node->data = data;
    new_node->left = NULL; //Initially node left-pointer is NULL
    new_node->right = NULL;//Initially node right-pointer is NULL

    if(*root == NULL)

    {
      //When adds a first node in binary tree
      *root = new_node;
    }
    else

    {
      struct Node *find = *root;

      //add new node to proper position
      while(find != NULL)
      {
        if(find -> data >= data)
        { 
          if(find->left==NULL)
          {
            find->left = new_node;
            break;
          }
          else
          { 
            //visit left sub-tree
            find = find->left;
          }
        }
        else
        {
          if(find->right == NULL)
          {
            find->right = new_node;
            break;
          }
          else
          {
            //visit right sub-tree
            find = find->right;
          }
        }
      }
    }
  }
  else
  {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }
}
void preorder(struct Node*root)
{
  if(root!=NULL)
  {
    printf("%3d ",root->data );
    preorder(root->left);

    preorder(root->right);
  }
}
void inorder(struct Node*root)
{
  if(root!=NULL)
  {

    inorder(root->left);
    printf("%3d ",root->data );
    inorder(root->right);
  }
}
void postorder(struct Node*root)
{
  if(root!=NULL)
  {

    postorder(root->left);

    postorder(root->right);

    printf("%3d ",root->data );
  }
}
int main()
{

  struct Node*root = NULL;

  //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */

  add(&root,10);
  add(&root,4);
  add(&root,3);
  add(&root,5);
  add(&root,15);
  add(&root,12);


  printf(" Preorder \n");
  preorder(root);

  printf("\n Inorder \n");
  inorder(root);

  printf("\n Postorder \n");
  postorder(root);

  return 0;
}

Output

 Preorder 
 10   4   3   5  15  12 
 Inorder 
  3   4   5  10  12  15 
 Postorder 
  3   5   4  12  15  10
//C++ Program 
//Insert node in binary search tree
#include <iostream>
using namespace std;

//Structure of Binary Search Tree node
struct Node
{
  int data;
  Node *left,*right; 
};

class BST
{

public:
  Node*root;
  BST();
  //public methods
  void add(int);
  void inorder(Node*);
  void preorder(Node*);
  void postorder(Node*);
};
BST::BST()
{
  root=NULL;
}
//Adding a new node in binary search tree
void BST :: add(int data)
{
  //Create a dynamic node of binary search tree 
   Node *new_node = new Node();

  if(new_node!=NULL)
  {
    //Set data and pointer values
    new_node->data = data;
    new_node->left = NULL; //Initially node left-pointer is NULL
    new_node->right = NULL;//Initially node right-pointer is NULL

    if(root == NULL)
    {
      //When adds a first node in binary tree
      root = new_node;
    }
    else
    {
       Node *find = root;

      //add new node to proper position
      while(find != NULL)
      {
        if(find -> data >= data)
        { 
          if(find->left==NULL)
          {
            find->left = new_node;
            break;
          }
          else
          { 
            //visit left sub-tree
            find = find->left;
          }
        }
        else
        {
          if(find->right == NULL)
          {
            find->right = new_node;
            break;
          }
          else
          {
            //visit right sub-tree
            find = find->right;
          }
        }
      }
    }
  }
  else
  {
    cout<<("Memory Overflow\n");
  }
}
void BST :: preorder(Node*root)
{
  if(root!=NULL)
  {
    cout<<"  "<< root->data;
    preorder(root->left);

    preorder(root->right);
  }
}
void BST :: inorder(Node*root)
{
  if(root!=NULL)
  {

    inorder(root->left);
    cout<<"  "<< root->data;
    inorder(root->right);
  }
}
void BST :: postorder(Node*root)
{
  if(root!=NULL)
  {

    postorder(root->left);

    postorder(root->right);

    cout<<"  "<< root->data;
  }
}
int main()
{
  //Create object
  BST obj;


  //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */

  obj.add(10);
  obj.add(4);
  obj.add(3);
  obj.add(5);
  obj.add(15);
  obj.add(12);


  cout<<" Preorder \n";
  obj.preorder(obj.root);

  cout<<"\n Inorder \n";
  obj.inorder(obj.root);

  cout<<"\n Postorder \n";
  obj.postorder(obj.root);


  return 0;
}

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
//Java program
//Insert node in binary search tree
class Node
{
  public int data;
  public Node left,right;
}
public class BST
{


  public Node root;
  //Class constructors
  BST()
  {
    root=null;
  } 

  //insert  element
  public void add(int data)
  {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node();

    if(new_node != null)
    {
      //Set data and pointer values
      new_node.data = data;
      //Initially node left-pointer is null
      new_node.left = null; 
      //Initially node right-pointer is null
      new_node.right = null;

      if(root == null)
      {
        //When adds a first node in binary tree
        root = new_node;
      }
      else
      {
        Node find = root;

        //add new node to proper position
        while(find != null)
        {
          if(find.data >= data)
          { 
            if(find.left==null)
            {
              find.left = new_node;
              break;
            }
            else
            { 
              //visit left sub-tree
              find = find.left;
            }
          }
          else
          {
            if(find.right == null)
            {
              find.right = new_node;
              break;
            }
            else
            {
              //visit right sub-tree
              find = find.right;
            }
          }
        }
      }
    }
    else
    {
      System.out.println("Memory Overflow");
    }
  }
  public void preorder(Node root)
  {
    if(root!=null)
    {
      System.out.print("  "+ root.data);
      preorder(root.left);

      preorder(root.right);
    }
  }
  public void inorder(Node root)
  {
    if(root!=null)
    {

      inorder(root.left);
      System.out.print("  "+ root.data);
      inorder(root.right);
    }
  }
  public void postorder(Node root)
  {
    if(root!=null)
    {

      postorder(root.left);

      postorder(root.right);

      System.out.print("  "+ root.data);
    }
  }


  public static void main(String[] args) 
  {

    BST obj = new BST();

    //Add nodes in binary search tree
    /*
         10
       /   \
      4     15
     / \   /
    3   5 12
    
    */

    obj.add(10);
    obj.add(4);
    obj.add(3);
    obj.add(5);
    obj.add(15);
    obj.add(12);


    System.out.println("Preorder ");
    obj.preorder(obj.root);

    System.out.println("\nInorder ");
    obj.inorder(obj.root);

    System.out.println("\nPostorder ");
    obj.postorder(obj.root);

  }
}

Output

Preorder 
  10  4  3  5  15  12
Inorder 
  3  4  5  10  12  15
Postorder 
  3  5  4  12  15  10
BST node presentation
#Python Program 
#Insert node in binary search tree
class Node:
  def __init__(self,data):
    self.data=data
    self.left=None
    self.right=None

   
class BST:

  def __init__(self):

    #Assign default value
    self.root=None
  
  #insert  element
  def add(self,data):

    #Create a dynamic node of binary search tree 
    new_node = Node(data)

    if(self.root == None):
      #When adds a first node in binary tree
      self.root = new_node
    
    else:
      find = self.root

      #add new node to proper position
      while(find != None):

        if(find.data >= data): 

          if(find.left==None):

            find.left = new_node
            break
          
          else: 

            #visit left sub-tree
            find = find.left
          
        
        else:

          if(find.right == None):

            find.right = new_node
            break
          
          else:

            #visit right sub-tree
            find = find.right

  def preorder(self, root):

    if(root!=None):

      print(root.data,end="  ")
      self.preorder(root.left)

      self.preorder(root.right)
    
  
  def inorder(self, root):

    if(root!=None):

      self.inorder(root.left)
      print(root.data,end="  ")
      self.inorder(root.right)
    
  
  def postorder(self, root):

    if(root!=None):

      self.postorder(root.left)

      self.postorder(root.right)

      print(root.data,end="  ")
    
  


def main():
  obj = BST()


  #Add nodes in binary search tree
  #
  #       10
  #      /   \
  #     4     15
  #    / \   /
  #   3   5 12
  #         
  #

  obj.add(10)
  obj.add(4)
  obj.add(3)
  obj.add(5)
  obj.add(15)
  obj.add(12)


  print("Preorder ")
  obj.preorder(obj.root)

  print("\nInorder ")
  obj.inorder(obj.root)

  print("\nPostost")
  obj.postorder(obj.root)

if __name__ =="__main__":
  main()

Output

Preorder 
10  4  3  5  15  12  
Inorder 
3  4  5  10  12  15  
Postost
3  5  4  12  15  10 
//C# program
//Insert node in binary search tree

using System;
public class Node
{
  public int data;
  public Node left,right;
}


public class BST
{


  public Node root;
  //Class constructors
  BST()
  {
    root=null;
  } 

  //insert  element
  public void add(int data)
  {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node();

    if(new_node != null)
    {
      //Set data and pointer values
      new_node.data = data;
      //Initially node left-pointer is null
      new_node.left = null; 
      //Initially node right-pointer is null
      new_node.right = null;

      if(root == null)
      {
        //When adds a first node in binary tree
        root = new_node;
      }
      else
      {
        Node find = root;

        //add new node to proper position
        while(find != null)
        {
          if(find.data >= data)
          { 
            if(find.left==null)
            {
              find.left = new_node;
              break;
            }
            else
            { 
              //visit left sub-tree
              find = find.left;
            }
          }
          else
          {
            if(find.right == null)
            {
              find.right = new_node;
              break;
            }
            else
            {
              //visit right sub-tree
              find = find.right;
            }
          }
        }
      }
    }
    else
    {
      Console.WriteLine("Memory Overflow");
    }
  }
  public void preorder(Node root)
  {
    if(root!=null)
    {
      Console.Write("  "+ root.data);
      preorder(root.left);

      preorder(root.right);
    }
  }
  public void inorder(Node root)
  {
    if(root!=null)
    {

      inorder(root.left);
      Console.Write("  "+ root.data);
      inorder(root.right);
    }
  }
  public void postorder(Node root)
  {
    if(root!=null)
    {

      postorder(root.left);

      postorder(root.right);

      Console.Write("  "+ root.data);
    }
  }


  public static void Main(String[] args) 
  {

    BST obj = new BST();

    //Add nodes in binary search tree
    /*
           10
          /   \
         4     15
        / \   /
       3   5 12
              
    */

    obj.add(10);
    obj.add(4);
    obj.add(3);
    obj.add(5);
    obj.add(15);
    obj.add(12);


    Console.WriteLine("Preorder ");
    obj.preorder(obj.root);

    Console.WriteLine("\nInorder ");
    obj.inorder(obj.root);

    Console.WriteLine("\nPostorder ");
    obj.postorder(obj.root);

  }
}

Output

Preorder 
  10  4  3  5  15  12
Inorder 
  3  4  5  10  12  15
Postorder 
  3  5  4  12  15  10
<?php
//Php program 
//Insert node in binary search tree
class Node
{
  public $data;
  public $left;
  public $right;
  function __construct($data)
  {
    $this->data = $data;
    $this->length = NULL;
    $this->right = NULL;
  }
}
class BST
{

  public $root;
  function __construct()
  {
    $root=NULL;
  }
  //Adding a new node in binary search tree
  function add($data)
  {
    //Create a dynamic node of binary search tree 
    $new_node = new Node($data);

    if($this->root == NULL)
    {
      //When adds a first node in binary tree
      $this->root = $new_node;
    }
    else
    {
      $find = $this->root;

      //add new node to proper position
      while($find != NULL)
      {
        if($find->data >= $data)
        { 
          if($find->left==NULL)
          {
            $find->left = $new_node;
            break;
          }
          else
          { 
          //visit left sub-tree
            $find = $find->left;
          }
        }
        else
        {
          if($find->right == NULL)
          {
            $find->right = $new_node;
            break;
          }
          else
          {
          //visit right sub-tree
            $find = $find->right;
          }
        }
      }
    }

  }
  function preorder($root)
  {
    if($root!=NULL)
    {
      echo "  ". $root->data;
      $this->preorder($root->left);

      $this->preorder($root->right);
    }
  }
  function inorder($root)
  {
    if($root!=NULL)
    {

      $this->inorder($root->left);
      echo "  ". $root->data;
      $this->inorder($root->right);
    }
  }
  function postorder($root)
  {
    if($root!=NULL)
    {

      $this->postorder($root->left);

      $this->postorder($root->right);

      echo "  ".$root->data;
    }
  }

}
function main()
{
  //Make a object of BST class
  $obj= new BST();
    //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */

  $obj->add(10);
  $obj->add(4);
  $obj->add(3);
  $obj->add(5);
  $obj->add(15);
  $obj->add(12);


  echo " Preorder \n";
  $obj->preorder($obj->root);

  echo "\n Inorder \n";
  $obj->inorder($obj->root);

  echo "\n Postorder \n";
  $obj->postorder($obj->root);

}
main();
?>

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
# Ruby Program
# Insert node in binary search tree

class Node 
  attr_reader :data, :left, :right
  attr_accessor :data, :left, :right
  def initialize(value) 
    @data = value
    @left = nil
    @right = nil
  end
end

class BinarySearchTree 
  attr_reader :root
  attr_accessor :root
  def initialize() 
    @root = nil
  end
  def add(value) 
    new_node = Node.new(value)
    if (new_node != nil) 
      if (@root == nil) 
        @root = new_node
      else 
        find = @root
        while (find != nil) 
          if (find.data >= value) 
            if (find.left == nil) 
              find.left = new_node
              break
            else 
              find = find.left
            end
          else 
            if (find.right == nil) 
              find.right = new_node
              break
            else 
              find = find.right
            end
          end
        end
      end
    else 
      print("Memory Overflow")
    end
  end
  def preorder(head) 
    if (head != nil) 
      print("  ", head.data)
      self.preorder(head.left)
      self.preorder(head.right)
    end
  end
  def inorder(head) 
    if (head != nil) 
      self.inorder(head.left)
      print("  ", head.data)
      self.inorder(head.right)
    end
  end
  def postorder(head) 
    if (head != nil) 
      self.postorder(head.left)
      self.postorder(head.right)
      print("  ", head.data)
    end
  end
end
def main() 
  obj = BinarySearchTree.new()
  obj.add(10)
  obj.add(4)
  obj.add(3)
  obj.add(5)
  obj.add(15)
  obj.add(12)
  #
  #         10
  #       /   \
  #      4     15
  #     / \   /
  #    3   5 12
  #  
  print("Preorder ")
  obj.preorder(obj.root)
  print("\nInorder ")
  obj.inorder(obj.root)
  print("\nPostorder ")
  obj.postorder(obj.root)
end


main()

Output

Preorder
  10  4  3  5  15  12
Inorder
  3  4  5  10  12  15
Postorder
  3  5  4  12  15  10
/*
 Node Js Program
 Insert node in binary search tree
*/

class Node {

  constructor(value) {
    this.data = value;
    this.left = null;
    this.right = null;
  }
}
class BinarySearchTree {

  constructor() {
    this.root = null;
  }
  add(value) {
    var new_node = new Node(value);
    if (new_node != null) {
      if (this.root == null) {
        this.root = new_node;
      } else {
        var find = this.root;
        while (find != null) {
          if (find.data >= value) {
            if (find.left == null) {
              find.left = new_node;
              break;
            } else {
              find = find.left;
            }
          } else {
            if (find.right == null) {
              find.right = new_node;
              break;
            } else {
              find = find.right;
            }
          }
        }
      }
    } else {
      process.stdout.write("Memory Overflow");
    }
  }
  preorder(head) {
    if (head != null) {
      process.stdout.write("  " + head.data);
      this.preorder(head.left);
      this.preorder(head.right);
    }
  }
  inorder(head) {
    if (head != null) {
      this.inorder(head.left);
      process.stdout.write("  " + head.data);
      this.inorder(head.right);
    }
  }
  postorder(head) {
    if (head != null) {
      this.postorder(head.left);
      this.postorder(head.right);
      process.stdout.write("  " + head.data);
    }
  }
}

function main() {
  var obj = new BinarySearchTree();
  obj.add(10);
  obj.add(4);
  obj.add(3);
  obj.add(5);
  obj.add(15);
  obj.add(12);
  /*
           10
         /   \
        4     15
       / \   /
      3   5 12
    
   */
  console.log("Preorder");
  obj.preorder(obj.root);
  console.log("\nInorder");
  obj.inorder(obj.root);
  console.log("\nPostorder ");
  obj.postorder(obj.root);
}

main();

Output

Preorder
  10  4  3  5  15  12
Inorder
  3  4  5  10  12  15
Postorder
  3  5  4  12  15  10
/*
 Swift 4 Program
 Insert node in binary search tree
*/

class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class BinarySearchTree {
  var root: Node? ;
  init() {
    self.root = nil;
  }
  func add(_ value: Int) {
    let new_node: Node? = Node(value);
    if (new_node != nil) {
      if (self.root == nil) {
        self.root = new_node;
      } else {
        var find: Node? = self.root;
        while (find != nil) {
          if (find!.data >= value) {
            if (find!.left == nil) {
              find!.left = new_node;
              break;
            } else {
              find = find!.left;
            }
          } else {
            if (find!.right == nil) {
              find!.right = new_node;
              break;
            } else {
              find = find!.right;
            }
          }
        }
      }
    } else {
      print("Memory Overflow");
    }
  }
  func preorder(_ head: Node? ) {
    if (head != nil) {
      print("  ", head!.data, terminator:"");
      self.preorder(head!.left);
      self.preorder(head!.right);
    }
  }
  func inorder(_ head: Node? ) {
    if (head != nil) {
      self.inorder(head!.left);
      print("  ", head!.data, terminator:"");
      self.inorder(head!.right);
    }
  }
  func postorder(_ head: Node? ) {
    if (head != nil) {
      self.postorder(head!.left);
      self.postorder(head!.right);
      print("  ", head!.data, terminator:"");
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
  obj.add(10);
  obj.add(4);
  obj.add(3);
  obj.add(5);
  obj.add(15);
  obj.add(12);
  /*
         10
       /   \
      4     15
     / \   /
    3   5 12
  
  */
  print("Preorder ");
  obj.preorder(obj.root);
  print("\nInorder ");
  obj.inorder(obj.root);
  print("\nPostorder ");
  obj.postorder(obj.root);
}
main();

Output

Preorder
  10  4  3  5  15  12
Inorder
  3  4  5  10  12  15
Postorder
  3  5  4  12  15  10

Recursive implementation

//C Program 
//Insertion in binary search tree
//Using recursion
#include<stdio.h>
#include<stdlib.h>
//structure of Binary Search Tree node
struct Node
{
  int data;
  struct Node *left,*right; 
};
//Add element into binary search tree
struct Node * add( struct Node *root, int data)
{
  
  if(root!=NULL)
  {

    if(root -> data >= data)
    { 
      //When new element is smaller or equal to current node
      root->left = add(root->left,data);
    }
    else
    {
      //When new element is higher to current node
      root->right = add(root->right,data);
    }
    //important to manage node
    return root;
  }
  else
  {
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node ));
    if(new_node!=NULL)
    {
      //Set data and pointer values
      new_node->data = data;
      new_node->left = NULL; 
      new_node->right = NULL;
    }
    else
    {
      printf("Memory Overflow\n");
      exit(0);
    }
    return new_node;
  }

}

void preorder(struct Node*root)
{
  if(root!=NULL)
  {
    printf("%3d ",root->data );
    preorder(root->left);

    preorder(root->right);
  }
}
void inorder(struct Node*root)
{
  if(root!=NULL)
  {
    
    inorder(root->left);
    printf("%3d ",root->data );
    inorder(root->right);
  }
}
void postorder(struct Node*root)
{
  if(root!=NULL)
  {
    
    postorder(root->left);

    postorder(root->right);

    printf("%3d ",root->data );
  }
}
int main(){
    
  struct Node*root = NULL;

  //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */


  //address of first node are assigning to root
  root=add(root,10);

  add(root,4);
  add(root,3);
  add(root,5);
  add(root,15);
  add(root,12);


  printf(" Preorder \n");
  preorder(root);

  printf("\n Inorder \n");
  inorder(root);

  printf("\n Postorder \n");
  postorder(root);

  return 0;
}

Output

 Preorder 
 10   4   3   5  15  12 
 Inorder 
  3   4   5  10  12  15 
 Postorder 
  3   5   4  12  15  10
//C++ Program 
//Insert node in binary search tree
//using recursion
#include <iostream>
using namespace std;

//Structure of Binary Search Tree node
struct Node
{
  int data;
  Node *left,*right; 
};

class BST
{

public:
  Node*root;
  BST();
  //public methods
  Node* add(Node*,int);
  void inorder(Node*);
  void preorder(Node*);
  void postorder(Node*);
};
BST::BST()
{
  root=NULL;
}
//Adding a new node in binary search tree
Node* BST :: add(Node*root,int data)
{
  if(root!=NULL)
  {

    if(root -> data >= data)
    { 
      //When new element is smaller or equal to current node
      root->left = add(root->left,data);
    }
    else
    {
      //When new element is higher to current node
      root->right = add(root->right,data);
    }
    //important to manage node
    return root;
  }
  else
  {
    Node *new_node = new Node();
    if(new_node!=NULL)
    {
      //Set data and pointer values
      new_node->data = data;
      new_node->left = NULL; 
      new_node->right = NULL;
    }
    else
    {
      cout<<("Memory Overflow\n");
    }
    return new_node;
  }
}
void BST :: preorder(Node*root)
{
  if(root!=NULL)
  {
    cout<<"  "<< root->data;
    preorder(root->left);

    preorder(root->right);
  }
}
void BST :: inorder(Node*root)
{
  if(root!=NULL)
  {

    inorder(root->left);
    cout<<"  "<< root->data;
    inorder(root->right);
  }
}
void BST :: postorder(Node*root)
{
  if(root!=NULL)
  {

    postorder(root->left);

    postorder(root->right);

    cout<<"  "<< root->data;
  }
}
int main()
{
  //Create object
  BST obj;


  //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */

  obj.root=obj.add(obj.root,10);
  obj.add(obj.root,4);
  obj.add(obj.root,3);
  obj.add(obj.root,5);
  obj.add(obj.root,15);
  obj.add(obj.root,12);


  cout<<" Preorder \n";
  obj.preorder(obj.root);

  cout<<"\n Inorder \n";
  obj.inorder(obj.root);

  cout<<"\n Postorder \n";
  obj.postorder(obj.root);


  return 0;
}

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
//Java program
//Insert node in binary search tree
//using recursion
public class BST
{

  static class Node
  {
    int data;
    Node left,right;
  }
  public Node root;
  //Class constructors
  BST()
  {
    root=null;
  } 

  //insert  element
  public Node add(Node root,int data)
  {
    if(root!=null)
    {

      if(root.data >= data)
      { 
        //When new element is smaller or equal to current node
        root.left = add(root.left,data);
      }
      else
      {
        //When new element is higher to current node
        root.right = add(root.right,data);
      }
      //important to manage root node
      return root;
    }
    else
    {
      Node new_node = new Node();
      if(new_node!=null)
      {
        //Set data and pointer values
        new_node.data = data;
        new_node.left = null; 
        new_node.right = null;
      }
      else
      {
        System.out.println("Memory Overflow\n");
      }
      return new_node;
    }
  }
  public void preorder(Node root)
  {
    if(root != null)
    {
      System.out.print("  "+ root.data);
      preorder(root.left);

      preorder(root.right);
    }
  }
  public void inorder(Node root)
  {
    if(root!=null)
    {

      inorder(root.left);
      System.out.print("  "+ root.data);
      inorder(root.right);
    }
  }
  public void postorder(Node root)
  {
    if(root!=null)
    {

      postorder(root.left);

      postorder(root.right);

      System.out.print("  "+ root.data);
    }
  }


  public static void main(String[] args) 
  {

    BST obj = new BST();

    //Add nodes in binary search tree
    /*
           10
          /   \
         4     15
        / \   /
       3   5 12
              
    */

    obj.root=obj.add(obj.root,10);
    obj.add(obj.root,4);
    obj.add(obj.root,3);
    obj.add(obj.root,5);
    obj.add(obj.root,15);
    obj.add(obj.root,12);


    System.out.println(" Preorder ");
    obj.preorder(obj.root);

    System.out.println("\n Inorder ");
    obj.inorder(obj.root);

    System.out.println("\n Postorder ");
    obj.postorder(obj.root);

  }
}

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
#Python Program 
#Insert node in binary search tree
#Using recursion
class Node:
  def __init__(self,data):
    self.data=data
    self.left=None
    self.right=None

   
class BST:

  def __init__(self):

    #Assign default value
    self.root=None
  
  #insert  element
  def add(self,root,data):

    if(root!=None):

      if(root.data >= data): 

        #When new element is smaller or equal to current node
        root.left = self.add(root.left,data)
      
      else :

        #When new element is higher to current node
        root.right =  self.add(root.right,data)
      
      #important to manage root node
      return root
    
    else:    
    
      return Node(data)
    

  def preorder(self, root):

    if(root!=None):

      print(root.data,end="  ")
      self.preorder(root.left)

      self.preorder(root.right)
    
  
  def inorder(self, root):

    if(root!=None):

      self.inorder(root.left)
      print(root.data,end="  ")
      self.inorder(root.right)
    
  
  def postorder(self, root):

    if(root!=None):

      self.postorder(root.left)

      self.postorder(root.right)

      print(root.data,end="  ")
    
  


def main():
  obj = BST()


  #Add nodes in binary search tree
  #
  #       10
  #      /   \
  #     4     15
  #    / \   /
  #   3   5 12
  #         
  #
  obj.root=obj.add(obj.root,10);
  obj.add(obj.root,4);
  obj.add(obj.root,3);
  obj.add(obj.root,5);
  obj.add(obj.root,15);
  obj.add(obj.root,12);

  print("Preorder ")
  obj.preorder(obj.root)

  print("\nInorder ")
  obj.inorder(obj.root)

  print("\nPostost")
  obj.postorder(obj.root)

if __name__ =="__main__":
  main()

Output

Preorder 
10  4  3  5  15  12  
Inorder 
3  4  5  10  12  15  
Postost
3  5  4  12  15  10 
//C# program
//Insert node in binary search tree
//using recursion
using System;
public class Node
{
  public int data;
  public Node left,right;
}
public class BST
{


  public Node root;
  //Class constructors
  BST()
  {
    root=null;
  } 

  //insert  element
  public Node add(Node root,int data)
  {
    if(root!=null)
    {

      if(root.data >= data)
      { 
        //When new element is smaller or equal to current node
        root.left = add(root.left,data);
      }
      else
      {
        //When new element is higher to current node
        root.right = add(root.right,data);
      }
      //important to manage root node
      return root;
    }
    else
    {
      Node new_node = new Node();
      if(new_node!=null)
      {
        //Set data and pointer values
        new_node.data = data;
        new_node.left = null; 
        new_node.right = null;
      }
      else
      {
        Console.WriteLine("Memory Overflow\n");
      }
      return new_node;
    }
  }
  public void preorder(Node root)
  {
    if(root != null)
    {
      Console.Write("  "+ root.data);
      preorder(root.left);

      preorder(root.right);
    }
  }
  public void inorder(Node root)
  {
    if(root!=null)
    {

      inorder(root.left);
      Console.Write("  "+ root.data);
      inorder(root.right);
    }
  }
  public void postorder(Node root)
  {
    if(root!=null)
    {

      postorder(root.left);

      postorder(root.right);

      Console.Write("  "+ root.data);
    }
  }


  public static void Main(String[] args) 
  {

    BST obj = new BST();

    //Add nodes in binary search tree
    /*
           10
          /   \
         4     15
        / \   /
       3   5 12
              
    */

    obj.root=obj.add(obj.root,10);
    obj.add(obj.root,4);
    obj.add(obj.root,3);
    obj.add(obj.root,5);
    obj.add(obj.root,15);
    obj.add(obj.root,12);


    Console.WriteLine(" Preorder ");
    obj.preorder(obj.root);

    Console.WriteLine("\n Inorder ");
    obj.inorder(obj.root);

    Console.WriteLine("\n Postorder ");
    obj.postorder(obj.root);

  }
}

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
<?php
//Php program 
//Insert node in binary search tree
//Using Recursion
class Node
{
  public $data;
  public $left;
  public $right;
  function __construct($data)
  {
    $this->data = $data;
    $this->left = NULL;
    $this->right = NULL;
  }
}
class BST
{

  public $root;

  function __construct()
  {
    $root=NULL;
  }

  //Adding a new node in binary search tree
  function add($root,$data)
  {
    if($root!=NULL)
    {

      if($root->data >= $data)
      { 
        //When new element is smaller or equal to current node
        $root->left = $this->add($root->left,$data);
      }
      else
      {
        //When new element is higher to current node
        $root->right = $this->add($root->right,$data);
      }
      //important to manage node
      return $root;
    }
    else
    {

      return new Node($data);
    }

  }
  function preorder($root)
  {
    if($root!=NULL)
    {
      echo "  ". $root->data;
      $this->preorder($root->left);

      $this->preorder($root->right);
    }
  }
  function inorder($root)
  {
    if($root!=NULL)
    {

      $this->inorder($root->left);
      echo "  ". $root->data;
      $this->inorder($root->right);
    }
  }
  function postorder($root)
  {
    if($root!=NULL)
    {

      $this->postorder($root->left);

      $this->postorder($root->right);

      echo "  ".$root->data;
    }
  }

}
function main()
{
  //Make a object of BST class
  $obj= new BST();
  //Add nodes in binary search tree
  /*
         10
        /   \
       4     15
      / \   /
     3   5 12
            
  */

  $obj->root=$obj->add($obj->root,10);
            $obj->add($obj->root,4);
            $obj->add($obj->root,3);
            $obj->add($obj->root,5);
            $obj->add($obj->root,15);
            $obj->add($obj->root,12);

  echo " Preorder \n";
  $obj->preorder($obj->root);

  echo "\n Inorder \n";
  $obj->inorder($obj->root);

  echo "\n Postorder \n";
  $obj->postorder($obj->root);

}
   main();
?>

Output

 Preorder 
  10  4  3  5  15  12
 Inorder 
  3  4  5  10  12  15
 Postorder 
  3  5  4  12  15  10
# Ruby Program
# Insert node in binary search tree
# Using recursion

class Node 
  attr_reader :data, :left, :right
  attr_accessor :data, :left, :right
  def initialize(value) 
    @data = value
    @left = nil
    @right = nil
  end
end

class BinarySearchTree 
  attr_reader :root
  attr_accessor :root
  def initialize() 
    @root = nil
  end
  def add(head, value) 
    if (head != nil) 
      if (head.data >= value) 
        head.left = self.add(head.left, value)
      else 
        head.right = self.add(head.right, value)
      end
      return head
    else 
      new_node = Node.new(value)
      if (new_node == nil) 
        print("Memory Overflow\n")
      end
      return new_node
    end
  end
  def preorder(head) 
    if (head != nil) 
      print("  ", head.data)
      self.preorder(head.left)
      self.preorder(head.right)
    end
  end
  def inorder(head) 
    if (head != nil) 
      self.inorder(head.left)
      print("  ", head.data)
      self.inorder(head.right)
    end
  end
  def postorder(head) 
    if (head != nil) 
      self.postorder(head.left)
      self.postorder(head.right)
      print("  ", head.data)
    end
  end
end
def main() 
  obj = BinarySearchTree.new()
  obj.root = obj.add(obj.root, 10)
  obj.add(obj.root, 4)
  obj.add(obj.root, 3)
  obj.add(obj.root, 5)
  obj.add(obj.root, 15)
  obj.add(obj.root, 12)
  #
  #         10
  #       /   \
  #      4     15
  #     / \   /
  #    3   5 12
  #  
  print("Preorder\n")
  obj.preorder(obj.root)
  print("\nInorder \n")
  obj.inorder(obj.root)
  print("\nPostorder \n")
  obj.postorder(obj.root)
end


main()

Output

Preorder
  10  4  3  5  15  12
Inorder 
  3  4  5  10  12  15
Postorder 
  3  5  4  12  15  10
/*
 Node Js Program
 Insert node in binary search tree
 Using recursion
*/

class Node {
  constructor(value) {
    this.data = value;
    this.left = null;
    this.right = null;
  }
}
class BinarySearchTree {
  
  constructor() {
    this.root = null;
  }
  add(head, value) {
    if (head != null) {
      if (head.data >= value) {
        head.left = this.add(head.left, value);
      } else {
        head.right = this.add(head.right, value);
      }
      return head;
    } else {
      var new_node = new Node(value);
      if (new_node == null) {
        process.stdout.write("Memory Overflow\n");
      }
      return new_node;
    }
  }
  preorder(head) {
    if (head != null) {
      process.stdout.write("  " + head.data);
      this.preorder(head.left);
      this.preorder(head.right);
    }
  }
  inorder(head) {
    if (head != null) {
      this.inorder(head.left);
      process.stdout.write("  " + head.data);
      this.inorder(head.right);
    }
  }
  postorder(head) {
    if (head != null) {
      this.postorder(head.left);
      this.postorder(head.right);
      process.stdout.write("  " + head.data);
    }
  }
}

function main() {
  var obj = new BinarySearchTree();
  obj.root = obj.add(obj.root, 10);
  obj.add(obj.root, 4);
  obj.add(obj.root, 3);
  obj.add(obj.root, 5);
  obj.add(obj.root, 15);
  obj.add(obj.root, 12);
  /*
           10
         /   \
        4     15
       / \   /
      3   5 12
    
   */
  process.stdout.write("Preorder\n");
  obj.preorder(obj.root);
  process.stdout.write("\nInorder \n");
  obj.inorder(obj.root);
  process.stdout.write("\nPostorder \n");
  obj.postorder(obj.root);
}

main();

Output

Preorder
  10  4  3  5  15  12
Inorder 
  3  4  5  10  12  15
Postorder 
  3  5  4  12  15  10
/*
 Swift 4 Program
 Insert node in binary search tree
 Using recursion
*/

class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class BinarySearchTree {
  var root: Node? ;
  init() {
    self.root = nil;
  }
  func add(_ head: Node? , _ value : Int) ->Node? {
    if (head != nil) {
      if (head!.data >= value) {
        head!.left = self.add(head!.left, value);
      } else {
        head!.right = self.add(head!.right, value);
      }
      return head;
    } else {
      let new_node: Node? = Node(value);
      if (new_node == nil) {
        print("Memory Overflow");
      }
      return new_node;
    }
  }
  func preorder(_ head: Node? ) {
    if (head != nil) {
      print("  ", head!.data,terminator:"");
      self.preorder(head!.left);
      self.preorder(head!.right);
    }
  }
  func inorder(_ head: Node? ) {
    if (head != nil) {
      self.inorder(head!.left);
      print("  ", head!.data,terminator:"");
      self.inorder(head!.right);
    }
  }
  func postorder(_ head: Node? ) {
    if (head != nil) {
      self.postorder(head!.left);
      self.postorder(head!.right);
      print("  ", head!.data,terminator:"");
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
  obj.root = obj.add(obj.root, 10);
  let _ = obj.add(obj.root, 4);
  let _ = obj.add(obj.root, 3);
  let _ = obj.add(obj.root, 5);
  let _ = obj.add(obj.root, 15);
  let _ = obj.add(obj.root, 12);
  /*
           10
         /   \
        4     15
       / \   /
      3   5 12
    
  */
  print("Preorder");
  obj.preorder(obj.root);
  print("\nInorder ");
  obj.inorder(obj.root);
  print("\nPostorder ");
  obj.postorder(obj.root);
}
main();

Output

Preorder
  10  4  3  5  15  12
Inorder 
  3  4  5  10  12  15
Postorder 
  3  5  4  12  15  10


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved