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.