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 5 of 5

Thread: How to find the larget number on binary search tree

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

    Default How to find the larget number on binary search tree

    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 Freaky Chris; December 23rd, 2011 at 10:05 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to find the larget number on binary search tree

    There is alot wrong with this, for starters, max is always set to 0, so you are always comparing against 0, so chances are you number will be bigger than 0, unless its negative.

    Secondly, where do you stor the result of the subsequent calls to FindMaxElement?


    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

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

    Default Re: How to find the larget number on binary search tree

    Actually i made some changes, this time i point only on the right nodes but again i can't find the largest number instead the second most largest number appears! I'm not sure what the mistake is, what should i do with the max ?

    private double FindMaxElement(TreeNode maxElement)
        {
            double max = 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;
        }

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Location
    Land of ice and snow
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find the larget number on binary search tree

    Hi Jurgen,

    Your method in general is very good, what i would like to say is that you need to have a public method (i think that you probably have created one and you don't show it here in this thread) in order to link it with this private method that you created. Also, in your private method you have only used if statements for the recursive case and not for the base case so you needed also to have an if statement for the base case in the case where the maxElement.right == null so that it could return 0.

    Here is the code, i hope that it will work for you :
    ...

    Let me know how it performed to you. Have a nice day!
    Last edited by copeg; December 24th, 2011 at 11:23 AM. Reason: spoonfeeding

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: How to find the larget number on binary search tree

    @mar, please read the forum rules and the following
    The Problem with Spoon-feeding

    @Jurgen, please do not double post the same question. Your question appears to be answered in your other thread - lets please keep discussions in that thread.
    Hello everybody nice to meet you all.

Similar Threads

  1. Balanced Binary Search Tree
    By D3158 in forum Object Oriented Programming
    Replies: 1
    Last Post: June 24th, 2011, 10:14 AM
  2. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  3. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 04:58 AM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 01:23 AM