| | |
Recursion: when do you use it?
![]() |
Hi ladies and gents,
I made this exercise in wich recursion has to be used and it worked as it should:
Now, I read the tutorial Dany made about this and she mentions: Wich algorithms are they, can someone give me some examples 
I mean, when do you really know you HAVE TO USE recursion
I made this exercise in wich recursion has to be used and it worked as it should:
C++ Syntax (Toggle Plain Text)
void ggd(short x, short y) { short a = 0; if (y == 0) { cout<<"The largest common divider is: "<< x <<endl; } else { a = (x% y); x = y; y = a; x% a != 0 ? ggd(x, y) :cout<<"The largest common divider is: "<< a <<endl; } } int main() { ggd(1147, 851); cin.get(); return 0; }
Now, I read the tutorial Dany made about this and she mentions:
•
•
•
•
Although recursion should generally be avoided, there are still algorithms which can only be solved with its use.

I mean, when do you really know you HAVE TO USE recursion
My experience in the world of programming is very short. However, I think every recursive function can be transformed into an iterative version. But often recursive functions are easily understood(not for all though
) and makes the code shorter. So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution. One thing to remember is that whether the choice is better or not depends on the problem at hand.
) and makes the code shorter. So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution. One thing to remember is that whether the choice is better or not depends on the problem at hand. •
•
•
•
Originally Posted by Asif_NSU
My experience in the world of programming is very short. However, I think every recursive function can be transformed into an iterative version.
•
•
•
•
But often recursive functions are easily understood(not for all though) and makes the code shorter.
•
•
•
•
So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution.
•
•
•
•
One thing to remember is that whether the choice is better or not depends on the problem at hand.
>when do you know it's better to use RECURSION
Most of the time, binary search trees are better written with recursive algorithms. Sure, it may be a fun exercise to do them iteratively, but anything but a simple binary search tree will be dreadfully complex without recursion. I haven't met many people who are up to the challenge of writing, say, a fully featured AVL tree without recursion.
So when is it better to use recursion? It's better when you can guarantee two things:
1) Each recursive step breaks down the problem into a smaller problem of the same type.
2) Each recursive step reduces the problem significantly.
The first is a general rule of recursion. If each step doesn't break the problem down into a smaller problem of the same type, it's harder to write a recursive function and guarantee that it will terminate. The second is my personal guideline. I generally won't write a recursive function unless it divides the problem in half with each step. This way I can verify with relative ease that the algorithm will be efficient.
>when do you really know you HAVE TO USE recursion
There are no situations where you have no choice but to use recursion. Any recursive algorithm can be converted to an interative algorithm through the use of a stack. The only question is how much work it takes to do this.
Most of the time, binary search trees are better written with recursive algorithms. Sure, it may be a fun exercise to do them iteratively, but anything but a simple binary search tree will be dreadfully complex without recursion. I haven't met many people who are up to the challenge of writing, say, a fully featured AVL tree without recursion.
So when is it better to use recursion? It's better when you can guarantee two things:
1) Each recursive step breaks down the problem into a smaller problem of the same type.
2) Each recursive step reduces the problem significantly.
The first is a general rule of recursion. If each step doesn't break the problem down into a smaller problem of the same type, it's harder to write a recursive function and guarantee that it will terminate. The second is my personal guideline. I generally won't write a recursive function unless it divides the problem in half with each step. This way I can verify with relative ease that the algorithm will be efficient.
>when do you really know you HAVE TO USE recursion
There are no situations where you have no choice but to use recursion. Any recursive algorithm can be converted to an interative algorithm through the use of a stack. The only question is how much work it takes to do this.
I'm here to prove you wrong.
>could you explain what a binary search tree actually is
I can, and I have.
I can, and I have.
I'm here to prove you wrong.
![]() |
Similar Threads
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C++ Forum
- Previous Thread: Help with a 5-way balanced sort merge
- Next Thread: Accessing a mouse button
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






