How to do the main method for this program BinaryNodejava pu

How to do the main method for this program?

BinaryNode.java

public class BinaryNode<E>
implements Parent<E>{

/** The item associated with this node. */
private E item;
/** The node at the root of the left subtree. */
private BinaryNode<E> left;
/** The node at the root of the right subtree. */
private BinaryNode<E> right;
/** Put item in a leaf node. */
public BinaryNode(E item) {
    this.item = item;
    // left and right are set to null by default
}

/** no-argument constructor sets everything to null */
public BinaryNode() {
    item = null;
    left = null;
    right = null;
}

/** Put item in a node with the specified subtrees. */
public BinaryNode(E item, BinaryNode<E> left,
                    BinaryNode<E> right) {
    this.item = item;
    this.left = left;
    this.right = right;
}

    /** Put item in a node with the specified subtrees. */
public BinaryNode<E> getChild(int direction) {
    if (direction < 0) {
      return left;
    } else {
      return right;
    }
}

/** Return the item associated with this node. */
public E getItem() {
    return item;
}
/** Return the root of the left subtree. */
public BinaryNode<E> getLeft() {
    return left;
}
/** Return the root of the right subtree. */
public BinaryNode<E> getRight() {
    return right;
}
/** Return true if this is a leaf. */
public boolean isLeaf() {
    return (left == null) && (right == null);
}
/** Replace the item associated with this node. */
public void setItem(E item) {
    this.item = item;
}
/** Replace the left subtree with the one rooted at left. */
public void setLeft(BinaryNode<E> left) {
    this.left = left;
}
/** Replace the right subtree with the one rooted at right. */
public void setRight(BinaryNode<E> right) {
    this.right = right;
}


public void setChild(int direction, BinaryNode<E> child) {
    if (direction < 0) {
      left = child;
    } else {
      right = child;
    }
}

/**
* Return the String representation of the tree rooted at this node
* traversed preorder.
**/
public String toStringPreorder() {
String result = \"\";
result += item;
if (left != null) {
    result += left.toStringPreorder();
}
if (right != null) {
    result += right.toStringPreorder();
}
return result;
}



/** Return a String representation of the tree rooted at this node
    * traversed inorder.
    **/
public String toStringInOrder() {
    String result = \"\";
    if (left != null) {
      result += left.toStringInOrder();
    }
    result += item;
    if (right != null) {
      result += right.toStringInOrder();
    }
    return result;
}
  
    /** Return a String representation of the tree rooted at this node,
      * traversed postorder.
      **/
    public String toStringPostorder() {
      String result = \"\";
      if (left != null) {
        result += left.toStringPostorder();
      }
      if (right != null) {
        result += right.toStringPostorder();
      }
      result += item;
      return result;
    }
  
    /**
     * Return a String representation of the tree rooted at this node,
     * traversed level order.
     **/
    public String toStringLevelOrder() {
      String result = \"\";
      Queue<BinaryNode<E>> q = new ArrayQueue<BinaryNode<E>>();
      q.add (this);
      while (!(q.isEmpty())) {
        BinaryNode<E> node = q.remove ();
        result += node.item;
        if (node.left != null) {
          q.add(node.left);
        }
        if (node.right != null) {
          q.add(node.right);
        }
      }
      return result;
    }
}


----------------------------------------------------------

BinarySearchTree.java

public class BinarySearchTree<E extends Comparable<E>>
implements Parent<E>,Set<E>{
/** Root node. */
private BinaryNode<E> root;
/** A BinarySearchTree is initially empty. */
public BinarySearchTree() {
    root = null;
}

    public void add(E target) {
    Parent<E> parent = this;
    BinaryNode<E> node = root;
    int comparison = 0;
    while (node != null) {
      comparison = target.compareTo(node.getItem());
      if (comparison < 0) {        // Go left
        parent = node;
        node = node.getLeft();
      } else if (comparison == 0) {    // It\'s already here
        return;
      } else {
        parent = node;
        node = node.getRight();
      }
    }
    parent.setChild(comparison, new BinaryNode<E>(target));
}
  
    public boolean contains(E target) {
    BinaryNode<E> node = root;
    while (node != null) {
      int comparison = target.compareTo(node.getItem());
      if (comparison < 0) {             // go left
        node = node.getLeft();
      } else if (comparison == 0) {        // found it
        return true;
      } else {
        node = node.getRight();
      }
    }
    return false;
}
  
  
public BinaryNode<E> getChild(int direction) {
    return root;
}

// Remove method
public void remove(E target) {
    Parent<E> parent = this;
    BinaryNode<E> node = root;
    int direction = 0;
    while (node != null) {
      int comparison = target.compareTo(node.getItem());
      if (comparison < 0) {              // Go left
        parent = node;
        node = node.getLeft();
      } else if (comparison == 0) { // Found it
        spliceOut(node, parent, direction);
        return;
      } else {                          // Go right
        parent = node;
        node = node.getRight();
      }
      direction = comparison;
    }
}

    /**
   * Remove the leftmost descendant of nde and return the
   * item contained in the removed node.
   **/
protected E removeLeftmost(BinaryNode<E> node, Parent<E> parent) {
    int direction = 1;
    while (node.getLeft() != null) {
      parent = node;
      direction = -1;
      node = node.getLeft();
    }
    E result = node.getItem();
    spliceOut(node, parent, direction);
    return result;
}

    public void setChild(int direction, BinaryNode<E> child) {
    root = child;
}

public int size() {
    return size(root);
}
/** Return the size of the subtree rooted at node. */
protected int size(BinaryNode<E> node) {
    if (node == null) {
      return 0;
    } else {
      return 1 + size(node.getLeft()) + size(node.getRight());
    }
}

/**
   * Remove node, which is a child of parent. Direction is positive
   * if node is the right child of parent. negative if it is the
   * left child.
   **/
protected void spliceOut(BinaryNode<E> node,
                           Parent<E> parent,
                           int direction) {
    if (node.getLeft() == null) {
      parent.setChild(direction, node.getRight());
    } else if (node.getRight() == null) {
      parent.setChild(direction, node.getLeft());
    } else {
      node.setItem(removeLeftmost(node.getRight(), node));
    }
}
}

------------------------------------

public interface Parent<E> {
/**
   * Return the left child if direction < 0, or the right child
   * otherwise.
   **/
public BinaryNode<E> getChild(int direction);

/**
   * Replace the specified child of this parent with the new child.
   * If direction < 0, replace the left child. Otherwise, replace
   * the right child.
   **/
public void setChild(int direction, BinaryNode<E> child);
}

------------------------------------------

/** A set of Objects. */
public interface Set<E> {
/** Add target to this Set. */
public void add(E target);
/** Return true if this Set contains target. */
public boolean contains(E target);
/** Remove target from this Set. */
public void remove(E target);
/** Return the number of elements in this Set. */
public int size();
}

Solution

With the given here we could actually perform various functions related to binary search tree. I am providing few example function calls in the below main function.

public static void main(String args[])
{
   BinarySearchTree bst = new BinarySearchTree(); //Initially an empty tree would be created.
   System.out.println(\"Insert elements into the BST\");
   bst.add(10); //Insert 10 into the tree, 10 would become root
   bst.add(8); //Insert 8 into BST, 8 would be inserted as left child of 10
   bst.add(12); //Following rules of BST, 12 would be inserted as right child of 12
   bst.add(11);
   bst.add(15);
   bst.add(6);
   bst.add(9);
   System.out.println(\"Size of the tree is \",bst.size());
   System.out.println(\"In order traversal of the tree\",bst.toStringInOrder());
   System.out.println(\"Does the BST contains 11 ?\",bst.contains(11));
   bst.remove(6); //Remove 6 from the tree
   System.out.println(\"Size of the tree is \",bst.size());
   System.out.println(\"In order traversal of the tree\",bst.toStringInOrder());
   //Much more functions to explore
}

How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.
How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.
How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.
How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.
How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.
How to do the main method for this program? BinaryNode.java public class BinaryNode<E> implements Parent<E>{ /** The item associated with this node.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site