Tries Part 2 :- There does not seem to be any base statement. Will it not run endlessly

Here display function is being called recursively, but there does not seem to be any base case here. In if block inside display function there should be return statement somewhere for backtracking.

I am not able to understand how is it working. Pls explain me

As you can see , display function is called recursively from inside for loop.
So the branch factor(no of children in recursion tree) depends on how many times loop runs.
No of times loop run depends on number of children of node.
When we traverse the tree and reach the leaf node, the recursion automatically stops(your base condition), because leaf node has no child and thus loop will not execute and there will be no call to display function.
Try to dry run the algo through a simple example, you will understand.

Thanks.

Yes the recursion stops after reaching the leaf node. But then how is backtracking happening.Backtracking cant happen without the return statement.

First you need to know that return statement and backtracking are not related. They are altogether different concepts.
Eg. void main(){


return;
}
This return statement can be avoided here as it has no significance.

Now see void main(){
int x =10;
}

Even this function has no return statement, but after executing its body, function will automatically return.

For backtracking function should not call itself infinitely again and again.
It must stop somewhere calling itself again.
And to stop it, one don’t need return statement. Only make sure that the function should not be called again which happens in leaf node. Once whole body executed, it automatically returns

Thanks a lot. Now you have cleared my confusion.

Your welcome. Please rate, as user rating is important to us.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.