Thread: C++ Binary Tree Maximum Value function

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Cool C++ Binary Tree Maximum Value function

    Hello -

    I cannot figure out why this binary tree max function will not work correctly. Note: This is to be done without having the binary tree ordered in anyway



    Code:
    template <typename T>	
    tnode<T> *max(tnode<T> *t)
    {
    	
    	// we will return maxValPtr
    	tnode<T> *maxValPtr,*leftMax, *rightMax;
    
    	
    	if (t == NULL)
    		maxValPtr=NULL;
    		
    	else
    	{
    		// temporarily assume t is pointing to the maximum
    		maxValPtr=t;
    		// leftMax points to the maximum value on the left subtree
    		leftMax=max(t->left);
    
    		// if leftMax is not NULL and the value to which it points
    		// is larger, change maxValPtr
    		if(leftMax!=NULL&&leftMax->nodeValue>maxValPtr->nodeValue)
    		{
    			maxValPtr=leftMax;
    		}
    
    		// rightMax points to the maximum value on the right subtree
    		rightMax=max(t->right);
    		// if rightMax is not NULL and the value to which it points
    		// is larger, change maxValPtr
    		if(rightMax!=NULL&&rightMax->nodeValue>maxValPtr->nodeValue)
    		{
    			maxValPtr=rightMax;
    		}
    		
    			
    	}
    
    		return maxValPtr;
    
    	// return a maxValPtr points to the node with maximum value for the tree
    	
    }



    This is the full code for the whole thing if you want to look at it:
    http://studentweb.uwstout.edu/clouti...rytreemax.html

    Any help would be appreciated.
    Last edited by krizam; 12-05-2005 at 08:19 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You didn't say why you think it's not working correctly. What's going wrong? A crash? Wrong output?

    I noticed that you never initialize maxValPtr and then you use it. That should make it crash, are you sure you don't have this backwards:
    Code:
    // temporarily assume t is pointing to the maximum

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    It doesnt work because the method runs forever, it never stops..and I am not real sure why, I will try what you suggested.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Now I did what you suggested, and it crashes. Any other tips?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    nevermind...i got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM