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
- 定义一个新的数据结构"
Snapshot
".他的作用是保存队规过程中的中间值数据和状态信息。 - "
Snapshot
" 结构中包含:- 递归函数的参数,但是,如果递归函数的参数是引用类型的参数,则无需放到
Snapshot
中. 因此, 实例如下, 参数n
应该放在Snapshot
中,而引用类型的参数retVal
不放Snapshot
中.void SomeFunc(int n, int &retVal);
- 递归的条件分类值 "
Stage
" (通常是一个int
值,可以放在 switch语句中,分别处理不同的情况)- 细节参照第六条sixth rule.
- 存储函数返回值的局部变量
- 递归函数的参数,但是,如果递归函数的参数是引用类型的参数,则无需放到
// Recursive Function "First rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input int test; // - local variable that will be used // after returning from the function call// - retIdx can be ignored since it is a reference. int stage; // - Since there is process needed to be done // after recursive call. (Sixth rule) };
...
}
Second rule
- 在函数顶层创建一个局部变量,用于存储最终结果(递归函数的返回值retVal = currentSnapshot.test)。
- 在迭代过程中,它象一个临时变量保存每一次递归调用的返回值.
- 如果递归函数返回类型是空
void
, 忽略此步. - 如果有默认的返回值,用默认值初始化这个局部变量。
// Recursive Function "Second rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.// (Second rule返回值retVal = currentSnapshot.test)
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
- 建一个"
Snapshot
" 类型的堆栈.
// Recursive Function "Third rule" example
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
...
// (Second rule)
return retVal;
}
Fourth rule
- 创建 "
Snapshot
" 实例,并初始化输入到迭代中的参数和递归条件分类的初始值"Stage
" . - 把
Snapshot实例压栈
stack.
// Recursive Function "Fourth rule" example
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
...
// (Second rule)
return retVal;
}
Fifth rule
- 建一个
while循环,当
堆栈stack 不空时执行循环。 - 在
while
循环的每次迭代中, pop 出栈一个Snapshot
对象;
// Recursive Function "Fifth rule" example
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
...
}
// (Second rule)
return retVal;
}
Sixth rule递归条件分类处理
- 分2步处理 stages 。第一步对在当前递归函数调用之前的处理,第二步是在当前递归函数调用之后对返回值进行一些运算。
- 如果递归过程要调用2个函数, 就要对stages分3步处理:
- ** (Stage 1 --> recursive call --> (returned from first recursive call) Stage 2 (recursive call within stage 1)--> (return from second recursive call) Stage 3
- 如果有3个不同的递归调用,至少分4步骤处理 stages.
- 以此类推.
// Recursive Function "Sixth rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
// (Sixth rule)
switch( currentSnapshot.stage)
{
case 0:
... // before ( SomeFunc(n-1, retIdx); )
break;
case 1:
... // after ( SomeFunc(n-1, retIdx); )
break;
}
}
// (Second rule)
return retVal;
}
Seventh rule
- 根据不同的Switch 处理不同的Stage
- 做相关的处理
// Recursive Function "Seventh rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
// (Sixth rule)
switch( currentSnapshot.stage)
{
case 0:
// (Seventh rule)
if( currentSnapshot.n>0 )
{
...
}
...
break;
case 1:
// (Seventh rule)
currentSnapshot.test = retVal;
currentSnapshot.test--;
...
break;
}
}
// (Second rule)
return retVal;
}
Eighth rule
- 如果递归函数有返回值,在每次循环迭代时,保存返回值到局部变量 (如 retVal ).
- 这个局部变量retVal 就是循环结束后,递归的最终值.
// Recursive Function "Eighth rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
// (Sixth rule)
switch( currentSnapshot.stage)
{
case 0:
// (Seventh rule)
if( currentSnapshot.n>0 )
{
...
}
...
// (Eighth rule)
retVal = 0 ;
...
break;
case 1:
// (Seventh rule)
currentSnapshot.test = retVal;
currentSnapshot.test--;
...
// (Eighth rule)
retVal = currentSnapshot.test;
...
break;
}
}
// (Second rule)
return retVal;
}
Ninth rule
- 如果递归含有返回值,把原来递归函数中的关键字 "
return
" 替换成"while
"循环中的关键字 "continue
"。
- 如果递归函数有返回值,如 "Eighth rule,"所述,把返回值保存到局部变量中 (如
retVal
), 然后"continue
"继续循环; - 多数情况下, "Ninth rule" 是可选的,但他有助于避免逻辑错误.
- 如果递归函数有返回值,如 "Eighth rule,"所述,把返回值保存到局部变量中 (如
// Recursive Function "Ninth rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
// (Sixth rule)
switch( currentSnapshot.stage)
{
case 0:
// (Seventh rule)
if( currentSnapshot.n>0 )
{
...
}
...
// (Eighth rule)
retVal = 0 ;
// (Ninth rule)
continue;
break;
case 1:
// (Seventh rule)
currentSnapshot.test = retVal;
currentSnapshot.test--;
...
// (Eighth rule)
retVal = currentSnapshot.test;
// (Ninth rule)
continue;
break;
}
}
// (Second rule)
return retVal;
}
Tenth rule (and the last...)
- 为了实现从递归调用到迭代函数的转换,在每次迭代中,创建一个新的 "Snapshot" 对象, 初始化 这个新的"
Snapshot
" 对象和递归条件分类 stage, 依据递归函数的参数设置他的成员变量,压栈, 然后继续 "continue
" - 如果递归函数调用之后,有其他处理过程,就需要调整当前"currentSnapshot"中保持的递归条件分类 stage ,并把当前"Snapshot"压栈,然后再对新建的"Snapshot"压栈。
// Recursive Function "Tenth rule" example
int SomeFunc(int n, int &retIdx)
{
...
if(n>0)
{
int test = SomeFunc(n-1, retIdx);
test--;
...
return test;
}
...
return 0;
}
// Conversion to Iterative Function
int SomeFuncLoop(int n, int &retIdx)
{
// (First rule)
struct SnapShotStruct {
int n; // - parameter input
int test; // - local variable that will be used
// after returning from the function call
// - retIdx can be ignored since it is a reference.
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
// (Third rule)
stack<SnapShotStruct> snapshotStack;
// (Fourth rule)
SnapShotStruct currentSnapshot;
currentSnapshot.n= n; // set the value as parameter value
currentSnapshot.test=0; // set the value as default value
currentSnapshot.stage=0; // set the value as initial stage
snapshotStack.push(currentSnapshot);
// (Fifth rule)
while(!snapshotStack.empty())
{
currentSnapshot=snapshotStack.top();
snapshotStack.pop();
// (Sixth rule)
switch( currentSnapshot.stage)
{
case 0:
// (Seventh rule)
if( currentSnapshot.n>0 )
{
// (Tenth rule)
currentSnapshot.stage = 1; // - current snapshot need to process after// returning from the recursive call snapshotStack.push(currentSnapshot); // - this MUST pushed into stack before
// new snapshot!
// Create a new snapshot for calling itself
SnapShotStruct newSnapshot;
newSnapshot.n= currentSnapshot.n-1; // - give parameter as parameter given
// when calling itself
// ( SomeFunc(n-1, retIdx) )
newSnapshot.test=0; // - set the value as initial value
newSnapshot.stage=0; // - since it will start from the
// beginning of the function,
// give the initial stage
snapshotStack.push(newSnapshot);
continue;
}
...
// (Eighth rule)
retVal = 0 ;
// (Ninth rule)
continue;
break;
case 1:
// (Seventh rule)
currentSnapshot.test = retVal;
currentSnapshot.test--;
...
// (Eighth rule)
retVal = currentSnapshot.test;
// (Ninth rule)
continue;
break;
}
}
// (Second rule)
return retVal;
}
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.
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
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