Binary Search Tree in Java

0

import java.util.Scanner;

/**
*
* @author vishnu
*/
class Node
{

Node left;
Node right;
int value;

public Node(int value) {
this.value = value;
}
}
class BinarySearchTree{

Node root;
public BinarySearchTree(){
root=null;

}
void insertNode(int value){
insert(root,value);
System.out.println(“inserting!!!!”+value+” “+root.value);
}
void insert(Node root, int value) {
if(this.root==null){

this.root=new Node(value);

return;
}
if (value < root.value) {
if (root.left != null) {
insert(root.left, value);
} else {
root.left = new Node(value);
System.out.println(“inserted!!!!”+value);

}
} else if (value > root.value) {
if (root.right != null) {
insert(root.right, value);
} else {
root.right = new Node(value);
System.out.println(“inserted!!!!”+value);
}
}
}

void inOrder(){
inOrder(root);
}

void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.println( node.value);
inOrder(node.right);
}
}

}

public class BST {

public static void main(String[] args)

{

BinarySearchTree tree = new BinarySearchTree();
Node rootnode = null;
Scanner S=new Scanner(System.in);
int c,n;
while(true){
System.out.println(“menu:\n1)Insert\n2)Display\n3)Exit”);
c=S.nextInt();
switch(c){
case 1:
System.out.println(“enter the value”);
n=S.nextInt();
tree.insertNode(n);
break;
case 2:

System.out.println(“Traversing tree in order”);
tree.inOrder();
break;
case 3:
System.exit(0);
break;

}

}

}

}