Proyecto
Proyecto
For Task.
A recursive function task is a function that calls itself to solve a
smaller instance of the same problem. It consists of a base case and a
recursive case to stop the recursion.
Basic Structure of a Recursive Function
2 Function Call
a function call refers to the instance when the function invokes
itself with a new set of parameters. This self-invocation is what
drives the recursion, allowing the function to break down the
problem into smaller, more manageable parts. Here’s a breakdown
of the key components:
Termination
3
Termination in recursive functions refers to the condition that ends
the recursion, preventing it from continuing indefinitely. This is
also known as the base case or base condition. It’s a crucial part of
a recursive function because it defines the point at which the
function should stop calling itself and begin to return values
Base Case and Recursive Case
1 Base Case
The condition that stops the recursive calls.
2 Recursive Case
The part that calls the function within itself.
3 Return Value
What the function returns when the base case is met.
1 Base Case
Every recursive function must have a base case. The base case is the
simplest scenario that does not require further recursion. This is a
termination condition that prevents the function from calling itself
indefinitely. Without a proper base case, a recursive function can
lead to infinite recursion.
2 Recursive Case
In the recursive case, the function calls itself with the modified
arguments. This is the essence of recursion – solving a larger
problem by breaking it down into smaller instances of the same
problem. The recursive case should move closer to the base case
with each iteration.
3 Return Value
This return value is often a simple, known result that can be used to
calculate the results of all the subsequent recursive calls.
Enter
RECURSION TREE
EXAMPLE
The function starts with the initial call nSum(5), where 5 is the
number you want to sum down to 0.
The factorial of a number is the product of all positive integers up to that number. For a
non-negative integer ( n ), the factorial is denoted by ( n! ) and is calculated as:
Factorial n!=n×(n−1)×(n−2)×…×2×1
Calculation For ( n = 0 ), the factorial is defined as ( 1 ).
In recursive functions, the factorial of a number can be calculated by defining a function
that calls itself with a decremented value until it reaches the base case.
return 0;
}
Tree Traversal
It shows a binary tree with nodes labeled from 1 to 7 and lists the
sequences of nodes visited during inorder, preorder, and postorder
traversals.
• Inorder Traversal: Visits the left subtree, the node, and then the right
subtree. The sequence is 4, 2, 5, 1, 6, 3, 7.
• Preorder Traversal: Visits the node first, then the left subtree, and
finally the right subtree. The sequence is 1, 2, 4, 5, 3, 6, 7.
• Postorder Traversal: Visits the left subtree, the right subtree, and
then the node. The sequence is 4, 5, 2, 6, 7, 3, 1.
#include<iostream> Factorial
using namespace std; Calculation
int factorial(int n);
int main() { • The main() function starts by prompting the user to enter a positive integer
and stores the input in the variable n.
int n; • It then calls the factorial() function, passing n as an argument.
• The factorial() function is defined to call itself recursively. Here’s the
cout << "Enter a positive integer: "; breakdown:
cin >> n; • If n is greater than 1, the function returns n multiplied by the factorial of n -
1. This is the recursive call.
cout << "Factorial of " << n << " = " << • If n is 1 or less, the function returns 1, which is the base case for the
factorial(n); recursion.
The recursion continues until it reaches the base case, where n is 1. At this point,
return 0; the function returns 1, and the recursion starts to unwind, multiplying all the
} numbers from 1 up to the original value of n.
int factorial(int n) {
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Maze Solving
Optimization
Can be optimized by the compiler to avoid growing the call stack.
Efficiency
Reduces memory consumption and can make recursive functions as
efficient as iterative solutions.
Best Practices for Using Recursive
Functions
Test Extensively Optimize Iteratively
Employ comprehensive testing to Consider iterative approaches if
ensure correctness and prevent recursion leads to performance
infinite recursion. issues.
Document Clearly
Thoroughly document the base case, recursive case, and the expected output.