Java Several tests in the binary search tree can't be fulfilled: 1. remove an element; 2. Test minimum--Find the minimum

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Java Several tests in the binary search tree can't be fulfilled: 1. remove an element; 2. Test minimum--Find the minimum

Post by answerhappygod »

Java
Several tests in the binary search tree can't be fulfilled: 1.
remove an element; 2. Test minimum--Find the minimum element; 2.
Test maximum---Find the maximum element
I guess problems come from deleteNode() method, findMin(), and
findMax().
Can someone please help me to edit it?
public class BinarySearchTreeIntegerTester {
public static void main(String[] args) {
BinaryTree listOfIntegers = new BinaryTree();
listOfIntegers .insert(50);
listOfIntegers
.insert(30);
listOfIntegers
.insert(20);
listOfIntegers
.insert(40);
listOfIntegers
.insert(70);
listOfIntegers
.insert(60);
listOfIntegers
.insert(80);
int user_choice = 9;
do
{
System.out.println("Binary Search Tree Tester Menu for Integer Data
Type\n");
System.out.println("1. Test remove");
System.out.println("2. Test minimum");
System.out.println("3. Test maximum");

System.out.println("Enter an option (1-3): ");
user_choice = new java.util.Scanner(System.in).nextInt();

if (user_choice == 1 )
{
System.out.print("Enter an integer to remove in BST: ");
int value = new java.util.Scanner(System.in).nextInt();
listOfIntegers.remove(value);
}
if (user_choice == 2 )
{
System.out.println("Find the maximum element in BST: " +
listOfIntegers.findMin());
}
if (user_choice == 3 )
{
System.out.println("Find the maximum element in BST: " +
listOfIntegers.findMax());
}
} while(true);
}

}
***************************Binary Tree***************
public class BinaryTree<T extends Comparable<T>> {
//Generic class
private TreeNode<T> root; // made TreeNode generic
public BinaryTree() {
root = null;
}
public void remove(T data_item){
if(contains(data_item))
{
root = deleteNode(data_item, root);
}
else
System.out.println("Data not found in BST.");

}
private TreeNode deleteNode(T item, TreeNode subRoot)
{
******************
}

public TreeNode findMin()
{
******************
}

public TreeNode findMax()
{
******************
}
public boolean contains(T search_value) {
return checkInTree(search_value, root);
}
private boolean checkInTree(T sv, TreeNode subRoot) {
if (subRoot == null) return false;
//using compare method of comparable class
else if (subRoot.getData().compareTo(sv) == 0) return true;
else if (subRoot.getData().compareTo(sv) == 1) return
checkInTree(sv, subRoot.getLeftNode());
else return checkInTree(sv, subRoot.getRightNode());
}
public void preOrderTraversal() {
this.preOrder(root);
System.out.println();
}
private void preOrder(TreeNode currentNode) {
if (currentNode == null) {
return;
}
System.out.print(currentNode.getData() + " ");
this.preOrder(currentNode.getLeftNode());
this.preOrder(currentNode.getRightNode());
}
private class TreeNode<T extends Comparable<T>> {
private T data; //made variable of type generic
private TreeNode leftNode;
private TreeNode rightNode;
public TreeNode(T newData, TreeNode left, TreeNode right)
{
this.data = newData;
this.leftNode = left;
this.rightNode = right;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public TreeNode getLeftNode() {
return leftNode;
}
public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
}
public TreeNode getRightNode() {
return rightNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply