Question: In java using eclipse, implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class which implements the Tree interface. Your assignment is to implement

In java using eclipse, implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class which implements the Tree interface. Your assignment is to implement all of the abstract methods of the BinaryTree class recursively. Please use recursion in the code and include the main method in the BinarySearchTree class. Make sure the code produces the output based on what is required.

They are:

Insert.

Iterator (non-recursive).

Remove.

Search.

You must also implement an Iterator inner class for the BinarySearchTree class. You must submit a modified BinarySearchTree.java file with your source code.

Do not submit and do not modify the Tree.java or BinaryTree.java files.

/* * * Tree.java * */ public interface Tree extends Iterable { void insert(E data); void remove(E key); boolean search(E key); } /* * * BinaryTree.java * */ public abstract class BinaryTree implements Tree { protected class Node { protected Node(T data) { this.data = data; } protected T data; protected Node left; protected Node right; } protected Node root; } /* * * BinarySearchTree.java * */ import java.util.Iterator; public class BinarySearchTree> extends BinaryTree { public void insert(E data) { return; } public Iterator iterator() { return null; } public void remove(E key) { return; } public boolean search(E key) { return false; } }

And unlike this code from chegg, this is what I got from eclipse.

class BinarySearchTree> extends BinaryTree {

//This method new Generic Node

public Node createNewNode(E data) { return new Node(data); } //Method to insert node in BST public void insert(E data) { if(root==null)// Check if root is null i.e. no element in tree root=createNewNode(data);// Create a root node else { // find the parent node Node parent = null; Node current = root; while (current != null) { if (data.compareTo(current.data) < 0) // if current node's data is greater than data passed then move to //left subtree { parent = current; current = current.left; } else if (data.compareTo(current.data) > 0)// else move to right sub tree { parent = current; current = current.right; } else return;//Duplicate node not inserted } // Create the new node and attach it to the parent node i.e. inserting node to the leaf level if (data.compareTo(parent.data) < 0) parent.left = createNewNode(data); else parent.right = createNewNode(data); } }

public Iterator iterator() { return new InorderIterator(); } // Inner class InorderIterator class InorderIterator implements Iterator{ // Store the elements in a list private ArrayList list = new ArrayList(); private int current = 0;// Point to the current element in list public InorderIterator(){ inorder();// Traverse binary tree and store elements in list } //Inorder traversal from the root public void inorder(){ inorder(root); } // Inorder traversal from a subtree public void inorder(Node root){ if(root==null)return; inorder(root.left); list.add(root.data); inorder(root.right); } // The overriden method which indicates is there any next element available for traversing @Override public boolean hasNext() { if(current return true; return false; } // Get the current element and move cursor to the next @Override public E next() { return(list.get(current)); }

}

public void remove(E key) { // Locate the node to be deleted and also locate its parent node Node parent = null; Node current = root; while (current != null) { if (key.compareTo(current.data) < 0) { parent = current; current = current.left; } else if (key.compareTo(current.data) > 0) { parent = current; current = current.right; } else break; // Element is in the tree pointed by current }

if (current == null) return; // Element is not in the tree

// Case 1: current has no left children if (current.left == null) { // Connect the parent with the right child of the current node if (parent == null) { root = current.right; } else { if (key.compareTo(parent.data) < 0) parent.left = current.right; else parent.right = current.right; } } else { // Case 2: The current node has a left child // Locate the rightmost node in the left subtree of // the current node and also its parent Node parentOfRightMost = current; Node rightMost = current.left;

while (rightMost.right != null) { parentOfRightMost = rightMost; rightMost = rightMost.right; // Keep going to the right }

// Replace the element in current by the element in rightMost current.data = rightMost.data;

// Eliminate rightmost node if (parentOfRightMost.right == rightMost) parentOfRightMost.right = rightMost.left; else // Special case: parentOfRightMost == current parentOfRightMost.left = rightMost.left; } }

public boolean search(E key) { Node current = root; // Start from the root

while (current != null) { if (key.compareTo(current.data) < 0) { current = current.left; } else if (key.compareTo(current.data) > 0) { current = current.right; } else // element matches current.element return true; // Element is found }

return false;

} }

Output:

Error: Main method not found in class BinarySearchTree, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!