Recursion Flashcards

1
Q

What is Recursion?

A

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Base Case?

A

the solution to base case is provided and solution of bigger problem is expressed in terms of smaller problems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why stack overflow occurs?

A

Each time a function is called, it’s removed from a call stack. The nature of a stack is LIFO. Before the call stack can begin calling its functions, it needs a starting point (base case) otherwise it will be building new stacks until infinity. It should instead stop the stacks from accumulating, and begin executing the functions and popping them off one at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between direct and indirect recursion?

A

Direct recursion happens when a function calls itself. Indirect recursion is when functions call another function which calls the original function.

void directRecFun()
{
    // Some code....
directRecFun();
    // Some code...
}
// An example of indirect recursion
void indirectRecFun1()
{
    // Some code...
indirectRecFun2();
    // Some code...
}
void indirectRecFun2()
{
    // Some code...
indirectRecFun1();
    // Some code...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between tailed and non tailed recursion

A

A recursive function is tail recursive when recursive call is the last thing executed by the function.

The tail recursive functions considered better than non tail recursive functions as tail-recursion can be optimized by compiler. The idea used by compilers to optimize tail-recursive functions is simple, since the recursive call is the last statement, there is nothing left to do in the current function, so saving the current function’s stack frame is of no use

An example of non tail is the factorial recursive function. The recursed data is used in the middle of the function, not the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is memory allocated to different function calls in recursion?

A

When any function is called from main(), the memory is allocated to it on stack. A recursive function calls itself, the memory for called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call. When the base case is reached, the function returns its value to the function by whom it is called and memory is de-allocated and the process continues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the disadvantages of recursive programming over iterative programming?

A

Note that both recursive and iterative programs have same problem solving powers, i.e., every recursive program can be written iteratively and vice versa is also true. Recursive program has greater space requirements than iterative program as all functions will remain in stack until base case is reached. It also has greater time requirements because of function calls and return overhead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the advantages of recursive programming over iterative programming?

A

Recursion provides a clean and simple way to write code. Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc. For such problems it is preferred to write recursive code. We can write such codes also iteratively with the help of stack data structure. For example refer Inorder Tree Traversal without Recursion, Iterative Tower of Hanoi.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly