Question: Whats wrong with my code? Assignment was to implement print reverse, remove, and to print an actual tree for this binary search tree class. here
Whats wrong with my code?
Assignment was to implement print reverse, remove, and to print an actual tree for this binary search tree class. here is my output when i ran the tester class that i made. The reverse order isnt working, and getting an weird error when i try to remove. everything is compling okay though. have not gotten to printing the tree because i wanted to get the print reverse and remove out of the way first.
*** looking for tips, lines to fix/what i did wrong, please try to avoid changing all the work i did.

public class MyTree{
private TreeNode root; public MyTree(){ root=null; } public void remove(int data){ //implement this method to remove a node with the same data value TreeNode focusNode = root; TreeNode valueNode = null; // will be variable to keep track of the node containing value we are removing if (root == null){} // cant remove anything if root is null else valueNode = removeHelper(focusNode, data); // to delete node if it has 0-1 children if (valueNode.getLeft() == null){ valueNode.setData(valueNode.getRight().getData()); //= valueNode.getRight().getData(); TreeNode temp = valueNode.getRight(); //valueNode.getRight().setData(null); temp = null; } else if (valueNode.getRight() == null){ valueNode.setData(valueNode.getLeft().getData()); //= valueNode.getLeft().getData(); TreeNode temp = valueNode.getLeft(); //valueNode.getLeft().setData(null); temp = null; } // to delete if it has 2 children or the root else if (valueNode.getLeft() != null && valueNode.getRight() != null){ valueNode.setData(getSuccessor(valueNode).getData()); //=getSuccessor(valueNode).getData(); TreeNode temp = getSuccessor(valueNode); //getSuccessor(valueNode).setData(null); temp = null; } } // end method // will find successor if valuenode has two children private TreeNode getSuccessor(TreeNode node){ TreeNode temp = node.getRight(); // temp node is the one to the down>right of the one we are trying to replace while(temp != null) temp = temp.getLeft(); return temp; } // searches through tree until it finds the node with the value, then returns the node private TreeNode removeHelper(TreeNode node, int data){ if (node.getData() == data) return node; else if (data
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
