How to do the main method for this program?
BinaryNode.java
public class BinaryNode
implements Parent{
/** The item associated with this node. */
private E item;
/** The node at the root of the left subtree. */
private BinaryNode left;
/** The node at the root of the right subtree. */
private BinaryNode 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 left,
BinaryNode right) {
this.item = item;
this.left = left;
this.right = right;
}
/** Put item in a node with the specified subtrees. */
public BinaryNode 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 getLeft() {
return left;
}
/** Return the root of the right subtree. */
public BinaryNode 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 left) {
this.left = left;
}
/** Replace the right subtree with the one rooted at right. */
public void setRight(BinaryNode right) {
this.right = right;
}
public void setChild(int direction, BinaryNode 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> q = new ArrayQueue>();
q.add (this);
while (!(q.isEmpty())) {
BinaryNode node = q.remove ();
result += node.item;
if (node.left != null) {
q.add(node.left);
}
if (node.right != null) {
q.add(node..
How to do the main method for this programBinaryNode.javapublic.pdf
1. How to do the main method for this program?
BinaryNode.java
public class BinaryNode
implements Parent{
/** The item associated with this node. */
private E item;
/** The node at the root of the left subtree. */
private BinaryNode left;
/** The node at the root of the right subtree. */
private BinaryNode 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 left,
BinaryNode right) {
this.item = item;
this.left = left;
this.right = right;
}
/** Put item in a node with the specified subtrees. */
public BinaryNode getChild(int direction) {
if (direction < 0) {
2. 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 getLeft() {
return left;
}
/** Return the root of the right subtree. */
public BinaryNode 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 left) {
this.left = left;
}
/** Replace the right subtree with the one rooted at right. */
public void setRight(BinaryNode right) {
this.right = right;
}
public void setChild(int direction, BinaryNode child) {
if (direction < 0) {
3. 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;
}
4. /** 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> q = new ArrayQueue>();
q.add (this);
while (!(q.isEmpty())) {
BinaryNode node = q.remove ();
result += node.item;
if (node.left != null) {
q.add(node.left);
}
if (node.right != null) {
q.add(node.right);
}
}
return result;
}
5. }
----------------------------------------------------------
BinarySearchTree.java
public class BinarySearchTree>
implements Parent,Set{
/** Root node. */
private BinaryNode root;
/** A BinarySearchTree is initially empty. */
public BinarySearchTree() {
root = null;
}
public void add(E target) {
Parent parent = this;
BinaryNode 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(target));
}
public boolean contains(E target) {
BinaryNode node = root;
while (node != null) {
6. 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 getChild(int direction) {
return root;
}
// Remove method
public void remove(E target) {
Parent parent = this;
BinaryNode 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;
}
}
7. /**
* Remove the leftmost descendant of nde and return the
* item contained in the removed node.
**/
protected E removeLeftmost(BinaryNode node, Parent 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 child) {
root = child;
}
public int size() {
return size(root);
}
/** Return the size of the subtree rooted at node. */
protected int size(BinaryNode 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.
8. **/
protected void spliceOut(BinaryNode node,
Parent 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 {
/**
* Return the left child if direction < 0, or the right child
* otherwise.
**/
public BinaryNode 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 child);
}
------------------------------------------
/** A set of Objects. */
public interface Set {
/** Add target to this Set. */
public void add(E target);
/** Return true if this Set contains target. */
public boolean contains(E target);
9. /** 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
}