In C I have a problem with BSTs printTree in this function

In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop?

Could you tell me how this sub-function(printTree) works? Thanks.

void printTree(Tree t, int depth) { if (t != NULL) { depth++; printTree(t->left, depth); int i; for (i-1: ivalue); printTree (t->right, depth);

Solution

This is a Recursive Call of Inorder procedure.

We know we will firrst traverse Till the Left Most Node, Then Print it and then Backtrack till we reach the Right Most Node.

In The given Example the Value of Depth is 4.

So initially we called printTree(Tree,depth)

Here since depth is 4 and tree is not null the depth value will be now 5 and we are calling the left most node of the root node. The same process will be carried out until we reach the left most node and the tree will be empty. The recursive calls are stored on a stack.

Now each entry will be popped out and the spacing will be provided by \\t and \ .

next it will print each element recursively

In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop? Could you

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site