![]() |
|
|
|
|
|||||||
| 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 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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... |
| Thread Tools | |
| Display Modes | |
|
|