Stack
Stack
void funcB() {
// Base condition
if (condition) return;
funcA(); // funcB calls funcA
}
Recursion
• 3. Tail Recursion
• The recursive call is the last operation in the function. After the
call, nothing is done.
• Example:
void tailRecursion(int n) {
if (n == 0) return;
tailRecursion(n - 1); // Recursive call is the last statement
}
Recursion
• 4. Non-Tail Recursion
• The recursive call is not the last operation, and some computation
happens after the recursive call.
• Example
int nonTailRecursion(int n) {
if (n == 0) return 1;
return n * nonTailRecursion(n - 1); // Further computation
after the recursive call
}
Recursion
• 5. Tree Recursion
• The function makes more than one recursive call in its body, leading
to a tree-like structure of function calls.
• Example:
• int treeRecursion(int n) {
• if (n <= 1) return n;
• return treeRecursion(n - 1) + treeRecursion(n - 2); // Multiple
recursive calls
• }
Recursion
• 6. Head Recursion
• The recursive call is the first statement in the function, and any
computation happens after the recursive call.
• Example:
void headRecursion(int n) {
if (n == 0) return;
headRecursion(n - 1); // Recursive call is the first statement
// Some computation after the recursive call
}