Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 42 of 42

Thread: find largest value in BST

  1. #26
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: find largest value in BST

    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.

    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.

    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.

    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.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: find largest value in BST

    How to save the value of the maximum
    Save what the method returns in a variable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: find largest value in BST

    How about another arg for the method: currMax
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #35
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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!!!
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: find largest value in BST

    What does the debugging output show?

    Why isn't the value returned by inorderHelper() used????
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: find largest value in BST

    It needs to use the returned value.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Java Bean (December 10th, 2012)

  17. #41
    Junior Member
    Join Date
    Dec 2012
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  18. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: find largest value in BST

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  2. Dictionary using Distributed Hash Table and BST Tree
    By dezett in forum What's Wrong With My Code?
    Replies: 28
    Last Post: June 23rd, 2012, 12:03 PM
  3. Write a method that searches the BST for a given input
    By Jurgen in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 6th, 2012, 11:46 AM
  4. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM
  5. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM