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.

Whats wrong with my code? Assignment was to implement print reverse, remove,

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 Re printed in order: 12 25 30 31 40 47 50 68 tree size is: 8 printed in reverse: 68 50 12 25 30 31 40 47 After removing 40: Exception in thread "main" java.lang.NullPointerException at MyTree.remove (MyTree.java:27) at MyTreeTester.main (MyTreeTester.java:20)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!