Could you please help me with this in Java

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> extends Collection {
//public class BinarySearchTree extends Collection {

//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 _root = null;

//removes the largest element from the SUBTREE starting at root
protected BinaryNode removeLargest(BinaryNode root)
{
//NULL tree/empty tree
if (root == null) return null;

//BASE CASE: root is the largest
if (root.getRightChild() == null)
{
return root.getLeftChild();
}

//Recursive
BinaryNode new_right = removeLargest(root.getRightChild());

//reconfigure our right pointer to the returned value
root.setRightChild(new_right);

return root;
}

//removes the smallest element
protected BinaryNode removeSmallest(BinaryNode root)
{
//NULL tree/empty tree
if (root == null) return null;

//without recursion
BinaryNode pre = null;

//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 findLargest(BinaryNode root)
{
while(root != null && root.getRightChild() != null)
root = root.getRightChild();
return root;
}

protected BinaryNode findSmallest(BinaryNode root)
{
while(root != null && root.getLeftChild() != null)
root = root.getLeftChild();
return root;
}

protected BinaryNode addElementHelper(BinaryNode root, T item)
{
// 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 removeElementHelper(BinaryNode root, T item)
{
//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 result = removeElementHelper(
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 result = removeElementHelper(
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 _left_child;

//right
BinaryNode _right_child;

public BinaryNode()
{
_left_child = null;
_right_child = null;
}

public BinaryNode(T value)
{
_value = value;
}

public BinaryNode(BinaryNode left, BinaryNode right)
{
_left_child = left;
_right_child = right;
}

public boolean isLeaf()
{
return _left_child == null && _right_child == null;
}

public BinaryNode getLeftChild()
{
return _left_child;
}

public void setLeftChild(BinaryNode left)
{
_left_child = left;
}

public BinaryNode getRightChild()
{
return _right_child;
}

public void setRightChild(BinaryNode right)
{
_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 root = new BinaryNode<>(“A”);

BinaryNode node = new BinaryNode<>(“B”);
node.setLeftChild(new BinaryNode(“D”));
node.setRightChild(new BinaryNode(“E”));

root.setLeftChild(node);

node = new BinaryNode<>(“C”);
node.setRightChild(new BinaryNode(“F”));

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 = new BinarySearchTree<>();
bst.addElement(“C”);
bst.addElement(“B”); //E, A, D, F

}

public static void preorder(BinaryNode root)
{
if (root == null) return;

//operations we perform on a certain node

Stack> stack = new 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 root)
{
if (root == null) return;

postorder(root.getLeftChild());
postorder(root.getRightChild());
System.out.println(root.getValue());
}

public static void inorder(BinaryNode root)
{
if (root == null) return;

inorder(root.getLeftChild());
System.out.println(root.getValue());
inorder(root.getRightChild());
}
}

Complete Answer:

Get Instant Help in Homework Asap
Get Instant Help in Homework Asap
Calculate your paper price
Pages (550 words)
Approximate price: -
Open chat
1
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?