Another Binary Tree

import java.util.*;
  class solution
  {
    static Scanner input = new Scanner(System.in);
    static Node createTree(){
      Node root = null;
      System.out.println("Enter Data : ");
      int data = input.nextInt();
      if(data == -1)
        return root;
      root = new Node(data);
      System.out.println("Enter to left of "+data);
      root.left  = createTree();
      System.out.println("Enter to right of "+data);
      root.right = createTree();
      return root;
    }

    static void inOrder(Node root){
      if(root == null) return;
      inOrder(root.left);
      System.out.println(root.data+"=>");
      inOrder(root.right);
    }
    static void postOrder(Node root){
      if(root == null) return;
      postOrder(root.left);
      postOrder(root.right);
      System.out.println(root.data+"=>");
    }
    static void preOrder(Node root){
      if(root == null) return;
      System.out.println(root.data+"=>");
      preOrder(root.left);
      preOrder(root.right);
    }
    public static void main(String[] args) {
        Node root = createTree();
        System.out.print("Inorder : ");
        inOrder(root);
        System.out.println("PostOrder : ");
        postOrder(root);
        System.out.println("PreOrder : ");
        preOrder(root);
    }

    static class Node{
      int data;
      Node left;
      Node right;
      public Node(int data)
      {
        this.data = data;
      }
    }
  }

Output :

Enter Data :
  10
  Enter to left of 10
  5
  Enter to left of 5
  2
  Enter to left of 2
  -1
  Enter to right of 2
  -1
  Enter to right of 5
  7
  Enter to left of 7
  -1
  Enter to right of 7
  -1
  Enter to right of 10
  15
  Enter to left of 15
  13
  Enter to left of 13
  -1
  Enter to right of 13
  -1
  Enter to right of 15
  20
  Enter to left of 20
  -1
  Enter to right of 20
  -1
  Inorder : 2=>5=>7=>10=>13=>15=>20
  PostOrder :2=>7=>5=>13=>20=>15=>10
  PreOrder :10=>5=>2=>7=>15=>13=>20

Popular Posts

Family Tree Project in Java

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Creating basic tic tac toe android app using java