Re: find largest value in BST
Thanks for your time with this Norm. For now I have to rest and sleep so I will try again tomorrow.
I cannot work out the toString() method still. But my main issue here is how to get it to return the biggest value which we know to be 2.16
That doesn't mean I don't want to work out what you want me to do the toString() method for or how to do it because I am curious now. But I am spending more time on this than I should and have other assignments waiting to be done. I will not give up on this, but need to prioritise.
Re: find largest value in BST
The inorderHelper() method looks like its a start in the right direction.
Some comments:
1)What are the x,y variables for? x,y would look like a position in a 2D graph not part of a search for a max value. Can they be renamed to reflect the values they hold? oldMax, currMax, newMax ???
2) the code calls itself recursively but does NOT save and use the value returned ???
3) What happens to the values when the method returns 0?
4) Too many lines of debug print out. Merge printed data to fewer lines.
A smaller test file would make the debug output smaller and easier to analyze.
Re: find largest value in BST
Quote:
Some comments:
1)What are the x,y variables for? x,y would look like a position in a 2D graph not part of a search for a max value. Can they be renamed to reflect the values they hold? oldMax, currMax, newMax ???
Basically the x and y variables were just done for quickness when trying out my idea and checking what is going on. The names you suggest are essentially what they represent. I have tried to avoid using my actual variable names where everyone can see them.
Quote:
2) the code calls itself recursively but does NOT save and use the value returned ???
This is the main part of my difficulty. How to save the value of the maximum so far. But not only that, I don't understand why it returns the roots value at the end despite having a different value altogether just before the return statement is. My test showed that because the variables are initialised at the start they just default back to the initialised variable.
Quote:
3) What happens to the values when the method returns 0?
I found the only time it returns 0 is when null is called as an argument instead of root.
Quote:
4) Too many lines of debug print out. Merge printed data to fewer lines.
A smaller test file would make the debug output smaller and easier to analyze.
My apologies. It was intended at the time to make it easier to see what is going on. I can change it on the post with the code when I get online properly later.
Re: find largest value in BST
Quote:
How to save the value of the maximum
Save what the method returns in a variable.
Re: find largest value in BST
The method returns the root's attribute which is frustrating me. Also, to store it in a variable, the variable needs initialising and therefore it will just default to the initialised value when the method calls itself again. I am pretty much OK until it comes to recursion :-??
Re: find largest value in BST
You need to have the current max value available as you transit the tree so you can test if the current node's value is bigger.
Re: find largest value in BST
I have been trying to not use global variables which is the only other way I can see ... so far. I know I must be missing something that once I know it I will know forever, but I just cannot see it for trying.
I am under the impression that using global variables for this type of operation is not recommended.
Re: find largest value in BST
How about another arg for the method: currMax
Re: find largest value in BST
I will pick this up again tomorrow afternoon. I think I might just get it. I almost want to keep at it now, but I need to do other things for now.
To update, I have added a variable as an argument. I am not quite getting the desired result as that variable gets modified to a lower value a couple of times when going through the method. I hope to work it out. I think it has something to do with the stack.
Re: find largest value in BST
I have edited the code in post #3 to match my latest testing on it. I am a bit rushed so will mention more about this on my return home.
Basically the max value is wrongly adjusted to a lower value twice. Also the returned value is the one for the first node alphabetically (which is only there for testing purposes anyway).
I have to rush out but will check for replies while I am out so I can think of a solution ready to crack on with immediately on my return.
Re: find largest value in BST
Can you explain why there are three double args to the method? The code only needs to have the current max to compare against.
The method should ALWAYS return the max found so far. Never 0!!!
Re: find largest value in BST
I took out the unnecessary args. I kind of made the situation worse trying to find a way to get it right and more variables crept in that were not needed.
The only time it could have returned 0 was if the node was null and I have changed that to currentMax.
After currentMax is changed when it reads the value for "Cat" it should not go back to the value for "AddedForTestingCount". Also it doesn't get changed again afterwards at any time which I am struggling to understand.
Re: find largest value in BST
What does the debugging output show?
Why isn't the value returned by inorderHelper() used????
Re: find largest value in BST
The way I am seeing it is that the returned value is used but that it is incorrect because inorderHelper() is obviously not right yet and I still need to solve that.
Re: find largest value in BST
It needs to use the returned value.
Re: find largest value in BST
Solved it :)
Many thanks Norm for you patience and time. I have changed various values in the file to make different animals have the biggest value and it has returned the correct maximum every time since.
Re: find largest value in BST