Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java class named BinaryTreeSortedList to implement the SortedList interf

ID: 3580266 • Letter: W

Question

Write a Java class named BinaryTreeSortedList to implement the SortedList interface(below), using a linked Binary Search Tree.

public interface SortedListInterface<T extends Comparable<? super T>> {

   /**

   * Adds a new entry to this list in its proper order, if it does not match

   * an existing object in the list.

   *

   * @param newEntry

   * An object to be added to the list.

   * @return True if the object was added to the list, otherwise return false.

   */

   public boolean add(T newEntry);

   /**

   * Removes the only occurrence of a specified entry from this

   * sorted list.

   *

   * @param anEntry

   * The object to be removed.

   * @return True if anEntry was located and removed; otherwise returns false.

   */

   public boolean remove(T anEntry);

   /**

   * Sees whether this list contains a given entry.

   *

   * @param anEntry

   * The object that is the desired entry.

   * @return True if the list contains anEntry, or false if not.

   */

   public boolean contains(T anEntry);

   /** Removes all entries from this list. */

   public void clear();

   /**

   * Gets the length of this list.

   *

   * @return the integer number of entries currently in the list.

   */

   public int getLength();

   /**

   * Sees whether this list is empty.

   *

   * @return true if the list is empty, or false if not.

   */

   public boolean isEmpty();

   /**

   * Retrieves all entries that are in this list in the order in which they

   * occur in the list.

   *

   * @return A newly allocated array of all the entries in the list. Note: If

   * the list is empty, the returned array is empty.

   */

   // Note: Can change return type to Object[] if T[] causes problems

   public T[] toArray();

  

   /**

   *

   * @return the sorted list of values with a space between each value

   */

   public String toString();

} // end SortedListInterface

Explanation / Answer

class Node{ int data; Node left; Node right; public Node(int data){ this.data = data; left = null; right = null; } } public class BinarySearchTree { public static Node root; public BinarySearchTree(){ this.root = null; } public boolean find(int id){ Node current = root; while(current!=null){ if(current.data==id){ return true; }else if(current.data>id){ current = current.left; }else{ current = current.right; } } return false; } public boolean delete(int id){ Node parent = root; Node current = root; boolean isLeftChild = false; while(current.data!=id){ parent = current; if(current.data>id){ isLeftChild = true; current = current.left; }else{ isLeftChild = false; current = current.right; } if(current ==null){ return false; } } //if i am here that means we have found the node //Case 1: if node to be deleted has no children if(current.left==null && current.right==null){ if(current==root){ root = null; } if(isLeftChild ==true){ parent.left = null; }else{ parent.right = null; } } //Case 2 : if node to be deleted has only one child else if(current.right==null){ if(current==root){ root = current.left; }else if(isLeftChild){ parent.left = current.left; }else{ parent.right = current.left; } } else if(current.left==null){ if(current==root){ root = current.right; }else if(isLeftChild){ parent.left = current.right; }else{ parent.right = current.right; } }else if(current.left!=null && current.right!=null){ //now we have found the minimum element in the right sub tree Node successor = getSuccessor(current); if(current==root){ root = successor; }else if(isLeftChild){ parent.left = successor; }else{ parent.right = successor; } successor.left = current.left; } return true; } public Node getSuccessor(Node deleleNode){ Node successsor =null; Node successsorParent =null; Node current = deleleNode.right; while(current!=null){ successsorParent = successsor; successsor = current; current = current.left; } //check if successor has the right child, it cannot have left child for sure // if it does have the right child, add it to the left of successorParent. // successsorParent if(successsor!=deleleNode.right){ successsorParent.left = successsor.right; successsor.right = deleleNode.right; } return successsor; } public void insert(int id){ Node newNode = new Node(id); if(root==null){ root = newNode; return; } Node current = root; Node parent = null; while(true){ parent = current; if(id
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote