Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP! I need this written in Java for please! PLEASE FOLLOW the coding requirements!! The skeleton for the BinarySearchTree.java is below: import java.util.List; import java.util.ArrayList;

HELP! I need this written in Java for please! PLEASE FOLLOW the coding requirements!!

image text in transcribed

image text in transcribed

The skeleton for the BinarySearchTree.java is below:

import java.util.List; import java.util.ArrayList;

public class BinarySearchTree> extends BinaryTree1 implements SearchTree {

protected boolean addReturn;

protected E deleteReturn;

@Override public E find(E target) {

return find(root, target); }

private E find(Node localRoot, E target) { if (localRoot == null) { //base case 1: target is not in this BST tree return null; } // Compare the target with the data at the current root node). int compResult = target.compareTo(localRoot.data); if (compResult == 0) { //base case 2: found the target in current root node return localRoot.data; } else if (compResult

private Node add(Node localRoot, E item) { if (localRoot == null) { // item is not in the tree and insert it. addReturn = true; return new Node(item); } else if (item.compareTo(localRoot.data) == 0) { // item is equal to localRoot.data, a duplicate, so cannot insert it addReturn = false; return localRoot; } else if (item.compareTo(localRoot.data)

private Node delete(Node localRoot, E item) { if (localRoot == null) { // item is not in the tree. deleteReturn = null; return localRoot; }

// Search for item to delete. int compResult = item.compareTo(localRoot.data); 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) { //1 child // If there is no left child, return right child // which can also be null. return localRoot.right; } else if (localRoot.right == null) { //1 child // If there is no right child, return left child. return localRoot.left; } else { //2 children // 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 local root data with the data in the // left child. localRoot.data = localRoot.left.data; // Replace the left child with left child's 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 parent) { // If the right child has no right child, then the right child is // the inorder predecessor. if (parent.right.right == null) { //right child is inorder predecessor E returnValue = parent.right.data; //remove the inorder predecessor by replacing it with its left child parent.right = parent.right.left; return returnValue; } else { //keep processing, starting with the right child. return findLargestChild(parent.right); } } public boolean contains(E target) { return false; }

public boolean remove(E target) { return false; } }

...and SearchTree.java

public interface SearchTree> {

boolean add(E item);

boolean contains(E target);

E find(E target);

E delete(E target);

boolean remove(E target); }

BinaryTree1.java:

import java.io.BufferedReader; import java.io.IOException; import java.io.Serializable; import java.util.Scanner;

public class BinaryTree1 implements Serializable {

protected static class Node implements Serializable {

protected E data; protected Node left; protected Node right;

protected Node(E data) { this.data = data; left = null; right = null; }

@Override public String toString() { return data.toString(); } }

protected Node root;

public BinaryTree1() { root = null; }

protected BinaryTree1(Node root) { this.root = root; }

public BinaryTree1(E data, BinaryTree1 leftTree, BinaryTree1 rightTree) { root = new Node(data);

if (leftTree != null) { root.left = leftTree.root; } else { root.left = null; } if (rightTree != null) { root.right = rightTree.root; } else { root.right = null; } }

public BinaryTree1 getLeftSubtree() { if (root != null && root.left != null) { //if there is a left subtree return new BinaryTree1(root.left); } else { return null; } }

public BinaryTree1 getRightSubtree() { if (root != null && root.right != null) { //if there is a right subtree return new BinaryTree1(root.right); } else { return null; } }

public E getData() { if (root != null) { return root.data; } else { return null; } }

public boolean isLeaf() { //empty tree or 1-node tree 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 node, int depth, StringBuilder sb) { for (int i = 1; i

public static BinaryTree1 readBinaryTree1(Scanner scan) { String data = scan.next(); if (data.equals("null")) { //data (i.e. current root node's data) is "null" return null; } else { BinaryTree1 leftTree = readBinaryTree1(scan); BinaryTree1 rightTree = readBinaryTree1(scan); return new BinaryTree1(data, leftTree, rightTree); } }

}

Add the followirg member methods to the class BinarySearchTree y ah hom nae Each item in thegiven BinearySearchT ree object has to be added to the calling BinarySearchTree object. . equals: check if both the structure and the content of a given BinarySearchTree object is exactly the same as the calling BinarySearchTree object. . findSmallest: find the smallest data in this binary search tree, ie. The calling BinarySearchTree object. The smallest data is the data in the leftmost node of the binary search tree

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions