Showing posts with label TREE. Show all posts
Showing posts with label TREE. Show all posts

JAVA program to perform iterative preorder traversal

Posted On // Leave a Comment

CODE:

import java.util.Scanner;
import java.util.Stack;

public class iterPre{

    static Scanner in = new Scanner(System.in);

        static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }

    static void iterPreorder(node root){
        if(root == null)
            return;
        node tmp = new node();
        Stack<node> s = new Stack<node>();
        s.push(root);
        while (s.isEmpty()==false) {
            tmp = s.pop();
            System.out.print("\t"+tmp.data);
            if(tmp.right != null)
                s.push(tmp.right);
            if(tmp.left != null)
                s.push(tmp.left);
        }
    }

    public static void main(String[] args) {
        node root = new node();
        root = inputTree();
        iterPreorder(root);
    }

}
[Read more]

JAVA program to traverse inorder without recursion

Posted On // Leave a Comment

CODE:


import java.util.Scanner;
import java.util.Stack;

public class inorderStack{

    static Scanner in = new Scanner(System.in);

    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }

    static void inorder(node tree){
        if(tree == null)
            return;
        Stack<node> s = new Stack<node>();
        while((s.isEmpty() == false)||(tree != null)){
            if(tree!=null){
                s.push(tree);
                tree = tree.left;
            }
            else{
                tree = s.pop();
                System.out.print(tree.data+"\t");
                tree = tree.right;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("Enter the tree: ");
        node tree = new node();
        tree = inputTree();
        System.out.println("Inorder traversal ");
        inorder(tree);   
    }

}
[Read more]

JAVA program to find sibling of parent of a given node

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class parentSibling{

    static Scanner in = new Scanner(System.in);

    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }

    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void printSibling(node tree,node par,node parofpar,int X){
        if(tree == null)
            return;
        if(tree.data==X){
            if((par != null)&&(parofpar != null)&&(parofpar.left != null)&&(parofpar.right != null)){
                if(parofpar.left == par)
                    System.out.println("Sibling "+parofpar.right.data);
                if(parofpar.right == par)
                    System.out.println("Sibling "+parofpar.left.data);
            }
            else
                System.out.println("No Sibling");
            return;
        }
        printSibling(tree.left,tree,par,X);
        printSibling(tree.right,tree,par,X);
    }

    public static void main(String[] args) {
        System.out.println("Enter the tree: ");
        node tree = new node();
        tree = inputTree();
        System.out.println("Inorder traversal ");
        inorder(tree);
        System.out.print("\nEnter node: ");
        printSibling(tree,null,null,in.nextInt());
    }

}

[Read more]

JAVA program to build a tree from inorder and postorder traversals

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class InPosttoTree{

    static Scanner in = new Scanner(System.in);
    static int postIndex;

    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void preorder(node tree){
        if(tree==null)
            return;
        System.out.print(tree.data+"\t");
        preorder(tree.left);
        preorder(tree.right);
    }

    static void postorder(node tree){
        if(tree==null)
            return;
        postorder(tree.left);
        postorder(tree.right);
        System.out.print(tree.data+"\t");
    }

    static node cnvrt(int[] inorder,int instart,int inend,int[] postorder){
            if((instart>inend))
                return null;
            node temp = new node();
            temp.data = postorder[postIndex--];
            if(instart==inend){
                temp.left=temp.right=null;
                return temp;
            }
            int i;
            for (i=0;(i<inorder.length)&&(inorder[i]!=temp.data);i++);
            temp.right = cnvrt(inorder,i+1,inend,postorder);
            temp.left = cnvrt(inorder,instart,i-1,postorder);
            return temp;
    }
   
    public static void main(String[] args) {
        int len;
        System.out.print("\nEnter length: ");
        len = in.nextInt();
        int[] iar = new int[len];
        int[] par = new int[len];
        int i;
        System.out.println("Enter inorder traversal");
        for(i=0;i<len;i++)
            iar[i] = in.nextInt();
        System.out.println("Enter postorder traversal");
        for(i=0;i<len;i++)
            par[i] = in.nextInt();
        node tree = new node();
        postIndex=len-1;
        tree = cnvrt(iar,0,len-1,par);
        System.out.println("Inorder traversal of the tree is:");
        inorder(tree);
        System.out.println("\nPreorder traversal of the tree is:");
        preorder(tree);
        System.out.println("\nPostorder traversal of the tree is");
        postorder(tree);
    }
}
[Read more]

JAVA program to build a tree from inorder and preorder traversals

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class InPreToTree{

    static Scanner in = new Scanner(System.in);
    static int preIndex;

    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void preorder(node tree){
        if(tree==null)
            return;
        System.out.print(tree.data+"\t");
        preorder(tree.left);
        preorder(tree.right);
    }

    static void postorder(node tree){
        if(tree==null)
            return;
        postorder(tree.left);
        postorder(tree.right);
        System.out.print(tree.data+"\t");
    }

    static node cnvrt(int[] inorder,int instart,int inend,int[] preorder){
            if((instart>inend))
                return null;
            node temp = new node();
            temp.data = preorder[preIndex++];
            if(instart==inend){
                temp.left=temp.right=null;
                return temp;
            }
            int i;
            for (i=0;(i<inorder.length)&&(inorder[i]!=temp.data);i++);
            temp.left = cnvrt(inorder,instart,i-1,preorder);
            temp.right = cnvrt(inorder,i+1,inend,preorder);
            return temp;
    }
   
    public static void main(String[] args) {
        int len;
        System.out.print("\nEnter length: ");
        len = in.nextInt();
        int[] iar = new int[len];
        int[] par = new int[len];
        int i;
        System.out.println("Enter inorder traversal");
        for(i=0;i<len;i++)
            iar[i] = in.nextInt();
        System.out.println("Enter preorder traversal");
        for(i=0;i<len;i++)
            par[i] = in.nextInt();
        node tree = new node();
        preIndex=0;
        tree = cnvrt(iar,0,len-1,par);
        System.out.println("Inorder traversal of the tree is:");
        inorder(tree);
        System.out.println("\nPreorder traversal of the tree is:");
        preorder(tree);
        System.out.println("\nPostorder traversal of the tree is");
        postorder(tree);
    }  
}
[Read more]

JAVA program to find height of a node in a binary tree

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class nodeHeight{
    static int height,nod;
    static void traverse(node tree,int currHeight){
        if(tree==null)
            return;
        if(tree.data==nod){
            height=currHeight+1;
            return;
        }
        if(height == -1){
            traverse(tree.left,currHeight+1);
            traverse(tree.right,currHeight+1);
        }
    }

    static Scanner in = new Scanner(System.in);

    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }

    public static void main(String[] args) {
        node tree = new node();
        System.out.println("Enter tree:");
        tree = inputTree();
        height = -1;
        System.out.print("Enter node:");
        nod=in.nextInt();
        traverse(tree,0);
        if(height==-1)
            System.out.println("Node not found");
        else
            System.out.print("Node is at height "+height);
    }
}

OUTPUT:

Enter tree:
Enter the value: 5
Enter y if the node 5 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: y
Enter the value: 1
Enter y if the node 1 has left subtree: n
Enter y if the node 1 has right subtree: n
Enter y if the node 2 has right subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: n
Enter y if the node 6 has right subtree: n
Enter node:1
Node is at height 3

NOT SURE WITH THE CODE PLS INFRM IF THERE ARE ANY TESTCASES IN WHICH THE CODE FAILS
[Read more]

JAVA program to convert an array to binary tree

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class arrayToBinaryTree{

    static Scanner in = new Scanner(System.in);

    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void preorder(node tree){
        if(tree==null)
            return;
        System.out.print(tree.data+"\t");
        preorder(tree.left);
        preorder(tree.right);
    }

    static void postorder(node tree){
        if(tree==null)
            return;
        postorder(tree.left);
        postorder(tree.right);
        System.out.print(tree.data+"\t");
    }

    static node cnvrt(int[] ar,int pos){
        if((pos>ar.length-1)||(ar[pos]==-1))
            return null;
        node tmp = new node();
        tmp.data = ar[pos];
        tmp.left = cnvrt(ar,2*pos+1);
        tmp.right = cnvrt(ar,2*pos+2);
        return tmp;
    }

    public static void main(String[] args) {
        int len;
        System.out.print("\nEnter length: ");
        len = in.nextInt();
        int[] ar = new int[len];
        int i;
        for(i=0;i<len;i++)
            ar[i] = in.nextInt();
        node tree = new node();
        tree = cnvrt(ar,0);
        System.out.println("Inorder traversal of the tree is:");
        inorder(tree);
        System.out.println("\nPreorder traversal of the tree is:");
        preorder(tree);
        System.out.println("\nPostorder traversal of the tree is");
        postorder(tree);
    }
}

OUTPUT:

Enter length: 7
2 1 4 -1 -1 3 5
Inorder traversal of the tree is:
1    2    3    4    5   
Preorder traversal of the tree is:
2    1    4    3    5   
Postorder traversal of the tree is
1    3    5    4    2   
[Read more]

JAVA program to convert a sorted array to BST

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class sortedArraytoBST{

    static Scanner in = new Scanner(System.in);

    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void preorder(node tree){
        if(tree==null)
            return;
        System.out.print(tree.data+"\t");
        preorder(tree.left);
        preorder(tree.right);
    }

    static void postorder(node tree){
        if(tree==null)
            return;
        postorder(tree.left);
        postorder(tree.right);
        System.out.print(tree.data+"\t");
    }

    static node convrt(int[] ar,int start,int end){
        if(start>end)
            return null;
        int mid = (start+end)/2;
        node tmp = new node();
        tmp.data = ar[mid];
        tmp.left = convrt(ar,start,mid-1);
        tmp.right = convrt(ar,mid+1,end);
        return tmp;
    }

    public static void main(String[] args) {
        int len;
        System.out.print("\nEnter length: ");
        len = in.nextInt();
        int[] ar = new int[len];
        int i;
        for(i=0;i<len;i++)
            ar[i] = in.nextInt();
        node tree = new node();
        tree = convrt(ar,0,len-1);
        System.out.println("Inorder traversal of the tree is:");
        inorder(tree);
        System.out.println("\nPreorder traversal of the tree is:");
        preorder(tree);
        System.out.println("\nPostorder traversal of the tree is");
        postorder(tree);
    }
}

OUTPUT:

Enter length: 5
1 2 3 4 5
Inorder traversal of the tree is:
1    2    3    4    5   
Preorder traversal of the tree is:
3    1    2    4    5   
Postorder traversal of the tree is
2    1    5    4    3   
[Read more]

JAVA program to find the lowest common ancestor of a binary tree

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class lowestcommonAnc{
    static Scanner in =new Scanner(System.in);
   
    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }
 
    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static node lca(node tree,int n1,int n2){
        if(tree == null)
            return tree;
        if((tree.data>n1)&&(tree.data>n2))
            return lca(tree.left,n1,n2);
        if((tree.data<n1)&&(tree.data<n2))
            return lca(tree.right,n1,n2);
        return tree;
    }

    public static void main(String[] args) {
        node tree=new node();
        System.out.print("Enter y if tree is null: ");
        if(in.next().charAt(0)=='y')
            tree=null;
        else
            tree=inputTree();
        if(tree != null){
            System.out.print("Inorder traversal of the tree is:\n");
            inorder(tree);
            System.out.print("\nEnter the node 1:");
            int n1 = in.nextInt();
            System.out.print("Enter the node 2:");
            int n2 = in.nextInt();
            System.out.print("Lowest commom ancestor is:"+lca(tree,n1,n2).data);
        }
    }
}

OUTPUT:

Enter y if tree is null: n
Enter the value: 8
Enter y if the node 8 has left subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: y
Enter the value: 1
Enter y if the node 1 has left subtree: n
Enter y if the node 1 has right subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 6 has right subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: n
Enter y if the node 7 has right subtree: n
Enter y if the node 8 has right subtree: y
Enter the value: 10
Enter y if the node 10 has left subtree: n
Enter y if the node 10 has right subtree: y
Enter the value: 14
Enter y if the node 14 has left subtree: y
Enter the value: 13
Enter y if the node 13 has left subtree: n
Enter y if the node 13 has right subtree: n
Enter y if the node 14 has right subtree: n
Inorder traversal of the tree is:
1    3    4    6    7    8    10    13    14  
Enter the node 1:1
Enter the node 2:7
Lowest commom ancestor is:3

[Read more]

JAVA program to do preorder,inorder,postorder traversals in a binary tree

Posted On // Leave a Comment

ALGORITHM:

        We have 3 types of tree traversals
  1. Preorder traversal
  2. Inorder traversal
  3. Postorder traversal
  • In preorder traversal a node is visited then its left and right subtree is visite.
  • In inorder traversal a node is visited after visiting its left subtree then it passes to the right subtree
  • In postorder traversal a node is visited after its left and right subtree is visited or either of it or both are null.
 Algorithm in detail is in this link.

CODE:

//utility class for node
public class node{
    public int data;
    public node left,right;
    public node(){
        data=0;
        left=right=null;
    }
}
//class to implement the algorithm
import java.util.Scanner;

public class treeTraversals {

    static Scanner in = new Scanner(System.in);

    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }
    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static void preorder(node tree){
        if(tree==null)
            return;
        System.out.print(tree.data+"\t");
        preorder(tree.left);
        preorder(tree.right);
    }

    static void postorder(node tree){
        if(tree==null)
            return;
        postorder(tree.left);
        postorder(tree.right);
        System.out.print(tree.data+"\t");
    }

    public static void main(String[] args) {
        System.out.println("Enter the tree: ");
        node tree = new node();
        tree = inputTree();
        System.out.println("Inorder traversal ");
        inorder(tree);
        System.out.println("\nPreorder traversal ");
        preorder(tree);
        System.out.println("\nPostorder traversal ");
        postorder(tree);
    }
}


OUTPUT:


Enter the tree:
Enter the value: 2
Enter y if the node 2 has left subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: n
Enter y if the node 6 has right subtree: y
Enter the value: 11
Enter y if the node 11 has left subtree: n
Enter y if the node 11 has right subtree: n
Enter y if the node 2 has right subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 9
Enter y if the node 9 has left subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 9 has right subtree: n
Inorder traversal
2    7    5    6    11    2    5    4    9  
Preorder traversal
2    7    2    6    5    11    5    9    4  
Postorder traversal
2    5    11    6    7    4    9    5    2   
[Read more]

JAVA program to print ancestors of a node in a BST

Posted On // Leave a Comment

ALGORITHM:

 To print ancestors of a node in a BST , we just add a print function to the searching function of a BST before the recursion.

Steps in a search function of BST:
  1. if current node is the required node print the node
  2. if current node is greater than required node goto step 1 for left subtree
  3. if current node is lesser than required node goto step 1 for right subtree

CODE:

//utility class for node
public class node{
    public int data;
    public node left,right;
    public node(){
        data=0;
        left=right=null;
    }
}
//class to implement the algorithm
import java.util.Scanner;

public class ancestorsNode {

    static Scanner in =new Scanner(System.in);
   
    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }


    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    public static void printAnc(node tree,int nod){


        if(nod==tree.data){
            System.out.println(nod);
            return;
        }
        System.out.print(tree.data+"---");
        if(nod<tree.data)
            printAnc(tree.left,nod);
       
        else
            printAnc(tree.right,nod);
    }

    public static void main(String[] args) {
        node tree=new node();
        System.out.print("Enter y if tree is null: ");
        if(in.next().charAt(0)=='y')
            tree=null;
        else
            tree=inputTree(tree);
        System.out.print("Inorder traversal of the tree is:\n");
        inorder(tree);
        System.out.print("\nEnter the node:");
        int nd = in.nextInt();
        printAnc(tree,nd);
        in.close();
    }
}

OUTPUT:

Enter y if tree is null: n
Enter the value: 5
Enter y if the node 5 has left subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: n
Enter y if the node 6 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 8
Enter y if the node 8 has left subtree: n
Enter y if the node 8 has right subtree: n
Inorder traversal of the tree is:
2    3    4    5    6    7    8    
Enter the node:8
5---7---8
[Read more]

JAVA program to check whether a binary tree is an BST or not

Posted On // Leave a Comment

ALGORITHM:

A binary search tree (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right subtree.

So to check whether a binary tree is a binary search tree or not we just use a recursive function which passes the left and right subtrees and lower limit and upper limit of the value which the current node can have .
  1. start with the root node with limits positive infinity and negative infinity
  2. return true if reached the leaf
  3. if the current node is legal then recurse the function by passing the left subtree and right subtree with limits passed lower limit argument and current node-1 as lower limit and upper limit for the left subtree and current node+1 and passed upper limit argument as lower limit and upper limit for right subtree.
  4. return false if current node is illegal

CODE:

//utility class for node
public class node{
    public int data;
    public node left,right;
    public node(){
        data=0;
        left=right=null;
    }
}
//class to implement the algorithm
import java.util.Scanner;

class isBST{
    static Scanner in = new Scanner(System.in);
    static boolean isBSTree(node tree,int min,int max){
        if((tree==null)||((tree.left==null)&&(tree.right==null)))
            return true;
        if((tree.right==null)&&(tree.left.data<=max))
            return isBSTree(tree.left,min,tree.left.data-1);
        if((tree.left==null)&&(tree.right.data>=min))
            return isBSTree(tree.right,tree.right.data+1,max);
        if((tree.left.data<=max)&&(tree.right.data>=min))
            return isBSTree(tree.left,min,tree.left.data-1)&&isBSTree(tree.right,tree.right.data+1,max);
        return false;
    }

    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }


    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    public static void main(String[] args) {
        node tree=new node();
        System.out.print("Enter y if tree is null: ");
        if(in.next().charAt(0)=='y')
            tree=null;
        else
            tree=inputTree();
        System.out.print("Inorder traversal of the tree is:\n");
        inorder(tree);
        if (isBSTree(tree,Integer.MIN_VALUE,Integer.MAX_VALUE))
            System.out.println("\nThe tree is BST....");
        else
            System.out.println("\nThe tree is not BST....");
    }
}
[Read more]