Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package Lab11; public class BinaryTreeNode { BinaryTreeNode left, right; int dat

ID: 3734434 • Letter: P

Question

package Lab11;

public class BinaryTreeNode {

    BinaryTreeNode left, right;

    int data;

   

    public BinaryTreeNode(){

    left=null;

    right=null;

    data=0;

    }

   

    public BinaryTreeNode(int value){

    left=null;

    right=null;

    data=value;

    }

   

     public void setLeft(BinaryTreeNode node){

    left=node;

      }

     

     public void setRight(BinaryTreeNode node){

    right=node;

      }

       

     public void setData(int value){

    data=value;

      }

        

     public BinaryTreeNode getLeft(){

    return left;

      }

    

     public BinaryTreeNode getRight(){

    return right;

      }

  

    public int getData(){

    return data;

      }

}

package Lab11;

public class BinaryTree {

    private BinaryTreeNode root;

   

    public BinaryTree(){

    root=null;

    }

   

    public boolean isEmpty(){

    return root==null;

    }

   

    public BinaryTreeNode insert(BinaryTreeNode node, int data)

    {

    if(node==null){

    node=new BinaryTreeNode(data);

    }

    else

    {

    if(node.getLeft()==null)

        node.left=insert(node.left,data);

    else

        node.right=insert(node.right,data);

    }

    return node;

    }

    

    public void insert(int data){

        root=insert(root, data);

    }

   

      

    private int countNodes(BinaryTreeNode r){

    if(r==null)

        return 0;

    else

    {

    int number=1;

    number+=countNodes(r.getLeft());

    number+=countNodes(r.getRight());

     return number;

    }

     }

   

    public int countNodes(){

    return countNodes(root);

    }

   

    public boolean search(BinaryTreeNode r, int data){

    if(r.getData()==data)

        return true;

    if(r.getLeft()!=null)

        if(search(r.getLeft(),data))

            return true;

    if(r.getRight()!=null)

        if(search(r.getRight(),data))

            return true;

    return false;

    }

   

    public boolean search(int val){

    return search(root, val);

    }

   

}

package Lab11;

import java.util.Scanner;

public class BinaryTreeTest {

        public static void main(String[] args){

            BinaryTree binaryTree=new BinaryTree();

    /*Scanner scan =new Scanner(System.in);

   

    System.out.println("Enter 10 integers to insert");

*/

          int[] a={50, 76,21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87,80};

       for(int i=0;i<16;i++){

    binaryTree.insert(a[i]);

    }

   

      /* Display tree */

            System.out.print(" Postorder : ");

            binaryTree.postorder();

            System.out.print(" Preorder : ");

            binaryTree.preorder();

            System.out.print(" Inorder : ");

            binaryTree.inorder();

           

           /* test search method */

            int searchKey=50;

      System.out.println("Search result : "+ binaryTree.search(searchKey));

     

      /* display the node number in a tree */

      System.out.println("Nodes = "+ binaryTree.countNodes());

     

      /* test the tree is empty or not*/

    System.out.println("Empty status = "+ binaryTree.isEmpty());}

}

whole Java code,thanks!

Data Structures. 1. Task: Implement preorder, inordex, postorder methods. Put them in the class of BinaryTree and then test their correctness in the class BinarvTreeTest class. 2. Procedure: download the attached files and fulfil the task, run your codes. * 3. Submission: Submit your code via word file along with the snapshots of your results.

Explanation / Answer

public class BinaryTree {

   private BinaryTreeNode root;

   public BinaryTree(){

       root=null;

   }

   public boolean isEmpty(){

       return root==null;

   }

   /* Given a binary tree, print its nodes according to the

"bottom-up" postorder traversal. */

   private void printPostorder(BinaryTreeNode node)

   {

       if (node == null)

           return;

       // first recur on left subtree

       printPostorder(node.getLeft());

       // then recur on right subtree

       printPostorder(node.getRight());

       // now deal with the node

       System.out.print(node.getData() + " ");

   }

   /* Given a binary tree, print its nodes in inorder*/

   private void printInorder(BinaryTreeNode node)

   {

       if (node == null)

           return;

       /* first recur on left child */

       printInorder(node.getLeft());

       /* then print the data of node */

       System.out.print(node.getData() + " ");

       /* now recur on right child */

       printInorder(node.getRight());

   }

   /* Given a binary tree, print its nodes in preorder*/

   private void printPreorder(BinaryTreeNode node)

   {

       if (node == null)

           return;

       /* first print data of node */

       System.out.print(node.getData() + " ");

       /* then recur on left sutree */

       printPreorder(node.left);

       /* now recur on right subtree */

       printPreorder(node.right);

   }

   // Wrappers over above recursive functions

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

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

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

   public BinaryTreeNode insert(BinaryTreeNode node, int data)

   {

       if(node==null){

           node=new BinaryTreeNode(data);

       }

       else

       {

           if(node.getLeft()==null)

               node.left=insert(node.left,data);

           else

               node.right=insert(node.right,data);

       }

       return node;

   }

   public void insert(int data){

       root=insert(root, data);

   }

   private int countNodes(BinaryTreeNode r){

       if(r==null)

           return 0;

       else

       {

           int number=1;

           number+=countNodes(r.getLeft());

           number+=countNodes(r.getRight());

           return number;

       }

   }

   public int countNodes(){

       return countNodes(root);

   }

   public boolean search(BinaryTreeNode r, int data){

       if(r.getData()==data)

           return true;

       if(r.getLeft()!=null)

           if(search(r.getLeft(),data))

               return true;

       if(r.getRight()!=null)

           if(search(r.getRight(),data))

               return true;

       return false;

   }

   public boolean search(int val){

       return search(root, val);

   }

}

############

import java.util.Scanner;

public class BinaryTreeTest {

   public static void main(String[] args){

       BinaryTree binaryTree=new BinaryTree();

       /*Scanner scan =new Scanner(System.in);

System.out.println("Enter 10 integers to insert");

       */

       int[] a={50, 76,21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87,80};

       for(int i=0;i<16;i++){

           binaryTree.insert(a[i]);

       }

       /* Display tree */

       System.out.print(" Postorder : ");

       binaryTree.postorder();

       System.out.print(" Preorder : ");

       binaryTree.preorder();

       System.out.print(" Inorder : ");

       binaryTree.inorder();

       /* test search method */

       int searchKey=50;

       System.out.println("Search result : "+ binaryTree.search(searchKey));

       /* display the node number in a tree */

       System.out.println("Nodes = "+ binaryTree.countNodes());

       /* test the tree is empty or not*/

       System.out.println("Empty status = "+ binaryTree.isEmpty());}

}

/*

Sample run:

Postorder :

76 4 64 52 100 2 70 80 87 3 83 14 15 32 21 50

Preorder :

50 76 21 4 32 64 15 52 14 100 83 2 3 70 87 80

Inorder :

76 50 4 21 64 32 52 15 100 14 2 83 70 3 80 87 Search result : true

Nodes = 16

Empty status = false

*/

Please rate my answer