0% found this document useful (0 votes)
5 views4 pages

Ds Module2

Recursion is a programming technique where a function calls itself, simplifying code and enhancing readability, with examples including the Towers of Hanoi and tree traversals. It requires a base condition to prevent infinite loops and is useful for solving problems with repeated operations on smaller inputs. The document also provides code examples for calculating factorial and solving the Towers of Hanoi puzzle.

Uploaded by

gowdamohitha29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Ds Module2

Recursion is a programming technique where a function calls itself, simplifying code and enhancing readability, with examples including the Towers of Hanoi and tree traversals. It requires a base condition to prevent infinite loops and is useful for solving problems with repeated operations on smaller inputs. The document also provides code examples for calculating factorial and solving the Towers of Hanoi puzzle.

Uploaded by

gowdamohitha29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Recursion:

The process in which a function calls itself directly or indirectly is called


recursion and the corresponding function is called a recursive function.
Examples of such problems are Towers of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
Need of Recursion
Recursion is an amazing technique with the help of which we can reduce the
length of our code and make it easier to read and write.
Properties of Recursion:
• Performing the same operations multiple times with different inputs.
• In every step, we try smaller inputs to make the problem smaller.
• Base condition is needed to stop the recursion otherwise infinite loop will
occur.
Recursive program to find factorial of a given number:
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}

Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C)
and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the
smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to
move the entire stack to another rod (here considered C), obeying the following simple rules:
• Only one disk can be moved at a time.
• Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a
stack.
• No disk may be placed on top of a smaller disk.
Examples:
Input: 2
Output: Disk 1 moved from A to B
Disk 2 moved from A to C
Disk 1 moved from B to C
Input: 3
Output: Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C

TOWER OF HANOI
#include <stdio.h>
void towers(int num, char src, char dest, char aux) {
if (num == 1) {
printf("\nMove disk 1 from peg %c to peg %c", src, dest);
return;
}
towers(num - 1, src, aux, dest); // Move top num-1 disks to
auxiliary peg
printf("\nMove disk %d from peg %c to peg %c", num, src, dest);
// Move nth disk to destination peg
towers(num - 1, aux, dest, src); // Move num-1 disks from
auxiliary to destination peg
}
int main() {
int num;
printf("Enter the number of disks: ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi
are:\n");
towers(num, 'A', 'C', 'B'); // A = source, C = destination, B =
auxiliary
return 0;
}
def tower_of_hanoi(n, source, destination, auxiliary):
if n == 1:
print(f"Move disk 1 from {source} to {destination}")
return
# Move top n-1 disks from source to auxiliary
tower_of_hanoi(n - 1, source, auxiliary, destination)
# Move the nth disk from source to destination
print(f"Move disk {n} from {source} to {destination}")
# Move the n-1 disks from auxiliary to destination
tower_of_hanoi(n - 1, auxiliary, destination, source)

# Example usage for 3 disks:


n=3
tower_of_hanoi(n, 'A', 'C', 'B');

For Circular queue refer below link:

[Link]
queue#:~:text=A%20circular%20queue%20is%20the,forming%20a%
20circle%2Dlike%20structure.&text=The%20circular%20queue%20s
olves%20the,be%20non%2Dusable%20empty%20space.

For double ended queue refer below link:


[Link]

For priority queue refer below link:


[Link]

You might also like