![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 10:41 AM |
| Calculating the average | kekanap | Shell Programming and Scripting | 2 | 01-26-2007 06:36 AM |
| finding duplicate files by size and finding pattern matching and its count | jerome Sukumar | Shell Programming and Scripting | 2 | 12-01-2006 01:20 AM |
| How to find no of occurances Finding average time? | redlotus72 | UNIX Desktop for Dummies Questions & Answers | 1 | 02-21-2005 12:22 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Actually, it would be nice if posters would read our rules and follow them. This thread is closed.
|
||||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|