先序遍历的非递归算法

合集下载

图的深度优先遍历(DFS)c++非递归实现

图的深度优先遍历(DFS)c++非递归实现

图的深度优先遍历(DFS)c++⾮递归实现深搜算法对于程序员来讲是必会的基础,不仅要会,更要熟练。

ACM竞赛中,深搜也牢牢占据着很重要的⼀部分。

本⽂⽤显式栈(⾮递归)实现了图的深度优先遍历,希望⼤家可以相互学习。

 栈实现的基本思路是将⼀个节点所有未被访问的“邻居”(即“⼀层邻居节点”)踹⼊栈中“待⽤”,然后围绕顶部节点猛攻,每个节点被访问后被踹出。

读者可以⾃⼰画图分析⼀下,难度并不⼤。

代码写的⽐较随意,仅供参考。

~#include <iostream>#include <stack>using namespace std;#define MaxNode 20#define MAX 2000#define StartNode 1int map[MaxNode+1][MaxNode+1];void dfs_stack(int start, int n){int visited[MaxNode],s_top;for(int i = 0;i <= MaxNode; i++){visited[i] = 0;}visited[start] = 1;stack <int> s;cout<<start<<"";for(int i = 1; i <= n; i++){if(map[i][start] == 1 && !visited[i] ){visited[i] = 1;s.push(i);}}while(!s.empty()){s_top = s.top();visited[s_top] = 1;cout<<s_top<<"";s.pop();for(int i = 1; i <= n; i++){if(map[i][s_top] == 1 && !visited[i] ){visited[i] = 1;s.push(i);}}}}int main(int argc, const char * argv[]) {int num_edge,num_node;int x,y;cout<<"Input number of nodes and edges >"<<endl;cin>>num_node>>num_edge;for(int i =0;i<num_node;i++){for(int j=0;j<num_node;j++){map[i][j] = 0;}}for(int i = 1; i <= num_edge; i++){cin>>x>>y;map[x][y] = map[y][x] = 1;}dfs_stack(StartNode, num_node);return0;}。

完全二叉树非递归无堆栈先序遍历算法的研究

完全二叉树非递归无堆栈先序遍历算法的研究

又 被 Mateti等人于 1988年改进 0 。国内也一直有 学者在做 相 关 的 研 究 。可 从 文 献 [4.12]的研 究 主 题 可 以看 出 ,近 10年 来 对 此 主 题 的研 究 从 未 间 断 ,并 且 近 几 年 的 关 注 度 更 高 。
0 引 言
二 叉 树 作 为 一种 重 要 的 数 据 结 构 是 工农 业 应 用 与 开 发 的 重要工 具。满 二叉树 的中序序列 能够与一 条有 向连续 曲线上 的 点 列 建 立 自起 点 到 终 点 的 一 一 对 应 的 关 系 ;二 叉 树 的 先 序 序 列 ,能 与 植 物 从 根 部 向枝 叶 的生 长 发 育 过 程 建 立 关 联 ,可 作 为 植 物 生 产 建 模 的 基 本 数 据 结 构 模 型 。因 此 ,研 究 二 叉 树 的 先 序 、中序 相 关 算 法 成 为 工 农 业 信 息 技 术 领 域 的关 注 点 。
Abstract: Through a study on the analytic relationship am ong a full binary tree, its sequential storage sequence a n d its preorder-traversal sequence, a algorithms is obtained,which can conve ̄ a full binary t ree a n d its sequential storage sequence into its preorder-traversal se· quence. Consequentl ̄ non—recursive and stack-free algorithms are deduced for preorder t raversal ofa complete binary tree and for inter- conversionsbetweenthe sequential storage sequen ce andthepreorder-tmversal seque n ce. The algor ithms carla1]SWe r a quer y ofanode in constant tim e an d perform a traversal in linear tim e. Being derived from exact m athem atical a n alysis and inosculated with deductions ofbinary encodes that naturally fit the bitwise operation, the algorithms are available for both conventional programming and professional developments such as embedded system and SO on. A sample example is presented to demonstrate the application of the algorithms in virtual-plants modeling. Key words: binary t ree; sequential storage m odel; preorder traversal; non--recursive and stack--free; virtual pla n ts

二叉树的遍历

二叉树的遍历

二叉树的遍历算法1 先序遍历(T、p、S()、top)\*先序遍历的非递归算法,T为根指针,p为指针,指向当前结点。

使用一个栈S()、top为栈顶指针*\1.if(T=NULL)2.Printf( “这是一棵空二叉树”);3.else{△△p=T;top=0;4. While(top≠0)||(p≠NULL){△while(p≠NULL){﹡Visit(p→data); \﹡访问结点﹡\top=top+1;if (top>n) 调用栈满else{S(top)=p→rchild;P=P→lchild;}}﹡if (top≠0){p= S(top);top--;}}△}△△{算法结束}算法2 中序遍历(T、p、S()、top)\*{中序遍历的非递归算法,使用栈S(),top为栈顶指针,T指向根,p为指针,指向当前结点*\top=0,p=TWhile(top≠0)||(P≠NULL){While(P≠NULL){Top=top+1if (top>n) 调用栈满else{S(top)=p, p=p→lchied;}}If (top≠null){p=S(top);top=top-1;Visit(p→data); \﹡访问结点﹡\p=p→rchild;}}{算法结束}算法3 后序遍历(T、p、S()、top)\*后序遍历的非递归算法,T指向根,P为指标指向当前结点,使用堆栈S(),栈顶指针为top,*\1、if (t=NIL)2、then { 输出“这是一棵空树”go 22 \* 结束 *\3、else { p=t;top=0;4、 while (top≠0)||(p≠NIL) {5、 while (p≠NIL){6、 top=top+1;7、 if (top﹥n)8、调用栈满9、 else{S(top)=p;10、 p=p→lchild;}11、 }12、 if (top≠0){13、 p=S(top);top=top-114、 if (p﹤0){15、 p=-p;16、 Visit(p→data); \﹡访问结点﹡\17、 p=NIL;〕18、 else {top=top+1;19、 S(top)=-P;20、 p=p→rchild;}21、 }22、{算法结束}算法4 二叉树后序遍历(t、p、S()、top、h)\*后序遍历的非递归算法,T指向根,使用堆栈S(),top为栈顶指针,P为指针,指向当前结点,h为指针,指向刚被访问结点*\1、if (T=Nil){ 输出“这是一棵空树”go 20}2、else{﹡p=t,top=03、 if (p→lchild=Nil)&&(P→rchild=Nil)4、 then go 125、 else{△△top=top+1;S(top)=p;6、 if (p→lchild=Nil)7、 {p= p→rchild; go 3;}8、 else {△if (p→rchild=Nil)9、 go 1110、 else {top=top+1; S(top)= p→rchild;}11、 P=p→lchil; go 3}△}△△12、 Visit(p→data); \﹡访问结点﹡\13、 h=p14、 if (top=0){15、输出“栈空” go 20;}16、 else {p= S(top);top=top-1;17、 if(p→Lchild=h)OR(p→rchild=h)18、 then go 12;19、 else go 3;}}﹡20、{算法结束}。

广度优先遍历非递归算法

广度优先遍历非递归算法

广度优先遍历非递归算法
广度优先遍历(BFS)是一种图的遍历算法,用来遍历图中所有的节点。

以下是广度优先遍历的非递归算法:
1. 创建一个队列,用来存放待访问的节点。

2. 将起始节点放入队列中。

3. 创建一个集合,用来存放已经访问过的节点。

4. 将起始节点加入到已访问节点的集合中。

5. 循环执行以下步骤,直到队列为空为止:
5.1. 从队列中取出一个节点。

5.2. 访问该节点。

5.3. 获取该节点的所有相邻节点。

5.4. 对于每个相邻节点,如果它没有被访问过,则将其加入到队列和已访问节点的集合中。

通过以上步骤,可以按广度优先的顺序遍历整个图。

这种非递归算法利用队列的先进先出(FIFO)特性,保证了相邻节点的访问顺序是按照它们与起始节点的距离逐渐增加的顺序进行的,从而实现了广度优先遍历。

先序遍历的非递归算法

先序遍历的非递归算法

数据结构1、先序遍历的非递归算法。

用c语言写。

void PreOrderUnrec(Bitree t){SqStack s;StackInit(s);p=t;while (p!=null || !StackEmpty(s)){while (p!=null) //遍历左子树{visite(p->data);push(s,p);p=p->lchild;}//endwhileif (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历{ p=pop(s);p=p->rchild;}//endif}//endwhile}//PreOrderUnrec/////////////////////////////////#include "stdio.h"#include "stdlib.h"#include "string.h"#define null 0struct node{char data;struct node *lchild;struct node *rchild;};//先序,中序建树struct node *create(char *pre,char *ord,int n){struct node * head;int ordsit;head=null;if(n<=0){return null;}else{head=(struct node *)malloc(sizeof(struct node)); head->data=*pre;head->lchild=head->rchild=null;ordsit=0;while(ord[ordsit]!=*pre){ordsit++;}head->lchild=create(pre+1,ord,ordsit);head->rchild=create (pre+ordsit+1,ord+ordsit+1,n-ordsit-1); return head;}}//中序递归遍历void inorder(struct node *head){if(!head)return;else{inorder(head->lchild );printf("%c",head->data );inorder(head->rchild );}}//中序非递归遍历void inorder1(struct node *head) {struct node *p;struct node *stack[20];int top=0;p=head;while(p||top!=0){while (p){stack[top++]=p;p=p->lchild ;}p=stack[--top];printf("%c ",p->data );p=p->rchild ;}}/////////////////////////////////////////////////////////////////// ////////////////////二叉树前序、中序、后序三种遍历的非递归算法1.先序遍历非递归算法void PreOrderUnrec(Bitree *t){Stack s;StackInit(s);Bitree *p=t;while (p!=NULL || !StackEmpty(s)){while (p!=NULL) //遍历左子树{visite(p->data);push(s,p);p=p->lchild;}if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历{p=pop(s);p=p->rchild;}//endif}//endwhile}2.中序遍历非递归算法void InOrderUnrec(Bitree *t){Stack s;StackInit(s);Bitree *p=t;while (p!=NULL || !StackEmpty(s)){while (p!=NULL) //遍历左子树{push(s,p);p=p->lchild;}if (!StackEmpty(s)){p=pop(s);visite(p->data); //访问根结点p=p->rchild; //通过下一次循环实现右子树遍历}//endif }//endwhile}3.后序遍历非递归算法typedef enum{L,R} tagtype;typedef struct{Bitree ptr;tagtype tag;}stacknode;typedef struct{stacknode Elem[maxsize];int top;}SqStack;void PostOrderUnrec(Bitree t) {SqStack s;stacknode x;StackInit(s);p=t;do{while (p!=null) //遍历左子树{x.ptr = p;x.tag = L; //标记为左子树push(s,x);p=p->lchild;}while (!StackEmpty(s) && s.Elem[s.top].tag==R){x = pop(s);p = x.ptr;visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点} if (!StackEmpty(s)){s.Elem[s.top].tag =R; //遍历右子树p=s.Elem[s.top].ptr->rchild;}}while (!StackEmpty(s));}//PostOrderUnrec二。

三叉链表存储结构及其非递归遍历算法

三叉链表存储结构及其非递归遍历算法

数据结构实验报告知识范畴:树实验题目:二叉树的基本算法二(三叉链表的建立、非递归遍历)实验内容及要求:设二叉树采用三叉链表存储结构,结点数据域为字符类型,从键盘输入先序遍历字符序列(用#字符表示NULL指针域)建立三叉链表存储结构。

对先序、中序、后序遍历分别定义各自的求第一访问结点地址first(bt)以及下一访问结点地址next(p)函数,然后用三种遍历的first(bt)和next(p)函数实现非递归遍历。

实验目的:掌握二叉树的三叉链表存储结构及其非递归遍历算法。

数据结构设计简要描述:采用双向链表,每个结点包括字符类型的数据域和一个指针域。

链表结点结构如下:typedef struct node{ElemTp data; //字符数据域struct node *lchild; //左儿子指针struct node *rchild; //右儿子指针struct node *parent; //双亲指针}算法设计简要描述:采用三叉链表的存储结构,双亲指针指向根结点,左右指针指向左右儿子构造双向链表的二叉树。

每一次遍历时先用该遍历的first函数获取第一个访问的结点地址,调用next函数获取下一个访问的结点地址,当结点为空为循环的结束条件。

获取下一个访问的结点地址时需要判断是否需要回溯,需要回溯时通过双亲指针获取根节点地址,再判断是否需要再回溯。

回溯的结束条件为根节点地址为空。

输入/输出设计简要描述:从键盘输入二叉树的数据域,用#表示空。

按先序遍历的顺序依次构建二叉树。

输出三种遍历的遍历结果,并有文字提示。

编程语言说明:使用Visual C++编程。

主要代码采用C语言实现;动态存储分配采用C的malloc操作符实现;输入与输出采用C的printf和scanf流;程序注释采用C/C++规范。

主要函数说明:void InitTrT(TrT bt) //初始化二叉树void crtTrT(TrT *bt) //创建三叉结构的二叉树void destroyTrT(TrT *bt) //销毁二叉树Status emptyTrT(TrT bt) //判断二叉树是否为空int depthTrT(TrT bt) //求二叉树的深度TrT prefirst(TrT bt) //找出先序遍历的第一个结点TrT prenext(TrT bt) //找出先序遍历的下一结点void preorder(TrT bt) //先序非递归遍历TrT midfirst(TrT bt,int &mark) //查找中序遍历的第一个结点TrT midnext(TrT bt,int &mark) //查找中序遍历的下一个结点void midorder(TrT bt) //中序非递归遍历TrT lastfirst(TrT bt,int &mark) //查找后序遍历的第一个结点TrT lastnext(TrT bt,int &mark) //查找后序遍历的下一个结点void lasorder(TrT bt) //后序非递归遍历程序测试简要报告:输入:ABC#F##D##E##输出:程序输出结果与期望输出结果相符。

二叉树的遍历及相关题目

二叉树的遍历及相关题目

⼆叉树的遍历及相关题⽬⼆叉树的遍历及相关题⽬1.1⼆叉树遍历的概念⼆叉树结构体的定义:typedef struct node{ ElemType data; struct node * lchild; struct node * rchild;}⼆叉树的遍历是指按照⼀定的次序访问⼆叉树中的所有的节点,并且每个节点仅访问⼀次的过程。

若规定先遍历左⼦树,后遍历右⼦树,则对于⾮空⼆叉树,可得到如下3种递归的遍历⽅法:(1)先序遍历访问根节点,先序遍历左⼦树,先序遍历右⼦树。

(根,左,右)(2)中序遍历中序遍历左⼦树,访问根节点,中序遍历右⼦树。

(左,根,右)(3)后序遍历后序遍历左⼦树,后序遍历右⼦树,访问根节点。

(左,右,根)除此之外也有层次遍历。

先访问根节点,在从左到右访问第⼆层的所有节点,从左到右访问第三层的所有节点......1.2⼆叉树遍历递归算法先序遍历递归算法:void PreOrder(BTNode * b){ if(n != NULL) { cout<<b->data; PreOrder(b->lchild); PreOrder(b->rchild); }}中序遍历递归算法void InOrder(BTNode * b){ if(n != NULL) { InOrder(b->lchild); cout<<b->data; InOrder(b->rchild); }}后序遍历递归算法:void PostOrder(BTNode * b){ if(b != NULL) { PostOrder(b->lchild); PostOrder(b->rchild); cout<<b->data; }}题⽬1:输出⼀个给定⼆叉树的所有的叶⼦节点:void DispLeaf(BTNode * b){ if(b != NULL) { if(b->lchild == NULL && b->rchild == NULL) cout<<b->data; DispLeaf(b->lchild); DispLeaf(b->rchild); }}以上算法采⽤先序遍历输出了所有的叶⼦节点,所以叶⼦节点是从左到右输出的。

计算机专业基础综合数据结构(树和二叉树)历年真题试卷汇编11

计算机专业基础综合数据结构(树和二叉树)历年真题试卷汇编11

计算机专业基础综合数据结构(树和二叉树)历年真题试卷汇编11(总分:60.00,做题时间:90分钟)一、填空题(总题数:24,分数:48.00)1.二叉树按某种顺序线索化后,任一结点均有指向前驱和后继的线索,这种说法是正确的么?__________【南京理工大学2005二、9(1分)】__________________________________________________________________________________________ 正确答案:(正确答案:说法错误。

只有空指针处才能加线索,左指针指向前驱(遍历的第一个结点无前驱),右指针指向后继(遍历的最后一个结点无后继)。

)2.线索二元树的左线索指向其__________,右线索指向其__________。

【哈尔滨工业大学2000二、3 (2分)】__________________________________________________________________________________________ 正确答案:(正确答案:(1)前驱 (2)后继)3.将一棵树转换成二叉树后,根结点没有__________子树。

【电子科技大学2005二、2(1分)】__________________________________________________________________________________________ 正确答案:(正确答案:右)4.哈夫曼树是__________。

【北京理工大学200l七、4(2)】【长沙铁道学院1998二、3(2分)】__________________________________________________________________________________________ 正确答案:(正确答案:带权路径长度最小的二叉树,又称最优二叉树)5.若以{4,5,6,7,8}作为叶子结点的权值构造哈夫曼树,则其带权路径长度是__________。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据结构
1、先序遍历的非递归算法。

用c语言写。

void PreOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;
while (p!=null || !StackEmpty(s))
{
while (p!=null) //遍历左子树
{
visite(p->data);
push(s,p);
p=p->lchild;
}//endwhile
if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历{
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}//PreOrderUnrec
/////////////////////////////////
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define null 0
struct node
{
char data;
struct node *lchild;
struct node *rchild;
};
//先序,中序建树
struct node *create(char *pre,char *ord,int n)
{
struct node * head;
int ordsit;
head=null;
if(n<=0)
{
return null;
}
else
{
head=(struct node *)malloc(sizeof(struct node));
head->data=*pre;
head->lchild=head->rchild=null;
ordsit=0;
while(ord[ordsit]!=*pre)
{
ordsit++;
}
head->lchild=create(pre+1,ord,ordsit);
head->rchild=create (pre+ordsit+1,ord+ordsit+1,n-ordsit-1); return head;
}
}
//中序递归遍历
void inorder(struct node *head)
{
if(!head)
return;
else
{
inorder(head->lchild );
printf("%c",head->data );
inorder(head->rchild );
}
}
//中序非递归遍历
void inorder1(struct node *head)
{
struct node *p;
struct node *stack[20];
int top=0;
p=head;
while(p||top!=0)
{
while (p)
{
stack[top++]=p;
p=p->lchild ;
}
p=stack[--top];
printf("%c ",p->data );
p=p->rchild ;
}
}
///////////////////////////////////////////////////////////////////////////////////////
二叉树前序、中序、后序三种遍历的非递归算法
1.先序遍历非递归算法
void PreOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;
while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL) //遍历左子树
{
visite(p->data);
push(s,p);
p=p->lchild;
}
if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历
{
p=pop(s);
p=p->rchild;
}//endif
}//endwhile
}
2.中序遍历非递归算法
void InOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;
while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL) //遍历左子树
{
push(s,p);
p=p->lchild;
}
if (!StackEmpty(s))
{
p=pop(s);
visite(p->data); //访问根结点
p=p->rchild; //通过下一次循环实现右子树遍历}//endif
}//endwhile
}
3.后序遍历非递归算法
typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;
typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;
void PostOrderUnrec(Bitree t)
SqStack s;
stacknode x;
StackInit(s);
p=t;
do
{
while (p!=null) //遍历左子树
{
x.ptr = p;
x.tag = L; //标记为左子树
push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点}
if (!StackEmpty(s))
{
s.Elem[s.top].tag =R; //遍历右子树
p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec
二。

前序最简洁算法
void PreOrderUnrec(Bitree *t)
{
Bitree *p;
Stack s;
s.push(t);
while (!s.IsEmpty())
{
s.pop(p);
visit(p->data);
if (p->rchild != NULL) s.push(p->rchild);
if (p->lchild != NULL) s.push(p->lchild);
}
三。

后序算法之二
void BT_PostOrderNoRec(pTreeT root)
{
stack<treeT *> s;
pTreeT pre=NULL;
while ((NULL != root) || !s.empty())
{
if (NULL != root)
{
s.push(root);
root = root->left;
}
else
{
root = s.top();
if (root->right!=NULL && pre!=root->right){ root=root->right;
}
else{
root=pre=s.top();
visit(root);
s.pop();
root=NULL;
}
}
}
}。

相关文档
最新文档