What is the difference between iteration and recursion in data structure?
-
-
Answer:
Hi Iteration:- --------------- for (i=0;i<10;i++) . This loop will execute 10 times. So each execution of the loop is called an iteration. 1st iteration, 2nd iteration and so on till 10. Recursion:- ------------------- Whenever a function calls itself it is called as recursion ex: int factorial( int a) { ---- if(a=1) --------return 1; -----else ---------return a*factorial(a-1) } HAPPY CODING !! Sujit
Wisdom Learner at Yahoo! Answers Visit the source
Other answers
Let A and B be two symbols which delimit a sequence. If there are N instructions between A and B inclusive, and program flow starts with A and does not end until B is reached, we can say the following: If program flow returns to A before reaching B, and the next instruction which would have been executed is pushed onto the top of the stack, then recursion is taking place as the flow continues from A again. Furthermore, as soon as program flow reaches B, a recursion will terminate if there exists an address on top of the stack placed there by at least one preceding recursion. If program flow does not reach B at least once, recursion will not terminate normally and the program will fail. Iteration is a situation where reaching B does not result in resuming execution from some point the address of which was previously saved onto the stack during a recursion. (By this token, a call to a routine is one iteration, whether that routine recurses or not.) During recursion, a global variable could be used to control the depth of recursion, or data pushed onto some stack could provide a termination condition; and in the latter case this termination condition is usually related to the form of the data structure being traversed. Recursive traversals and tree structures are naturally related, just as iterative calculations and mathematical summations and products are naturally related. (Although many series possess recursion formulas, recursions are not generally factorizable and therefore are not often reducible to partial sums or products. Factorize a recursion rule and you wind up with other recursion rules.) It is often maintained that the generation of terms in certain sequences or series is most naturally expressed in the form of a recursion, and this is viewed as a kind of algorithmic structure which embodies the natural properties of the data (i.e., the terms of the series). I have even heard it said that recursion is in itself a more "fitting" method of calculation than iteration. This is complete balderdash and nothing but mathematical superstition, but some people hold it up as a watchword and as the epitome of computational sophistication. In any event, there is obviously structuring of data in a recursion which differs from the form it would have in an iteration; but the real difference lies in the concept of the stack (and it is well known that any recursive algorithm can be replaced with an iterative algorithm which uses an auxiliary stack for data).
Greater Meridian
Iteration is straight forward coding whereas recursion coding is using code over and over again to solve your answer. Here is an example of using recursion and iteration solving for factorials. for (int i=max; i>1; i--){ factorial = factorial * i; } An example of recursion is int factorial(int x){ if (x==1) return 1; else return x*(factorial(x-1)); }
Adiuvat
In a data structure iteration corresponds to arrays and recursion to trees. So if you store a list of things, then that would be an iterative data structure. If you have a data structure that represents a family tree, that hierarchy will be a recursive data structure.
peteams
Related Q & A:
- What web scraping tool is the best to extract data?Best solution by Quora
- What's the difference between Current (I) and Potential Difference (V?Best solution by diffen.com
- What does it mean to be unresponsive? What's the difference between unresponsive and unconscious?Best solution by answers.yahoo.com
- What are some phones for Verizon Wireless without a data plan or with a cheap data plan?Best solution by Yahoo! Answers
- What's the difference between a static data member and a regular data member?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.