BINARY SEARCH TREE OPERATIONS IN JAVA
import java.util.*;
class BST{
Node root;
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data=data;
this.left=null;
this.right=null;
public void insert(int data){
root=insert_rec(root,data);
public Node insert_rec(Node root,int data){
if(root==null){
Node nn=new Node(data);
return nn;
if(data<root.data){
root.left=insert_rec(root.left,data);
}
else{
root.right=insert_rec(root.right,data);
return root;
public void inorder() {
inorder_rec(root);
public void inorder_rec(Node root) {
if (root == null) {
return;
inorder_rec(root.left);
System.out.print(root.data+" ");
inorder_rec(root.right);
public void preorder() {
preorder_rec(root);
public void preorder_rec(Node root) {
if (root == null) {
return;
System.out.print(root.data+" ");
preorder_rec(root.left);
preorder_rec(root.right);
public void postorder() {
postorder_rec(root);
public void postorder_rec(Node root) {
if (root == null) {
return;
postorder_rec(root.left);
postorder_rec(root.right);
System.out.print(root.data+" ");
public void max(){
max_rec(root);
public void min(){
min_rec(root);
public void max_rec(Node root){
if(root==null){
return;
while(root.right!=null){
root=root.right;
}
System.out.println("Maximum number is "+root.data);
public void min_rec(Node root){
if(root==null){
return;
while(root.left!=null){
root=root.left;
System.out.println("Minimum number is "+root.data);
public int count() {
return count_rec(root);
public int count_rec(Node root) {
if (root == null) {
return 0;
int leftcount=count_rec(root.left);
int rightcount=count_rec(root.right);
return 1+leftcount+rightcount;
public int sum() {
return sum_rec(root);
}
public int sum_rec(Node root) {
if (root == null) {
return 0;
int left_tree=sum_rec(root.left);
int right_tree=sum_rec(root.right);
return root.data+left_tree+right_tree;
public void search(int t){
b_search(root,t);
public void b_search(Node root,int t){
if(root==null){
System.out.println("Element not found");
return;
if (root.data == t) {
System.out.println("Element found");
return;
else if(root.data<t)
b_search(root.right,t);
else
b_search(root.left,t);
}
public void r_tree(int t){
r_tree_rec(root,t);
public void r_tree_rec(Node root,int t){
if(root==null){
System.out.println("Element not found");
return;
if (root.data == t) {
inorder_rec(root.right);
else if(root.data<t)
r_tree_rec(root.right,t);
else
r_tree_rec(root.left,t);
public void l_tree(int t){
l_tree_rec(root,t);
public void l_tree_rec(Node root,int t){
if(root==null){
System.out.println("Element not found");
return;
}
if (root.data == t) {
inorder_rec(root.left);
else if(root.data<t)
l_tree_rec(root.right,t);
else
l_tree_rec(root.left,t);
public void diff(int t){
diff_rec(root,t);
public void diff_rec(Node root,int t){
if(root==null){
System.out.println("Element not found");
return;
if (root.data == t) {
int leftside=sum_rec(root.left);
int rightside=sum_rec(root.right);
int ans=Math.abs(leftside-rightside);
System.out.println(ans);
else if(root.data<t)
diff_rec(root.right,t);
else
diff_rec(root.left,t);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BST t=new BST();
t.insert(17);t.insert(28);t.insert(53);t.insert(49);t.insert(76);
t.insert(1);t.insert(14);t.insert(15);t.insert(23);t.insert(36);
boolean k=true;
while(k){
System.out.println("\nEnter your choice: ");
int choice=sc.nextInt();
switch(choice){
case 1:
System.out.println("Inorder Traversal");
t.inorder();
break;
case 2:
System.out.println("Preorder Traversal");
t.preorder();
break;
case 3:
System.out.println("Postorder Traversal");
t.postorder();
break;
case 4:
t.max();
t.min();
break;
case 5:
System.out.println("Counting the nodes: "+t.count());
break;
case 6:
System.out.println("Sum of all nodes: "+t.sum());
break;
case 7:
System.out.println("Search a node: ");
int target=sc.nextInt();
t.search(target);
break;
case 8:
System.out.println("Right sum tree: ");
t.r_tree(28);
break;
case 9:
System.out.println("Left sum tree: ");
t.l_tree(53);
break;
case 10:
System.out.println("Difference for right and left subtree of given node(28): ");
t.diff(28);
break;
case 11:
k = false;
System.out.println("Exiting");
break;