This document contains Java code that defines a binary tree class with methods for inserting nodes, searching for values, counting nodes, checking if the tree is empty, and traversing the tree in preorder, inorder and postorder manners. A main method is included that allows a user to test the binary tree operations by selecting options from a menu to insert nodes, search, count nodes or check emptiness and displays the results of tree traversals.
5. post_order(r.get_go());
System.out.print(r.get_data() +" ");
}
}
}
public class Bina_Tree//binary tree class
{
public static void main(String[] args)//here main methode
{
Scanner sc = new Scanner(System.in);//here scanner
Bina_tree bin_c = new Bina_tree();
System.out.println("Binary Tree ");
char ch;
do
{
System.out.println(" here Binary Tree Operations ");
System.out.println("1 for insert 2 for search 3 for count 4 for check");//here choice
numbers
int cho = sc.nextInt();
int k;
switch (cho)
{
k++;
case 1 :System.out.println("please enter insert ele");
bin_c.tee_insert( sc.nextInt() );
break;
case 2 :System.out.println("enter bin_search");//here search option
System.out.println("result"+ bin_c.bin_serc( sc.nextInt() ));//result
break;
case 3 :System.out.println("Nodes"+ bin_c.cou_Node());
break;
6. case 4 :System.out.println("Empty"+ bin_c.isEmpty());
break;
default ://here default condition
System.out.println("enter 1 TO 4 only");//here enter 1 to 4
break;
}
System.out.print(" Post_order : ");
bin_c.post_order();
System.out.print(" Pre_order : ");
bin_c.pre_order();
System.out.print(" In_order : ");
bin_c.in_order();
System.out.println("do you want to continue type y or n ..* ");
ch = sc.next().charAt(0); //to convert charat
} while(ch=='y'||ch=='Y');//TO CHECK THE CONDITON
}
System.out.println("sum of nodes is %d",k);
}
Solution
ANS:
import java.util.Scanner;
class Bina_node
{
Bina_node l_1, r_1;
int dt;//here declare variables
public Bina_node()//constructor
{
l_1 = null;
r_1 = null;
dt = 0;
7. }
public Bina_node(int n)//here parameter constractor
{
l_1 = null;
r_1 = null;
dt = n;
}
public void nin_left(Bina_node n)//copy constractor
{
l_1 = n;
}
public void nin_right(Bina_node n)
{
r_1 = n;
}
public Bina_node get_lstf()
{
return l_1;
}
public Bina_node get_go()
{
return r_1;
}
public void set_data(int d)
{
dt = d;
}
public int get_data()
{
return dt;
}
}
class Bina_tree//class banary tree
{
8. private Bina_node rf;
public Bina_tree()
{
rf = null;
}
public boolean isEmpty()//here empty or not
{
return rf == null;
}
public void tee_insert(int dt)
{
rf = tee_insert(rf, dt);
}
private Bina_node tee_insert(Bina_node node, int dt)
{
if (node == null)
node = new Bina_node(dt);
else
{
if (node.get_go() == null)
node.r_1 = tee_insert(node.r_1, dt);
else
node.l_1 = tee_insert(node.l_1, dt);
}
return node;
}
public int cou_Node()
{
return cou_Node(rf);
}
private int cou_Node(Bina_node r)
{
if (r == null)
return 0;
else
9. {
int l = 1;
l += cou_Node(r.get_lstf());
l += cou_Node(r.get_go());
return l;
}
}
public boolean bin_serc(int tree_val)//function called
{
return bin_serc(rf, tree_val);
}
private boolean bin_serc(Bina_node r, int tree_val)
{
if (r.get_data() == tree_val)
return true;
if (r.get_lstf() != null)
if (bin_serc(r.get_lstf(), tree_val))
return true;
if (r.get_go() != null)
if (bin_serc(r.get_go(), tree_val))
return true;
return false;
}
public void in_order()
{
in_order(rf);
}
private void in_order(Bina_node r)
{
if (r != null)
{
in_order(r.get_lstf());
System.out.print(r.get_data() +" ");
in_order(r.get_go());
}
}
10. public void pre_order()
{
pre_order(rf);
}
private void pre_order(Bina_node r)
{
if (r != null)
{
System.out.print(r.get_data() +" ");
pre_order(r.get_lstf());
pre_order(r.get_go());
}
}
public void post_order()
{
post_order(rf);
}
private void post_order(Bina_node r)
{
if (r != null)
{
post_order(r.get_lstf());
post_order(r.get_go());
System.out.print(r.get_data() +" ");
}
}
}
public class Bina_Tree//binary tree class
{
public static void main(String[] args)//here main methode
{
Scanner sc = new Scanner(System.in);//here scanner
Bina_tree bin_c = new Bina_tree();
System.out.println("Binary Tree ");
char ch;
11. do
{
System.out.println(" here Binary Tree Operations ");
System.out.println("1 for insert 2 for search 3 for count 4 for check");//here choice
numbers
int cho = sc.nextInt();
int k;
switch (cho)
{
k++;
case 1 :System.out.println("please enter insert ele");
bin_c.tee_insert( sc.nextInt() );
break;
case 2 :System.out.println("enter bin_search");//here search option
System.out.println("result"+ bin_c.bin_serc( sc.nextInt() ));//result
break;
case 3 :System.out.println("Nodes"+ bin_c.cou_Node());
break;
case 4 :System.out.println("Empty"+ bin_c.isEmpty());
break;
default ://here default condition
System.out.println("enter 1 TO 4 only");//here enter 1 to 4
break;
}
System.out.print(" Post_order : ");
bin_c.post_order();
System.out.print(" Pre_order : ");
bin_c.pre_order();
System.out.print(" In_order : ");
bin_c.in_order();
System.out.println("do you want to continue type y or n ..* ");
12. ch = sc.next().charAt(0); //to convert charat
} while(ch=='y'||ch=='Y');//TO CHECK THE CONDITON
}
System.out.println("sum of nodes is %d",k);
}