Hi everyone. I have pretty much done all of the coding for my assignment apart from one method.
The main aim of the assignment is to read in entries from a file and put them into a BST sorted alphabetically. The objects on each node contain a String and a double attribute. The method I am stuck on is to return the largest double. Being as they are sorted in alphabetical order I cannot just call to the right while there is a right node.
My thinking with the code I pasted in is to get the maximum value from the left hand side and the right hand side. And then to return the larger of those two values. I tried this and I did not actually get the largest. I am thinking that I am missing left nodes on the right hand side and vice versa (fortunately the largest must fall into one of these two categories or I might have missed this error). There is also a largestRight method to mirror what the left one does. I just can't seem to think how to get past this problem. Before this all I was able to do was return the value of the root node's double attribute. This seems to be the closest I have got so far ...
I am also thinking there is a much simpler solution. I will be very grateful for any help in getting me to understand how to get this right.
public double largest() { return largestLeft(root); } private double largestLeft(TreeNode node) { double maxValue = -1.0; while ( node != null ) { if (node.doubleValue > maxValue) maxValue = node.doubleValue; node=node.left; } return maxValue; }