0
int Size(struct node* node) { if(node == NULL) { return 0; } else if(node != NULL) { return (Size(node->left) + 1 + Size(node->right)); } } 

Hi, can anybody please post the stack trace for the following piece of code.

Lets say if we insert the values 2, 1, 10, 5... Then what could be the stack representation during the recursion process.. Please, its very urgent, and its very confusing too...

5
  • 9
    You won't learn anything if we do your homework for you. Commented May 9, 2010 at 9:37
  • Well Sir, its not a HW. i am just trying to make a program to calculate the size of the tree.. How many elements are there in the Tree. And with some internet help, i found out this piece of code, which looks pretty easy, but i am trying to understand it, but little bit of fuzzy concepts in recursion.. Commented May 9, 2010 at 9:43
  • Plz if any body can make ths code, little bit understanding, thn i vl be highly obliged.. Thankx.. Commented May 9, 2010 at 9:44
  • aree with above. use debugger(your ide internal debugger or gdb), and read this en.wikipedia.org/wiki/Nested_set_model Commented May 9, 2010 at 9:44
  • Sir, i tried installing gdb on my Fedora machine.. But it throws some errors at the end during MAKE command.. i have posted one question on the same on stackoverflow,, but no replies.....stackoverflow.com/questions/2792912/…..... Commented May 9, 2010 at 9:49

2 Answers 2

2

Why not simply use printf? One when entering and one when leaving the function:

int Size(struct node* node) { printf("Enter %d\n", ( node ? node->value : -1 )); ... printf("Leave %d\n", ( node ? node->value : -1 )); } 
Sign up to request clarification or add additional context in comments.

3 Comments

This idea is pretty kool.. but where to put this "Leave" printf statement, in my code.. I want to understand the flow of the programm that i have posted.. plz little bit guidance..:-) Anyways Thankx for that idea..:-)
Put it before each return. You have to restructure the function a bit and use a temporary variable for the second return, of course. Since you're already working with recursion, I'm sure you know how to do this. And be prepared to answer the question what the ?: is doing when your tutor asks you about it. Or instead use ifs to check the NULL case.
Hmm thnx for the reply.. I am working in an IT Company, and just improving upong my data structure concepts,, so no point comes of tutor as such!! Anywayz thanx a lot for that stuff.. :-) Keep Rocking!!
0

Try using gdb and see the backtrace/bt command for gdb.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.