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.

Results 1 to 10 of 10

Thread: Hello everybody nice to meet you all.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Hello everybody nice to meet you all.

    The main reason that i registered in this forum is to learn and communicate with other java programmers. This days I'm working on one assigment that has as purpose to create a Binary Search Tree that read a text files(which contains a String name and a double number) and implements some methods. Unfortunately i'm not very familiar with BST so I'm facing some difficulties. At the moment i want to write a method to search the BST to find and return the largest number. At the moment i'm using this code to do that but unfortunately i receive only the first number of the list and not the actual largest. Here is my method
    private double FindMaxElement(TreeNode maxElement)
        {
           double max = 0;
            if(maxElement != null)
            { 
               if(maxElement.number > max)
               {
                   max = maxElement.number;
                   FindMaxElement(maxElement.left);
                   FindMaxElement(maxElement.right); 
                }  
            }
           return max;
        }
    Can anyone help me or advice me with this method?
    Last edited by helloworld922; December 23rd, 2011 at 12:06 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hello everybody nice to meet you all.

    You must make sure to manage the returned values when you call FindMaxElement - currently you call the method but if the returned value is greater than max, you do not set max.

    For future reference, please don't post technical questions to the introduction forum (I've moved your post to a more appropriate forum), and please wrap your code in the code tags.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Jurgen (December 22nd, 2011)

  4. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hello everybody nice to meet you all.

    Thank you for the guidance, I'm new in the forum and a little confused at the moment

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hello everybody nice to meet you all.

    Quote Originally Posted by Jurgen View Post
    Thank you for the guidance, I'm new in the forum and a little confused at the moment
    You are welcome. Is your problem solved?

  6. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hello everybody nice to meet you all.

    Unfortunately not yet, I understood what was the problem but I can't figure out what to change in order to work properly, can you make an example so that I can see how to manage the return values so that to search all the values of the BST?

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hello everybody nice to meet you all.

    Take a step back and write down the logic of how the method as written will work. For instance, look at the line of code below

    FindMaxElement(maxElement.left);

    This method call will return the maximum value of the nodes in the tree below and to the left of the current node. To get the max of the tree, you must do something with this value (perhaps check if the value returned is greater than max?)

  8. The Following User Says Thank You to copeg For This Useful Post:

    Jurgen (December 23rd, 2011)

  9. #7
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hello everybody nice to meet you all.

    Sorry about the double post but i'm really nervous about this assignment. As i was telling, at the beginning i thought i found the solution by doing this :
    private double FindMaxElement(TreeNode maxElement)
        {
            double max = 0;
            if(maxElement.right == null)
            {
                return 0;
            }
            if(maxElement.right != null)
            {      
               if(maxElement.number > max)
               {
                  max = maxElement.number;
                  FindMaxElement(maxElement.right);
     
                  if(FindMaxElement(maxElement.right) > max)
                  {
                      max = FindMaxElement(maxElement.right);
                    }
               }
            }
            return max;

    But i found that the result that was returned was not actually the largest number but the second largest. At the beginning i didn't saw that fact but as i was investigating the file i saw the was wrong. The problem now is that i can't understand why this happend, it supposed to return the largest number from the right nodes, why i had other result? Do you have any advice or do you noticed something that i didn't?
    Last edited by Jurgen; December 24th, 2011 at 04:24 PM.

  10. #8
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hello everybody nice to meet you all.

    Can you make an evaluation of the method and tell me what causes me to not reach the largest number?

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello everybody nice to meet you all.

    Can you post a full program that compiles and executes and demonstrates the problem?

    One problem is that you do NOT save and use the value returned by the method. You call it but do nothing with its return value.

  12. #10
    Junior Member
    Join Date
    Dec 2011
    Location
    Greece
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hello everybody nice to meet you all.

    Well i finally solved the problem but thank you for your interest.

Similar Threads

  1. Need some nice sites to learn Java
    By java in forum Java Theory & Questions
    Replies: 32
    Last Post: April 5th, 2021, 10:46 AM
  2. Does this code meet the standards set forth?
    By Java Student in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2011, 05:42 PM
  3. hi, its nice to found this forum :D
    By ordinarypeople in forum Member Introductions
    Replies: 2
    Last Post: April 4th, 2010, 02:12 PM
  4. Replies: 0
    Last Post: September 2nd, 2009, 08:50 AM