binary trees-finding the maximum and min. value

This is a discussion on binary trees-finding the maximum and min. value within the C++ Programming forums, part of the General Programming Boards category; are the following coding correct for finding the maximum value of a binary tree and to find the minimum value ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    binary trees-finding the maximum and min. value

    are the following coding correct for finding the maximum value of a binary tree and to find the minimum value of a bin. tree??????

    to find the maximum value:
    int Tree::findMax(Node* localroot)
    {
    int max;
    if(localroot!=NULL)
    {
    findMax(localroot->leftChild);
    max=localroot->iData;
    findMax(localroot->rightChild);
    }
    return max;
    }//findMax
    -----------------------------------------------
    to find the minimum value:---

    int Tree::findMin(Node* localroot)
    {
    int arr;
    if(localroot!=NULL)
    {
    findMin(localroot->leftChild);
    arr=localroot->iData;
    return arr;
    }
    return -1;
    }//findMin

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off, use code tags when posting code.

    Second off, no the code is not correct. You aren't comparing values, you just always return the local value. You need to compare the values returned from the left branch and the right branch with the current value, and return the min or max of the three.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well this is how I would do these methods:
    Code:
    findMin( Node* t )
    {
    	if( t == NULL )
    		return NULL;
    	if( t->left == NULL )
    		return t;
    	return findMin( t->left);
    }
    
    findMax( Node* t )
    {
    	if( t != NULL )
    		while( t->right != NULL )
    			t = t->right;
    	return t;
    }
    edit:: I just read PJY's comments...my code is for a binary SEARCH tree....sorry misread the op.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    hmm...i'm a little confused now with the last coding that was posted to this thread
    ??
    i went through it now. It does work for the findMin(to find the minimum) right??

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    in my coding i'm not comparing the values b'coz im using part of the 'Inorder' traversing coding. So i thought because of that i'll naturally be getting the Minimum and the maximum values in the way that i've given my coding.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    sachitha, the code I posted works on a binary search tree, which follows certain rules that makes the findMin() and findMax() a bit easier than in a common binary tree. If your tree is just a binary tree, you need some sort of a comparison system, if it is a search tree, my code will work.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    oophs...i was referring to a 'Binary search tree' indeed. Thank you for the help! And sorry if i have confused anyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 12:39 PM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 11:38 AM
  3. min and maximum number
    By manzoor in forum C++ Programming
    Replies: 7
    Last Post: 06-28-2008, 04:50 PM
  4. Maximum And Minimum
    By drdodirty2002 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 01:39 AM
  5. Finding MAX or MIN within a string
    By DBA1 in forum C Programming
    Replies: 10
    Last Post: 02-27-2004, 08:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21