manthanthakker40

Binary Tree Insertion + Inorder,Preorder,Postorder Traversal

Jan 4th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package binarytree;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author manth
  13.  */
  14. class Node
  15. {
  16.     Node()
  17.     {
  18.        
  19.     }
  20.     Node(int name,Node left,Node right)
  21.     {
  22.         this.name=name;
  23.         this.left=left;
  24.         this.right=right;
  25.     }
  26.     int name;
  27.     Node left;
  28.     Node right;
  29. }
  30. public class BinaryTree {
  31.  
  32.     /**
  33.      * @param args the command line arguments
  34.      */
  35.     static Node root;
  36.     public static void main(String[] args) {
  37.        input_tree();
  38.        System.out.println("Inroder Traversal ");
  39.        inorder(root);
  40.        System.out.println("preroder Traversal ");
  41.        preorder(root);
  42.        System.out.println("postroder Traversal ");
  43.        postorder(root);
  44.        
  45.        
  46.        
  47.     }
  48.     public static void input_tree()
  49.     {
  50.         Scanner sc=new Scanner(System.in);
  51.         System.out.println("Enter number of nodes ");
  52.         int n=sc.nextInt();
  53.         System.out.println(" Enter nodes names ");
  54.         for(int i=0;i<n;i++)
  55.         {
  56.             int new_node_name=sc.nextInt();
  57.             Node new_node=new Node(new_node_name,null,null);
  58.             root=add(root,new_node);                      
  59.            
  60.         }
  61.        
  62.     }
  63.     public static void inorder(Node node)
  64.     {
  65.         if(node!=null)
  66.         {
  67.          
  68.         inorder(node.left);
  69.         System.out.println(node.name+" ");
  70.         inorder(node.right);
  71.         }
  72.     }
  73.     public static void postorder(Node node)
  74.     {
  75.         if(node!=null)
  76.         {
  77.          
  78.         inorder(node.left);
  79.         inorder(node.right);
  80.         System.out.print(node.name+" ");
  81.         }
  82.     }
  83.     public static void preorder(Node node)
  84.     {
  85.         if(node!=null)
  86.         {
  87.          System.out.print(node.name+" ");
  88.         inorder(node.left);
  89.         inorder(node.right);
  90.         }
  91.     }
  92.     public static Node add(Node node,Node new_node)
  93.     {
  94.        
  95.         if(root==null)
  96.         {
  97.             root=new_node;
  98.             node=root;
  99.             //System.out.println("root "+ root.name);
  100.         }
  101.         else if(node==null)
  102.         {
  103.             node=new Node(new_node.name,null,null);
  104.             //System.out.println("new "+ node.name);
  105.         }
  106.         else if(node.name<new_node.name)
  107.         {
  108.             node.right=add(node.right,new_node);
  109.            // System.out.println("noderight pass "+node.name);
  110.         }
  111.         else
  112.             node.left=add(node.left,new_node);
  113.         return node;
  114.     }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment