Question
Binary Search Tree Overview You are given the classes BinaryTree1.java,, BinarySearchTree.java , and BinarySearchTreeTest.java . Add the following member methods to the class BinarySearchTree :
Binary Search Tree
Overview
You are given the classes BinaryTree1.java,, BinarySearchTree.java , and BinarySearchTreeTest.java .
Add the following member methods to the class BinarySearchTree :
depthOfMinValue Recursive () : returns the depth of the node in the calling BST which contains the minimum value. Use recursion.
depthOfMinValue Iterative ()):: returns the depth of the node in the calling BST which contains the minimum value. Do not use recursion.
equal Struct (BinarySearchTree bst2): Check if the calling BST and the bst2 have the same structure.
Test your methods in another class BinarySearchTree Test that uses the class BinarySearchTree and completes a number of operations on a single BinarySearchTree object and multiple BinarySearchTree objects.
implementation Requirements
You must use the classes provided to you.
This requirement means that you cannot use Java API class for Binary Search Tree.
You must implement the required methods as stated in the BinarySearchTree class.
---BinarySearchTree.java---
import java.util.List;
import java.util.ArrayList;
public class BinarySearchTree
protected boolean addReturn; protected E deleteReturn; @Override public E find(E target) { return find(root, target); }
private E find(Node
int compResult = target.compareTo(localRoot.data); if (compResult == 0) { return localRoot.data; } else if (compResult < 0) { return find(localRoot.left, target); } else { return find(localRoot.right, target); } } @Override public boolean add(E item) { root = add(root, item); return addReturn; }
private Node
private Node
// Search for item to delete. int compResult = item.compareTo(localRoot.data); if (compResult < 0) { // item is smaller than localRoot.data. localRoot.left = delete(localRoot.left, item); return localRoot; } else if (compResult > 0) { // item is larger than localRoot.data. localRoot.right = delete(localRoot.right, item); return localRoot; } else { // item is at local root. deleteReturn = localRoot.data; if (localRoot.left == null) { // If there is no left child, return right child // which can also be null. return localRoot.right; } else if (localRoot.right == null) { // If there is no right child, return left child. return localRoot.left; } else { // Node being deleted has 2 children, replace the data // with inorder predecessor. if (localRoot.left.right == null) { // The left child has no right child. // Replace the data with the data in the // left child. localRoot.data = localRoot.left.data; // Replace the left child with its left child. localRoot.left = localRoot.left.left; return localRoot; } else { // Search for the inorder predecessor (ip) and // replace deleted node's data with ip. localRoot.data = findLargestChild(localRoot.left); return localRoot; } } } } private E findLargestChild(Node
}
---BinaryTree1---
import java.io.BufferedReader; import java.io.IOException; import java.io.Serializable; import java.util.Scanner;
public class BinaryTree1
protected static class Node
public E data; public Node
public Node(E data) { this.data = data; left = null; right = null; }
@Override public String toString() { return data.toString(); } } protected Node
public BinaryTree1() { root = null; }
protected BinaryTree1(Node
public BinaryTree1(E data, BinaryTree1
public BinaryTree1
public BinaryTree1
public E getData() { if (root != null) { return root.data; } else { return null; } }
public boolean isLeaf() { return (root == null || (root.left == null && root.right == null)); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); preOrderTraverse(root, 1, sb); return sb.toString(); }
private void preOrderTraverse(Node
public static BinaryTree1
}
---BinarySearchTreeTest---
public class BinarySearchTreeTest { public static void main(String[] args) { //create a binary search tree BinarySearchTree
---Search Tree---
public interface SearchTree
boolean add(E item);
boolean contains(E target);
E find(E target);
E delete(E target);
boolean remove(E target); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started