Could you please help me with this in Java
fully implement the addElementHelper and removeElementHelper functions inside the BinarySearchTree class. in addition, you should also write test cases in “MA3_main.java”.
import java.util.Vector;
public class BinarySearchTree
//public class BinarySearchTree
//keeps track of the remove direction (left or right)
private int _remove_counter = 0;
//keeps track of BST size
private int _size_counter = 0;
protected BinaryNode
//removes the largest element from the SUBTREE starting at root
protected BinaryNode
{
//NULL tree/empty tree
if (root == null) return null;
//BASE CASE: root is the largest
if (root.getRightChild() == null)
{
return root.getLeftChild();
}
//Recursive
BinaryNode
//reconfigure our right pointer to the returned value
root.setRightChild(new_right);
return root;
}
//removes the smallest element
protected BinaryNode
{
//NULL tree/empty tree
if (root == null) return null;
//without recursion
BinaryNode
//root is the smallest value
while (root.getLeftChild() != null)
{
pre = root;
root = root.getLeftChild();
}
//check if pre is null
if(pre != null)
pre.setLeftChild(root.getRightChild());
return pre;
}
protected BinaryNode
{
while(root != null && root.getRightChild() != null)
root = root.getRightChild();
return root;
}
protected BinaryNode
{
while(root != null && root.getLeftChild() != null)
root = root.getLeftChild();
return root;
}
protected BinaryNode
{
// MA3 TODO
//check for null first
//if null, create new node return pointer to that node
//if not null, compare value, add to correct place
//you can choose whether to use recursion or not
//to compare, use this method of the item: item.compareTo(/*arguments to compare to*/)
//always return root because we don’t know where the recursion ends
return root;
}
protected BinaryNode
{
//check for null
//if null, return it.
if (root == null)
{
return root;
}
//three possibilities:
//we found the item (root value == item)
//item is less than root (item < root)
//item is greater than root (item > root)
if (item.compareTo(root.getValue()) == 0)
{
//increment removal counter
_remove_counter++;
//we found the item
//do we remove from the left or right?
if (_remove_counter % 2 == 0)
{
//let’s assume we are removing from the left when it’s an even number
// MA3 TODO
}
else
{
//remove from the right subtree when it’s an odd number
// MA3 TODO
}
}
else if (item.compareTo(root.getValue()) < 0)
{
//item is less than root
//go on finding it in the left subtree
BinaryNode
root.getLeftChild(),
item
);
//the recursive call *might* have altered our
//left child’s structure. Inform root of this change
root.setLeftChild(result);
}
else
{
//item is greater than root
//finding it in the right subtree
BinaryNode
root.getRightChild(),
item
);
root.setRightChild(result);
}
return root;
}
@Override
public void addElement(T item) {
_root = addElementHelper(_root, item);
_size_counter++;
}
public void removeElement(T item) {
_root = removeElementHelper(_root, item);
_size_counter–;
}
@Override
public boolean isEmpty() {
return _root == null;
}
@Override
public int getSize() {
return _size_counter;
}
}
public class BinaryNode
//underlying value in the node
private T _value;
//a *pointer* to the left child
//In Java, just the left child
BinaryNode
//right
BinaryNode
public BinaryNode()
{
_left_child = null;
_right_child = null;
}
public BinaryNode(T value)
{
_value = value;
}
public BinaryNode(BinaryNode
{
_left_child = left;
_right_child = right;
}
public boolean isLeaf()
{
return _left_child == null && _right_child == null;
}
public BinaryNode
{
return _left_child;
}
public void setLeftChild(BinaryNode
{
_left_child = left;
}
public BinaryNode
{
return _right_child;
}
public void setRightChild(BinaryNode
{
_right_child = right;
}
public T getValue()
{
return _value;
}
public void setValue(T value)
{
_value = value;
}
}
public abstract class Collection
public abstract void addElement(T item);
public abstract boolean isEmpty();
public abstract int getSize();
}
public class MA3_main {
public static void main(String[] args) {
//Build a temporary tree for demo
BinaryNode
BinaryNode
node.setLeftChild(new BinaryNode
node.setRightChild(new BinaryNode
root.setLeftChild(node);
node = new BinaryNode<>(“C”);
node.setRightChild(new BinaryNode
root.setRightChild(node);
//root is the root of the tree.
//let’s try traversal now!
System.out.println(“————“);
System.out.println(“Pre-order Travesal”);
preorder(root);
System.out.println();
System.out.println(“————“);
System.out.println(“Post-order Travesal”);
postorder(root);
System.out.println();
System.out.println(“————“);
System.out.println(“In-order Travesal”);
inorder(root);
System.out.println();
BinarySearchTree
bst.addElement(“C”);
bst.addElement(“B”); //E, A, D, F
}
public static void preorder(BinaryNode
{
if (root == null) return;
//operations we perform on a certain node
Stack
stack.push(root);
while(!stack.isEmpty())
{
root = stack.pop();
System.out.println(root.getValue());
if (root.getRightChild() != null)
stack.push(root.getRightChild());
if (root.getLeftChild() != null)
stack.push(root.getLeftChild());
}
//preorder(root.getLeftChild());
//preorder(root.getRightChild());
}
public static void postorder(BinaryNode
{
if (root == null) return;
postorder(root.getLeftChild());
postorder(root.getRightChild());
System.out.println(root.getValue());
}
public static void inorder(BinaryNode
{
if (root == null) return;
inorder(root.getLeftChild());
System.out.println(root.getValue());
inorder(root.getRightChild());
}
}