Question

In: Computer Science

In what case(s) would you use recursion over iteration and which approach is more memory efficient...

In what case(s) would you use recursion over iteration and which approach is more memory efficient and why when you compare which approach would you say is faster and why?

Solutions

Expert Solution

One must use recursion for complex problems like the ones which can have many iterative loops like the tree traversal problem since a tree is basically a non uniform data structure and only the height of a tree is known and we dont have a clear idea about the number of nodes so traversing a tree can be hectic task if we use iteration like for and while loop however recursion makes the tree traversal easy by running a particular task until and unless the final condition is met a recursion basically divides a complex problems into different parts which makes the problem much easier to solve .

One must also use recursion where there is a repetative pattern like in fibonacci series which is type of series in which the third term is the sum of the other two and this type of program usse a dynamic approach as the variable changes in order to further calculate the fibonacci series.

Though recursion is not memory efficient as compared to iteration because recursion  stores the data in a stack and then back tracks it but it helps us to solve complex problems easily and iteration does not use much space so recursion in not memory efficient.

Recursion can be slower because every time it needs to allocate a new stack until and unless it is optimized it basiclly depends upon the kind of problem we approach for example in the case of tree traversal one needs to write many for loops to iterate through a tree while there is not much iteration in recursion and program can be written in simple manner howevere generally iteration is faster than recursion


Related Solutions

Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want to use iterative vs. recursive programming? Where does coding fit in concerning the software process (i.e. its use in design, in coding, etc.)? Describe recursion problems, and why does it seem to fit with recursion? (e.g. nature, math, music, sports game, etc.)  
1. What are the strengths and weaknesses of mercantilism and liberalism? Which would be more efficient...
1. What are the strengths and weaknesses of mercantilism and liberalism? Which would be more efficient in addressing global economic issues? 2. What are the reasons states impose protectionists policies on other countries? 3. What are the advantages and disadvantages of state ownership of the economy and private ownership? 4. How important is a stable international monetary system to cooperation among countries trade relations? Why would this be considered a collective good? 5. What are the factors that multinational corporations...
Which approach among the Optimistic, Conservative, and Minimax Regret approach would you like to use while...
Which approach among the Optimistic, Conservative, and Minimax Regret approach would you like to use while you are making a business decision? Please describe the decision problem first with at least 2 decision alternatives and at least 2 states of nature.
What unstated requirements are there, and what method(s) would you use to collect a more comprehensive set of requirements?
What unstated requirements are there, and what method(s) would you use to collect a more comprehensive set of requirements? Develop a Word document that describes all of the requirements and explains how they would be met using hardware, software, networking, and security. In other words, summarize the elements of the solution that are necessary to address the requirements. Support your proposal.The document should be APA-formatted.
"A key assumption is that crossing over is random. In which case, would you have an...
"A key assumption is that crossing over is random. In which case, would you have an equal number of recombinants?" Does this mean that each of the totals for the recombinant groups should all be relatively similar to each other if crossing over is actually random?
Suggest one way in which your Efficient frontiers could be made more efficient. What is the...
Suggest one way in which your Efficient frontiers could be made more efficient. What is the limitation of this suggestion?
What approach would you use to analyze a series/parallel AC circuit? How would you determine the...
What approach would you use to analyze a series/parallel AC circuit? How would you determine the magnitude and phase relationships for the voltages, currents, and powers in the circuit? Preferred typed answer please.
Under what circumstance(s) would you borrow money? What economic factors would lead to you borrowing more...
Under what circumstance(s) would you borrow money? What economic factors would lead to you borrowing more or less money?
In six sentences or more, explain how you would use the descriptive statistical procedure(s) at work...
In six sentences or more, explain how you would use the descriptive statistical procedure(s) at work or in your personal life.
Under what circumstances would it be more efficient for the circus owner, rather than the tamers...
Under what circumstances would it be more efficient for the circus owner, rather than the tamers themselves, to be liable for injuries to audience members? What is this principle called in Common Law?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT