-
C++ Binary Tree Maximum Value function
Hello -
I cannot figure out why this binary tree max function will not work correctly, when i try to run this function the program crashes 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 10:53 AM.
-
You should re-write this thing.
It looks like, from a tired person's eyes, that the thing iterates over all the guys but replaces with NULL on every leaf!!
Its doing a traversal that looks good in the else area, but EVERY time you hit a null pointer, you lose the value of max and replace it with null. Oops??
Last edited by jonnin; 12-05-2005 at 05:14 PM.
-
I have done a similar program but not with templates. I used Linked lists to implement my binary Tree.. Anyway you may get some tips from this? It can be done recursively..
Code:
getRightmostData()
{ if(right == null)
return data;
else
return right.getRightmostData(); }
I did this Java and just tried to roughly convert it over for you.. :)
-
His is recursive too.
and that doesnt look correct either? The if/else seems bad, won't this just return
the deepest data (his list is not in order remember)?
It reads:
if your on the deepest one,
return data (how do you know this ones the largest)
else
go deeper //you dont check these values if right is not null, which it isnt on the way back up the stack.
-
Yes you are correct, I didnt realise his list was not ordered..
My apologies for not reading the whole thread!
Last edited by Code_Nerd; 12-05-2005 at 07:00 PM.
Similar Threads
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
By Marcos in forum VB Classic
Replies: 3
Last Post: 01-25-2006, 11:18 AM
-
By Scott in forum VB Classic
Replies: 12
Last Post: 12-21-2001, 04:21 PM
-
Replies: 1
Last Post: 11-27-2001, 06:53 AM
-
By Kunal Sharma in forum VB Classic
Replies: 2
Last Post: 04-25-2000, 03:45 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks