The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to average in awk saint2006 Shell Programming and Scripting 4 06-04-2008 08:29 AM
average value su_in99 UNIX for Dummies Questions & Answers 2 03-10-2007 09:41 AM
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 05:36 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 12:20 AM
How to find no of occurances Finding average time? redlotus72 UNIX Desktop for Dummies Questions & Answers 1 02-21-2005 11:22 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-18-2004
Registered User
 

Join Date: Mar 2002
Location: Madison, WI
Posts: 5
Stumble this Post!
Finding Average for BST

Helo guys, this is not specifically a C programming problem for the unix platform, but a problem i came across in class:

Find the average for a binary search tree:


typedef Struct SNode
{
double value;
SNode *leftChild;
SNode *rightChild;
} as SNode;


/*
* Solution 1
*/

double GetBSTSum (SNode *root)
{
// Base case 1
if( !root )
return 0;

// Base case 2
if( root->leftChild == NULL && root->rightChild == NULL )
{
return root->value;
}

// Recursive Case
else
{
return root->value + GetSum(root->leftChild) + GetSum(root->rightChild);
}

}

int GetNumNodes (SNode *root)
{
// Base case 1
if( !root )
return 0;

// Base case 2
if( root->LeftChild == NULL && root->rightChild == NULL )
return 1;

// Recursive case
else
return 1 + GetNumNodes(root->leftChild) + GetNumNodes(root->rightChild);


}

double GetAvg (SNode *root)
{
double dTotal = GetBSTSum( root );
int iNumNodes = GetNumNodes( root );

return dTotal/iNumNOdes;
}



/*
* Solution 2
*/
void GetTotalAndCount (SNode *root, double & dSum, int & dCount)
{
if (!root)
return;
else
{
GetTotalAndCount (root->leftChild, dSum, dCount);
GetTotalAndCount (root->rightChild, dSum, dCount);

dSum += root->value;
dCount++;
}
}

double GetAvg (SNode *root)
{
double dTotal = 0;
int iNumNodes = 0;

GetTotalAndCount (root, dTotal, iNumNodes);

return dTotal/iNumNOdes;
}




Questions:

For solution 1, would it be better to use base case 1 or base case 2? (assuming of course that a leaf node will have null pointers for its children)

Is there any reason to choose solution 2 over solution 1 besides the fact that we only need to iterate through the tree once? Both solutions are O(n).

Any comments appreciated.
__________________
If only life is as simple as C...
Forum Sponsor
  #2 (permalink)  
Old 02-18-2004
Registered User
 

Join Date: Feb 2004
Location: Bangalore (India)
Posts: 1
Stumble this Post!
as you have said both have the complexity O(n)...
so any of these will do...
but it is better to write simple code.....
so that the next time you see it you know what it means....

and what does that Base Case 1 and Base Case 2 mean....
couldn't understand.....??
it would be nice if you could make it clear...
  #3 (permalink)  
Old 02-19-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
Actually, it would be nice if posters would read our rules and follow them. This thread is closed.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0