Question

Problem Binary Search Tree Implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class. Both can be seen here. Your assignment is to implement all of the abstract methods of the BinaryTree class recursively. 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 BinaryTree.java file Back What need to be done
media%2F30e%2F30e9e2b7-b361-40de-b5cd-6eWhat’s given
media%2F277%2F277b66d5-90b7-44e8-9143-23
media%2Fceb%2Fcebf1c22-6a4c-4aa5-a31b-11
0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI I am providing the BinarySearchTree class as you already have the BinaryTree class and Main class. Let me know if you have any issue

public class BinarySearchTree<E extends Comparable<? super E>> extends BinaryTree<E> {

private Node<E> findIOP(Node<E> curr) {

for (curr = curr.left; curr.right != null; curr = curr.right)

;

return curr;

}

public void insert(E data) {

Node<E> temp = new Node<E>(data);

if (root == null) {

root = temp;

} else {

Node<E> curr = root;

while (true) {

if (data.compareTo(curr.data) <= 0) {

if (curr.left != null) {

curr = curr.left;

} else {

curr.left = temp;

break;

}

} else {

if (curr.right != null) {

curr = curr.right;

} else {

curr.right = temp;

break;

}

}

}

}

}

public Iterator<E> iterator() {

return null;

}

public void remove(E data) {

if (root != null) {

if (data.compareTo(root.data) == 0) {

if (root.left == null || root.right == null) {

root = root.left != null ? root.left : root.right;

} else {

Node<E> iop = findIOP(root);

E temp = root.data;

root.data = iop.data;

iop.data = temp;

if (root.left == iop) {

root.left = root.left.left;

} else {

Node<E> curr = root.left;

while (curr.right != iop) {

curr = curr.right;

}

curr.right = curr.right.left;

}

}

} else {

Node<E> curr = root;

int cmp;

while (true) {

cmp = data.compareTo(curr.data);

if (cmp < 0 && curr.left != null && data.compareTo(curr.left.data) != 0) {

curr = curr.left;

} else if (cmp > 0 && curr.right != null && data.compareTo(curr.right.data) != 0) {

curr = curr.right;

} else {

break;

}

}

if (cmp < 0 && curr.left != null) {

if (curr.left.left == null || curr.left.right == null) {

curr.left = curr.left.left != null ? curr.left.left : curr.left.right;

} else {

Node<E> iop = findIOP(curr.left);

E temp = curr.left.data;

curr.left.data = iop.data;

iop.data = temp;

if (curr.left.left == iop) {

curr.left.left = curr.left.left.left;

} else {

Node<E> node = curr.left.left;

while (node.right != iop) {

node = node.right;

}

node.right = node.right.left;

}

}

} else if (cmp > 0 && curr.right != null) {

if (curr.right.left == null || curr.right.right == null) {

curr.right = curr.right.left != null ? curr.right.left : curr.right.right;

} else {

Node<E> iop = findIOP(curr.right);

E temp = curr.right.data;

curr.right.data = iop.data;

iop.data = temp;

if (curr.right.left == iop) {

curr.right.left = curr.right.left.left;

} else {

Node<E> node = curr.right.left;

while (node.right != iop) {

node = node.right;

}

node.right = node.right.left;

}

}

}

}

}

}

public boolean search(E data) {

Node<E> curr = root;

while (curr != null) {

if (data.compareTo(curr.data) == 0) {

return true;

} else if (data.compareTo(curr.data) < 0) {

curr = curr.left;

} else {

curr = curr.right;

}

}

return false;

}

}

Add a comment
Know the answer?
Add Answer to:
What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the ...

    JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the AbstractList class. Both can be seen here. Your assignment is to implement (recursively) all of the abstract methods of the AbstractList class. They are: insert (recursive) iterator remove (recursive) retrieve search (recursive) You must also implement an Iterator inner class for the SortedList class. You must submit a modified SortedList.java file with your source code. Do not submit and do not modify the List.java or...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search...

    *****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE...

    please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to...

    Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if any) that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT