Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jagratadeb committed Feb 4, 2025
1 parent 2d66f9a commit b454718
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"ostream": "cpp",
"iostream": "cpp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ using namespace std;

void fun(int n)
{
// If n is greater than 0, print n and call fun(n - 1)
// If n is greater than 0, call fun(n - 1) and print n
if (n > 0)
{
fun(n - 1); // tail recursion
fun(n - 1); // Head recursion: The function calls itself before executing any other statements
cout << n << endl;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void fun(int n)
if (n > 0)
{
cout << n << endl;
fun(n - 1); // tail recursion
fun(n - 1); // Tail recursion: The function calls itself as the last action in its code block
}
}

Expand All @@ -18,6 +18,5 @@ int main()
cout << "Output:" << endl;
fun(n); // Call the function with n = 3


return 0;
}
24 changes: 24 additions & 0 deletions Basic Concept/7 Recursion/2 Types of Recursion/treeRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream> // Include the standard input-output stream library
using namespace std; // Use the standard namespace

// Function definition for 'fun' which performs tree recursion
void fun(int n)
{
if (n > 0) // Base case: when n is greater than 0
{
cout << n; // Print the current value of n
fun(n - 1); // Recursive call to 'fun' with n-1
fun(n - 1); // Another recursive call to 'fun' with n-1
}
}

int main()
{
int n = 3; // Initialize n with a value of 3
fun(n); // Call the 'fun' function with n
cout << "\nThis is an example of Tree Recursion" << endl; // Print a message
cout << "Output:" << endl; // Print a label for the output
return 0; // End of the main function
}

// Output: 3211211

0 comments on commit b454718

Please sign in to comment.