What is recursion ?
It is a way we break a big problem into multiple sub problems to find the the solution. As it is a function that calls itself either directly or indirectly therefore it is called recursion.
Things to remember
- Think of base case with the notion that you have to break the problem into one or more smaller problems, and add one or more base conditions that stop recursion
def factorial(n): if n <= 1: return 1 # This is base case else: return n * factorial(n-1)
- What are the disadvantages of recursive programming over iterative programming?
- 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.
- What are the advantages of recursive programming over iterative programming?
Recursion provides a clean and simple way to write code.
- Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc.
- They are simple to design.
Famous Examples:
- Factorial
- Fibonacci
- Recursive Selection sort
- Recursive Bubble Sort
- Recursive Insertion Sort
- Given a string, print all possible palindromic partitions
- Check if a number is Palindrome
- Print all possible strings of length k that can be formed from a set of n characters
- Find all even length binary sequences with same sum of first and second half bits
- Print all possible expressions that evaluate to a target
- String with additive sequence
- Generate all binary strings without consecutive 1’s
- Recursive solution to count substrings with same first and last characters
- All possible binary numbers of length n with equal sum in both halves
- Combinations in a String of Digits
- Count consonants in a string (Iterative and recursive methods)
- Program for length of a string using recursion
- First uppercase letter in a string (Iterative and Recursive)
- Partition given string in such manner that i’th substring is sum of (i-1)’th and (i-2)’th substring
- Power Set in Lexicographic order
- Function to copy string (Iterative and Recursive)
- Print all possible combinations of r elements in a given array of size n
- Print all increasing sequences of length k from first n natural numbers
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Program to find the minimum (or maximum) element of an array
- Sum triangle from array
- Reverse a stack using recursion
- Sort a stack using recursion
- Recursive function to delete k-th node from linked list
- Recursive insertion and traversal linked list
- Reverse a Doubly linked list using recursion
- Delete a linked list using recursion
- Print alternate nodes of a linked list using recursion
- Recursive approach for alternating split of Linked List
- Find middle of singly linked list Recursively
- Print all leaf nodes of a Binary Tree from left to right
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Print all longest common sub-sequences in lexicographical order
- Recursive Tower of Hanoi using 4 pegs / rods
- N Queen
There are alot of such problems that can be addressed using recursion, but the above ones are just a gist to understand the scope of recursion. In the following series in the articles, we will develop a capability to adress some complex problems.