摘要
算法设计中的递归和非递归转换是学习算法设计的基础,熟练地运用递归与非递归转换是算法设计的基础,。
关键词:算法设计递归与非递归转换
Abstract
The algorithm design recursive and non recursive conversion is the basis for learning algorithm design, skilled use of recursive and non recursive conversion is the basis for algorithm design in this paper I will introduce several algorithm design recursive and non recursive conversion method. so that we can better achieve the recursive algorithm design and non-recursive conversion.
Keywords: algorithm design recursive and non recursive conversion
三种遍历树的算法
递归与非递归转换的基础知识是能够正确理解三种树的遍历方法:前序,中序和后序,第一篇就是关于这三种遍历方法的递归和非递归算法。
一、为什么要学习递归与非递归的转换的实现方法
.
.
,树等数据结构.
二、三种遍历树的递归和非递归算法
递归与非递归的转换基于以下的原理:,这个"原理"并没有经过严格的数学证明,只是我的一个猜想,,有三种方法可以遍历树:前序,中序,,,这里以特殊的二叉树来说明,不过大多数情况下二叉树已经够用,而且理解了二叉树的遍历,其它的树遍历方式就不难了。
1)前序遍历
a)递归方式:
void preorder_recursive(Bitree T) /* 先序遍历二叉树的递归算法*/
{
if (T) {
visit(T); /* 访问当前结点*/
preorder_recursive(T->lchild); /* 访问左子树*/
preorder_recursive(T->rchild); /* 访问右子树*/
}
}
b)非递归方式
void preorder_nonrecursive(Bitree T) /* 先序遍历二叉树的非递归算法*/
{
initstack(S);
push(S,T); /* 根指针进栈*/
while(!stackempty(S)) {
while(gettop(S,p)&&p) { /* 向左走到尽头*/
visit(p); /* 每向前走一步都访问当前结点*/
push(S,p->lchild);
}
pop(S,p);
if(!stackempty(S))
信息与计算科学专业毕业论文--算法设计中的递归与非递归转换 来自淘豆网m.daumloan.com转载请标明出处.