Sponsored Content
Full Discussion: Finding Average for BST
Top Forums Programming Finding Average for BST Post 47791 by heljy on Wednesday 18th of February 2004 06:47:21 PM
Old 02-18-2004
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.
 

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

How to find no of occurances Finding average time?

Hi, I have MyLog.log file, and it contains "*** response Time 150", I want to develop Unix script like , 1. extract all such occurances in the MyLog.log file and 2. compute the average time taken I am new to Unix, any one can give any idea/sample code for this? Thanks in advance. (1 Reply)
Discussion started by: redlotus72
1 Replies

2. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

3. UNIX for Dummies Questions & Answers

average value

If I have a file like this, could anyone please guide me how to find the average value in each metrix. The file has got about 130,000 metrixs. Grid-ref= 142, 235 178 182 203 240 273 295 289 293 283 262 201 176 167 187 187 246 260 282 299 312 293 276 230 191 169 ... (2 Replies)
Discussion started by: su_in99
2 Replies

4. HP-UX

change time mode from BST to GMT

I want to know how to change the time zone from BST to GMT avoid the daylight savings in hp-ux (3 Replies)
Discussion started by: tomjones
3 Replies

5. Shell Programming and Scripting

count average

Hi Friends, Can any one help me with count average of student marks in this file (i can not change structure of the input file): input file: 1:John Smith:2 3 4 5 2:Mark Anderson:3 2 3:Susan Waterman:2 4 2 (numbers of marks are different) output: Name:John Smith ID#: 1 Avg. mark:... (6 Replies)
Discussion started by: mleplawy
6 Replies

6. Solaris

changing time zone from ist to bst

Hi, Can anybody tell me how to change time zone from ist to bst, What changes should be done in /etc/TIMEZONE file. wheather it is possible to change timezone without rebooting the server. Regards Manoj (1 Reply)
Discussion started by: manoj.solaris
1 Replies

7. Shell Programming and Scripting

Perl- Finding average "frequency" of occurrence of duplicate lines

Hello, I am working with a perl script that tries to find the average "frequency" in which lines are duplicated. So far I've only managed to find the way to count how many times the lines are repeated, the code is as follows: perl -ae' my $filename= $ENV{'i'}; open (FILE, "$filename") or... (10 Replies)
Discussion started by: acsg
10 Replies

8. Shell Programming and Scripting

shell script for finding average runtime of other script

so I've made a shell script that downloads 6 files in succession from a given url, then deletes them. Now I want to time the script, and the average time it uses by running it ~100 times. My problem is tho, how do I store the time it takes for each run through of the script? I know time writes to... (3 Replies)
Discussion started by: navlelo
3 Replies

9. Shell Programming and Scripting

Finding minimum maximum and average

I am trying to find the minimum maximum and average from one file which has values Received message from https://www.demandmatrix.net/app/dm/xml] in milliseconds. Received message from https://www.demandmatrix.net/app/dm/xml] in milliseconds. Received message from... (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

10. Shell Programming and Scripting

Finding an average

Basically, I need to find average of numbers which are given like: sh average file1 file (in files can be more than one number) ->10 sh average 5 7 ->6 sh average /users/file ->5 echo 5 7 | sh average 6 So basically i wrote my code but it gives me error... I am pretty sure it has to work... (10 Replies)
Discussion started by: Manu1234567
10 Replies
SQRT(3) 						   BSD Library Functions Manual 						   SQRT(3)

NAME
cbrt, cbrtf, cbrtl, sqrt, sqrtf, sqrtl -- cube root and square root functions LIBRARY
Math Library (libm, -lm) SYNOPSIS
#include <math.h> double cbrt(double x); float cbrtf(float x); long double cbrtl(long double x); double sqrt(double x); float sqrtf(float x); long double sqrtl(long double x); DESCRIPTION
The cbrt(), cbrtf(), and cbrtl() functions compute the cube root of x. The sqrt(), sqrtf(), and sqrtl() functions compute the non-negative square root of x. RETURN VALUES
The cbrt(), cbrtf(), and cbrtl() functions return the requested cube root. The sqrt(), sqrtf(), and sqrtl() functions return the requested square root unless an error occurs. An attempt to take the sqrt() of negative x raises an invalid exception and causes an NaN to be returned (except that the square root of -0 is valid and equal to -0.) SEE ALSO
fenv(3), math(3) STANDARDS
The cbrt(), cbrtf(), cbrtl(), sqrt(), sqrtf(), and sqrtl() functions conform to ISO/IEC 9899:1999 (``ISO C99''). HISTORY
The cbrt() function appeared in 4.3BSD. The sqrtl() function appeared in FreeBSD 8.0. The cbrtl() function appeared in FreeBSD 9.0. BSD
March 5, 2011 BSD
All times are GMT -4. The time now is 09:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy