Dear Experts RESEND my Java coding as shows I need to print

Dear Experts,

RESEND

my Java coding as shows. I need to print \"Level Order BST\" accurately whenever input. Not sure whether coding reading correctly. The out put for level order should print level by level Thanks.

package btnode;
import java.util.*;


import java.util.Scanner;

public class BTNode {

    BTNode left, right;
     int data;

     /* Constructor */
     public BTNode()
     {
         left = null;
         right = null;
         data = 0;
     }
     /* Constructor */
     public BTNode(int n)
     {
         left = null;
         right = null;
         data = n;
     }
     /* Function to set left node */
     public void setLeft(BTNode n)
     {
         left = n;
     }
     /* Function to set right node */
     public void setRight(BTNode n)
     {
         right = n;
     }
     /* Function to get left node */
     public BTNode getLeft()
     {
         return left;
     }
     /* Function to get right node */
     public BTNode getRight()
     {
         return right;
     }
     /* Function to set data to node */
     public void setData(int d)
     {
         data = d;
     }
     /* Function to get data from node */
     public int getData()
     {
         return data;
     }    
}

/* Class BT */
class BT
{

    private static BTNode r;
     private BTNode root;

     /* Constructor */
     public BT()
     {
         root = null;
     }
     /* Function to check if tree is empty */
     public boolean isEmpty()
     {
         return root == null;
     }
     /* Functions to insert data */
     public void insert(int data)
     {
         root = insert(root, data);
     }
     /* Function to insert data recursively */
     private BTNode insert(BTNode node, int data)
     {
         if (node == null)
             node = new BTNode(data);
         else
         {
             if (node.getRight() == null)
                 node.right = insert(node.right, data);
             else
                 node.left = insert(node.left, data);            
         }
         return node;
     }    
     /* Function to count number of nodes */
     public int countNodes()
     {
         return countNodes(root);
     }
     /* Function to count number of nodes recursively */
     private int countNodes(BTNode r)
     {
         if (r == null)
             return 0;
         else
         {
             int l = 1;
             l += countNodes(r.getLeft());
             l += countNodes(r.getRight());
             return l;
         }
     }
     /* Function to search for an element */
     public boolean search(int val)
     {
         return search(root, val);
     }
     /* Function to search for an element recursively */
     private boolean search(BTNode r, int val)
     {
         if (r.getData() == val)
             return true;
         if (r.getLeft() != null)
             if (search(r.getLeft(), val))
                 return true;
         if (r.getRight() != null)
             if (search(r.getRight(), val))
                 return true;
         return false;        
     }
     /* Function for inorder traversal */
     public void inorder()
     {
         inorder(root);
     }
     private void inorder(BTNode r)
     {
         if (r != null)
         {
             inorder(r.getLeft());
             System.out.print(r.getData() +\" \");
             inorder(r.getRight());
         }
     }
     /* Function for preorder traversal */
     public void preorder()
     {
         preorder(root);
     }
     private void preorder(BTNode r)
     {
         if (r != null)
         {
             System.out.print(r.getData() +\" \");
             preorder(r.getLeft());            
             preorder(r.getRight());
         }
     }
     /* Function for postorder traversal */
     public void postorder()
     {
         postorder(root);
     }
     private void postorder(BTNode r)
     {
         if (r != null)
         {
             postorder(r.getLeft());            
             postorder(r.getRight());
             System.out.print(r.getData() +\" \");
         }}
    
   void levelorder(BTNode r, int i)
     {
        int h = height(root);
       
        for (i=1; i<=h; i++)
          GivenLevel(root, i);

       
     }
     int height(BTNode root)
    {
        if (root == null)
           return 0;
        else
        {
            /* compute height of each subtree */
            int lheight = height(root.left);
          
            int rheight = height(root.right);
        
            
            /* use the larger one */
            if (lheight > rheight)
                return(lheight+1);
          
            else return(rheight+1);
           
        }
   
    }

    
     void GivenLevel (BTNode root ,int level)

     {
         if (root == null)
             return;
         if (level == 1)
         System.out.print(root.data + \" \");
         else if (level > 1)
        {
           GivenLevel(root.left, level-1);
           GivenLevel(root.right, level-1);

        }

            
           
         }

   
     public static void main(String[] args)
    {           
        Scanner scan = new Scanner(System.in);
        /* Creating object of BT */
        BT bt = new BT();
     
         int i = 1;
        /* Perform tree operations */
        System.out.println(\"Binary Tree Test\ \");         
        char ch;       
        do   
        {
            System.out.println(\"\ Binary Tree Operations\ \");
            System.out.println(\"1. insert \");
            System.out.println(\"2. search\");
            System.out.println(\"3. count nodes\");
            System.out.println(\"4. check empty\");

            int choice = scan.nextInt();           
            switch (choice)
            {
            case 1 :
                System.out.println(\"Enter integer element to insert\");
                bt.insert( scan.nextInt() );                    
                break;                         
            case 2 :
                System.out.println(\"Enter integer element to search\");
                System.out.println(\"Search result : \"+ bt.search( scan.nextInt() ));
                break;                                         
            case 3 :
                System.out.println(\"Nodes = \"+ bt.countNodes());
                break;    
            case 4 :
                System.out.println(\"Empty status = \"+ bt.isEmpty());
                break;           
            default :
                System.out.println(\"Wrong Entry \ \");
                break;  
            }
            /* Display tree */
            System.out.print(\"\ Post order : \");
            bt.postorder();
            System.out.print(\"\ Pre order : \");
            bt.preorder();
            System.out.print(\"\ In order : \");
            bt.inorder();
            System.out.print( \"\ level order :\" );
           
        
           bt.levelorder(r, i);
            
            System.out.println(\"\ \ Do you want to continue (Type y or n) \ \");
          
           
            ch = scan.next().charAt(0);                       
        } while (ch == \'Y\'|| ch == \'y\');              
    }
}

Solution

Please follow the code and comments for description :

CODE :

import java.util.Scanner;

public class BTNode {

BTNode left, right;
int data;

/* Constructor */
public BTNode() {
left = null;
right = null;
data = 0;
}
/* Constructor */

public BTNode(int n) {
left = null;
right = null;
data = n;
}
/* Function to set left node */

public void setLeft(BTNode n) {
left = n;
}
/* Function to set right node */

public void setRight(BTNode n) {
right = n;
}
/* Function to get left node */

public BTNode getLeft() {
return left;
}
/* Function to get right node */

public BTNode getRight() {
return right;
}
/* Function to set data to node */

public void setData(int d) {
data = d;
}
/* Function to get data from node */

public int getData() {
return data;
}
}

/* Class BT */
class BT {

private static BTNode r;
private BTNode root;

/* Constructor */
public BT() {
root = null;
}
/* Function to check if tree is empty */

public boolean isEmpty() {
return root == null;
}
/* Functions to insert data */

public void insert(int data) {
root = insert(root, data);
}
/* Function to insert data recursively */

private BTNode insert(BTNode node, int data) {
if (node == null) {
node = new BTNode(data);
} else {
if (node.getRight() == null) {
node.right = insert(node.right, data);
} else {
node.left = insert(node.left, data);
}
}
return node;
}
/* Function to count number of nodes */

public int countNodes() {
return countNodes(root);
}
/* Function to count number of nodes recursively */

private int countNodes(BTNode r) {
if (r == null) {
return 0;
} else {
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to search for an element */

public boolean search(int val) {
return search(root, val);
}
/* Function to search for an element recursively */

private boolean search(BTNode r, int val) {
if (r.getData() == val) {
return true;
}
if (r.getLeft() != null) {
if (search(r.getLeft(), val)) {
return true;
}
}
if (r.getRight() != null) {
if (search(r.getRight(), val)) {
return true;
}
}
return false;
}
/* Function for inorder traversal */

public void inorder() {
inorder(root);
}

private void inorder(BTNode r) {
if (r != null) {
inorder(r.getLeft());
System.out.print(r.getData() + \" \");
inorder(r.getRight());
}
}
/* Function for preorder traversal */

public void preorder() {
preorder(root);
}

private void preorder(BTNode r) {
if (r != null) {
System.out.print(r.getData() + \" \");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */

public void postorder() {
postorder(root);
}

private void postorder(BTNode r) {
if (r != null) {
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() + \" \");
}
}

void levelorder(BTNode r, int i) {
int h = height(root);

for (i = 1; i <= h; i++) {
GivenLevel(root, i);
System.out.println(\" \");
}

}

int height(BTNode root) {
if (root == null) {
return 0;
} else {
/* compute height of each subtree */
int lheight = height(root.left);

int rheight = height(root.right);

/* use the larger one */
if (lheight > rheight) {
return (lheight + 1);
} else {
return (rheight + 1);
}

}

}

void GivenLevel(BTNode root, int level) {
if (root == null) {
return;
}
if (level == 1) {
System.out.print(root.data + \" \");
} else if (level > 1) {
GivenLevel(root.left, level - 1);
GivenLevel(root.right, level - 1);
}
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Creating object of BT */
BT bt = new BT();

int i = 1;
/* Perform tree operations */
System.out.println(\"Binary Tree Test\ \");
char ch;
do {
System.out.println(\"\ Binary Tree Operations\ \");
System.out.println(\"1. Insert \");
System.out.println(\"2. Search\");
System.out.println(\"3. Count Nodes\");
System.out.println(\"4. Check Empty\");

int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println(\"Enter the Integer Element to Insert : \");
bt.insert(scan.nextInt());
break;
case 2:
System.out.println(\"Enter the Integer Element to Search : \");
System.out.println(\"Search result : \" + bt.search(scan.nextInt()));
break;
case 3:
System.out.println(\"Nodes = \" + bt.countNodes());
break;
case 4:
System.out.println(\"Empty status = \" + bt.isEmpty());
break;
default:
System.out.println(\"Wrong Entry..!!\ \");
break;
}
/* Display tree */
System.out.print(\"\ Post order : \");
bt.postorder();
System.out.print(\"\ Pre order : \");
bt.preorder();
System.out.print(\"\ In order : \");
bt.inorder();
System.out.print(\"\ level order : \");

bt.levelorder(r, i);

System.out.println(\"\ \ Do you want to continue (Type y or n) \ \");

ch = scan.next().charAt(0);
} while (ch == \'Y\' || ch == \'y\');
}
}


OUTPUT :

Binary Tree Test


Binary Tree Operations

1. Insert
2. Search
3. Count Nodes
4. Check Empty
1
Enter the Integer Element to Insert :
1

Post order : 1
Pre order : 1
In order : 1
level order : 1


Do you want to continue (Type y or n)

y

Binary Tree Operations

1. Insert
2. Search
3. Count Nodes
4. Check Empty
1
Enter the Integer Element to Insert :
2

Post order : 2 1
Pre order : 1 2
In order : 1 2
level order : 1
2


Do you want to continue (Type y or n)

y

Binary Tree Operations

1. Insert
2. Search
3. Count Nodes
4. Check Empty
1
Enter the Integer Element to Insert :
3

Post order : 3 2 1
Pre order : 1 3 2
In order : 3 1 2
level order : 1
3 2


Do you want to continue (Type y or n)

y

Binary Tree Operations

1. Insert
2. Search
3. Count Nodes
4. Check Empty
1
Enter the Integer Element to Insert :
4

Post order : 4 3 2 1
Pre order : 1 3 4 2
In order : 3 4 1 2
level order : 1
3 2
4


Do you want to continue (Type y or n)

y

Binary Tree Operations

1. Insert
2. Search
3. Count Nodes
4. Check Empty
1
Enter the Integer Element to Insert :
5

Post order : 5 4 3 2 1
Pre order : 1 3 5 4 2
In order : 5 3 4 1 2
level order : 1
3 2
5 4


Do you want to continue (Type y or n)

n


Hope this is helpful.

Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \
Dear Experts, RESEND my Java coding as shows. I need to print \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site