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

JAVA PROGRAMMING : Binary Tree Removal ALSO WRITE A TEST CLASS TO TEST THE CODES

ID: 3574997 • Letter: J

Question

JAVA PROGRAMMING : Binary Tree Removal

ALSO WRITE A TEST CLASS TO TEST THE CODES (Tester.java)

For this assignment, you are required to implement remove method to remove a node from the BT.
1. Create a class named BT.java, which extends BTA, then implement two abstract methods definded in BTA.java
2. Create an aplication to test your implementation of the remove method. You simply create a file named
BTApp.java, then copy/paste the following code
======================================
public class BTApp{

public static void main(String[] args){
BT tree=new BT();
tree.insert(new Node(100));
tree.insert(new Node(20));
tree.insert(new Node(56));
tree.insert(new Node(199));
tree.insert(new Node(82));
tree.insert(new Node(82));
tree.inorder();
tree.remove(56);
tree.remove(new Node(82));
tree.inorder();
}
}

Explanation / Answer

// Java program to delete a tree

/* A binary tree node has data, pointer to left child
and pointer to right child */
class Node
{
   int data;
   Node left, right;

   Node(int d)
  
   {

   data = d;

       left = right = null;

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

/* Function to get right node */
public Node getRight()
{
return right;
}

    abstract void remove(Node node);
abstract Node insert(Node node, int data);
abstract public void inorder();

}

class BT extends Node
{

   static Node root;

/* 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 Node insert(Node node, int data)
{
if (node == null)
node = new Node(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);   
}
return node;
}

   /* This function is to delete a node from Binary tree */
   void remove(Node node)
   {
       if (node == null)
       {
           return;
       }

       /* first delete both subtrees */
       remove(node.left);
       remove(node.right);

       /* then delete the node */
       System.out.println("The deleted node is " + node.data);
       node = null;
   }

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

public class BTApp{
public static void main(String[] args){
BT tree=new BT();
tree.insert(new Node(100));
tree.insert(new Node(20));
tree.insert(new Node(56));
tree.insert(new Node(199));
tree.insert(new Node(82));
tree.insert(new Node(82));
tree.inorder();
tree.remove(56);
tree.remove(new Node(82));
tree.inorder();
}
}