Friday, July 31, 2009

I desperately need help with a C++ program! I need some intelligent advice!?

I'm writing a c++ function to sort through a BST with the purpose of determining its stem of greatest height, solely through the use of recursion, and given that the only variable in the function is a nodal pointer, the only way to pass any instances of information back is to return the desired integral value since this is an "int" type recursive function


I need 2 integers to make it work; i only get to return 1. i'm trying to think of a new way to do it with just 1.





i cannot pass anything down...


the only variable that the function can pass is the nodal pointer


%26amp; thus the only way to "pass" the height is to send it back up


If i was allowed to pass down, yeah, it'd be cake but it won't let me





I had to determine the "height", or the longest stem, the one with the most terms in it, going from the root up.

I desperately need help with a C++ program! I need some intelligent advice!?
I think this should work





int treeheight(tree *t) {


//base case


if((t-%26gt;left == null) %26amp;%26amp; (t-%26gt;right == null))


return 1;


int h1, h2;


if(t-%26gt;left != null) {


h1 = treeheight(t-%26gt;left);


}


if(t-%26gt;right != null) {


h2 = treeheight(t-%26gt;right);


}


if(h1 %26gt; h2) {


return h1 + 1;


}


else {


return h2 + 1;


}





}
Reply:check out http://www.pscode.com - it's the best source code site around. I don't do C++, but I use that site all the time.


No comments:

Post a Comment