Recursion,Types of Recursion,Advantages and Disadvantages of Recursion,Example Programs of Recursion

When a function calls itself until terminating condition executed then this type of function calling is called Recursion. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series,Tower of Hanoi etc.
We have to follow two rules for making a successful recursion process:-
जब एक फंक्शन टर्मिनेटिंग कंडीशन के आने तक स्वयं को कॉल करता रहता है तब इस प्रकार की फंक्शन कालिंग को रिकर्सन कहा जाता है। रिकर्सिव फंक्शन के द्वारा कई गणितीय समस्याओ को हल किया जाता है जैसे फ़ैक्टोरियल, फिबोनाक्की सीरीज, टावर ऑफ़ हनोई इत्यादि। 
एक सफल रिकर्सन प्रोसेस तैयार करने के लिए हमे निम्न दो नियमो का पालन करना अनिवार्य है -

1.) Terminating condition :- This is a statement or condition which will stop the recursive call, must be presented in a function otherwise function may enter inside infinite loop. 

2.) Recursive call :- one or more statements which call function recursively, must be presented in a function.

1.) टर्मिनेटिंग कंडीशन:- यह एक विशेष स्टेटमेंट या कंडीशन होती है, जो रिकर्सिव कॉल को तत्काल समाप्त कर देती है इसका फंक्शन में होना अनिवार्य है अन्यथा फंक्शन इनफिनिट लूप में जा सकता है। 

2.) रिकर्सिव कॉल :- एक या अधिक स्टेटमेंट जो फंक्शन को बार-बार कॉल करते रहते है इनका फंक्शन में होना अनिवार्य है।  

Syntax:-
void func()
{
    ... .. ...
    func();
    ... .. ...
}
int main()
{
    ... .. ...
    func();
    ... .. ...
}

Example Code:-

long factorial(long f){
if(f==0) return 1;
return f*factorial(f-1);
}

Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Types of Recursion:-
रिकर्सन के प्रकार:-
 

1. Tail recursion:- when a recursive call is the last statement of function and there is no more statement(s) left to execute then it is called tail recursion.
1. टेल रिकर्सन:- जब रिकर्सिव कॉल फंक्शन का अंतिम स्टेटमेंट हो अर्थात कोई स्टेटमेंट रन होना शेष ना हो तब इसे टेल रिकर्सन कहा जाता है। 

2. Non-tail recursion :- when a recursive call is not the last statement of function and there is one or more statements left to execute then it is called non-tail recursion.
2. नॉन-टेल रिकर्सन:- जब रिकर्सिव कॉल फंक्शन का अंतिम स्टेटमेंट न हो अर्थात कोई स्टेटमेंट रन होना शेष हो तब इसे नॉन-टेल रिकर्सन कहा जाता है। 

3. Direct recursion :- when recursive call will directly call function then it is called direct recursion. It means a function has recursive call to itself.
3. डायरेक्ट रिकर्सन:- जब रिकर्सिव कॉल स्वयं को प्रत्यक्ष रूप से कॉल करती है तब इसे डायरेक्ट रिकर्सन कहा जाता है।  

4. Indirect recursion:- when a function (F1) has recursive call of function(F2) then function (F2) has recursive call of function (F1) then it is called indirect recursion.It means a function has no recursive call to itself.
4. इनडायरेक्ट रिकर्सन:- जब फंक्शन F1 की रिकर्सिव कॉल फंक्शन F2 को कॉल करती है एवं फंक्शन F2 की रिकर्सिव कॉल फंक्शन F1 कॉल करती है तब इसे इनडायरेक्ट रिकर्सन कहा जाता है अर्थात फंक्शन के पास स्वयं का रिकर्सिव कॉल नहीं है।   

5. Simple recursion :- if there is only one function calling statement exist in a recursive call then it is called simple recursion.
5. सिंपल रिकर्सन:- जब रिकर्सिव कॉल में केवल एक फंक्शन कालिंग स्टेटमेंट हो तब इसे सिंपल रिकर्सन कहा जाता है। 

6. Tree recursion :- if there are more than one function calling statements exist in recursive call then it is called tree recursion.
6. ट्री रिकर्सन:- जब रिकर्सिव कॉल में एक से अधिक फंक्शन कालिंग स्टेटमेंट हो तब इसे ट्री रिकर्सन कहा जाता है।

Advantages of Recursion:-
रिकर्सन के लाभ :-
1. It makes program elegant and cleaner.
1. यह प्रोग्राम को विशिष्ट एवं स्पष्ट बनता है। 
2. All algorithms can be defined recursively which makes it easier to visualize and prove. 
2. सभी अल्गोरिथम को रिकर्सिव बनाया जा सकता है जो इसे सरल एवं समझने में आसान बनता है।  

Disadvantages of Recursion:-
रिकर्सन की हानियाँ:-
1. If the speed of the program is very important then, we should avoid using recursion. 
1. यदि प्रोग्राम की गति अतिमहत्वपूर्ण है तब रिकर्सन का प्रयोग नहीं करना चाहिए।
2. It uses more memory and are generally slow.
2. यह अत्यधिक मेमोरी का प्रयोग करता है एवं धीमी गति से कार्य करता है।  

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...