如何用堆栈和循环结构代替递归调用--递归转换为非递归的10条军规
10 Rules (steps) for replacing the recursive function with stack and while-loop
轉自http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and??
First rule
- void SomeFunc(int n, int &retVal);
- 細節參照第六條sixth rule.
Second rule
int stage; // - Since there is process needed to be done // after recursive call. (Sixth rule) }; // (Second rule) int retVal = 0; // initialize with default returning value ... // (Second rule) return retVal; }
Third rule
Fourth rule
Fifth rule
Sixth rule遞歸條件分類處理
Seventh rule
Eighth rule
Ninth rule
- 如果遞歸函數有返回值,如 "Eighth rule,"所述,把返回值保存到局部變量中 (如?retVal), 然后"continue"繼續循環;
- 多數情況下, "Ninth rule" 是可選的,但他有助于避免邏輯錯誤.
Tenth rule (and the last...)
Simple Examples by types of recursion??
- Please download?RecursiveToLoopSamples.zip
- Unzip the file.
- Open the project with Visual Studio.
- This project has been developed with Visual Studio 2008
- Sample project contains
- Linear Recursion Example
- Binary Recursion Example
- Tail Recursion Example
- Mutual Recursion Example
- Nested Recursion Example
More Practical Example Sources??
The below sources contain both a recursive version and a simulated version, where the simulated version has been derived using the above methodology.?
- epQuickSort.h
- epMergeSort.h
- epKAryHeap.h
- epPatriciaTree.h
Why do the sources contain both the simulated version and the recursive version???
If you look at the source, you can easily notice the simulated versions look much more complex than the recursive versions.?For those who don't know what the function does, it will be much harder to figure out what the function with the loop actually does.?So I prefer to keep both versions, so people can easily test out simple inputs and outputs with the recursive version, and for huge operations, use simulated version to avoid stack overflow.?
Conclusion???
My belief is that when writing C/C++ or Java code, the recursive functions MUST be used with care to avoid the stack-overflow error. However as you can see from the examples, in many cases, the recursive functions are easy to understand, and easy to write with the downside of "if the recursive function call's depth goes too deep, it leads to stack-overflow error". So conversion from recursive function to?simulated function is not for increasing readability nor increasing algorithmic performance, but it is simple way of evading the crashes or undefined behaviors/errors. As I stated above, I prefer to keep both recursive version and?simulated version in my code, so I can use the recursive version for readability and?maintenance of the code, and the simulated version for running and testing the code.??It will be your choice how to write your code as long as you know the pros and cons?for the choice, you are making.??
Reference???
- http://www.dreamincode.net/forums/topic/51296-types-of-recursion/
- EpLibrary 2.0?
History??
- 07.02.2015:- Broken link fixed
- 09.06.2013:- Typo fixed (Thanks to ?lovewubo)?
- 08.22.2013:- ?Re-distributed under MIT License from GPL v3?
- 08.10.2012: - Table of contents updated?
- 08.04.2012: - Moved the article's subsection to "Howto"?
- 07.23.2012: - Minor fixes on the article??
- 07.13.2012: - Table of contents modified?
- Sections removed
- Moved the article to Beginner section?
- Changed the wording?
- 07.13.2012: - Table of contents added.
- Titles modified.
- New sections added.
- Difference between Recursive and Iterative function
- Pros and Cons of Recursive and Iterative approach
- 07.12.2012: - Sample bugs fixed.
- Article re-organized.
- Ninth and Tenth rule added.
- Examples for each rule added.
- 07.11.2012: - Submitted the article.
License
This article, along with any associated source code and files, is licensed under?The MIT License
轉載于:https://www.cnblogs.com/baiyu/p/4625848.html
總結
以上是生活随笔為你收集整理的如何用堆栈和循环结构代替递归调用--递归转换为非递归的10条军规的全部內容,希望文章能夠幫你解決所遇到的問題。