采用队列数据结构按层次遍历二叉树
来源:互联网 发布:网上兼职赚钱日结淘宝 编辑:程序博客网 时间:2024/05/22 07:54
本例是采用一般的队列来存储和访问二叉树
访问过程描述如下:
访问根结点,并将该结点记录下来;
若记录的所有结点都已处理完毕,则结束遍历操作;否则重复下列操作。
取出记录中第一个还没有访问孩子的结点,若它有左孩子,则访问左孩子,并将记录下来;
若它有右孩子,则访问右孩子,并记录下来。
在这个算法中,应使用一个队列结构完成这项操作。所谓记录访问结点就是入队操作;
而取出记录的结点就是出队操作。这样一来,我们的算法就可以描述成下列形式:
(1)访问根结点,并将根结点入队;
(2)当队列不空时,重复下列操作:
从队列退出一个结点;
若其有左孩子,则访问左孩子,并将其左孩子入队;
若其有右孩子,则访问右孩子,并将其右孩子入队;
其基本算法为:
//按层次遍历树中结点
void traverse(bitree bt)
{
linkqueue q;
bitree p;
initqueue(q); //初始化一个空的队列
p=bt;
enqueue(q,p); //入队
while(queueempty(q)!=1)
{
dequeue(q,p); //出队
if(p->lchild!=NULL)
enqueue(q,p->lchild); //访问左子树
if(p->rchild!=NULL)
enqueue(q,p->rchild); //访问右子树
}
printf("/n");
}
为了保证唯一地构造出所希望的二叉树,在键入这棵树的先序序列时,需要在所有空二叉
树的位置上填补一个特殊的字符,比如,' '。在算法中,需要对每个输入的字符进行判
断,如果对应的字符是' ',则在相应的位置上构造一棵空二叉树;否则,创建一个新结
点。整个算法结构以先序遍历递归算法为基础,二叉树中结点之间的指针连接是通过指针
参数在递归调用返回时完成。
int createbitree(bitree &T,int &sum)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(bitree)malloc(sizeof(binode))))
return 0;
T->data=ch;
sum++;
createbitree(T->lchild,sum);
createbitree(T->rchild,sum);
}
return 1;
}
具体实现过程为:
#include<stdio.h>
#include<malloc.h>
typedef struct binode
{
char data;
struct binode *lchild,*rchild;
}binode,*bitree; //定义树结点结构
typedef struct queuenode
{
bitree ch;
struct queuenode *next;
}queuenode,*queueptr; //定义队列结点结构
typedef struct
{
queueptr front;
queueptr rear;
}linkqueue; //定义队列指针
//建树
int createbitree(bitree &T,int &sum)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(bitree)malloc(sizeof(binode))))
return 0;
T->data=ch;
sum++;
createbitree(T->lchild,sum);
createbitree(T->rchild,sum);
}
return 1;
}
//初始化一个带头结点的队列
void initqueue(linkqueue &q)
{
q.front=q.rear=(queueptr)malloc(sizeof(queuenode));
q.front->next=NULL;
}
//入队列
void enqueue(linkqueue &q,bitree p)
{
queueptr s;
int first=1;
s=(queueptr)malloc(sizeof(queuenode));
s->ch=p;
s->next=NULL;
q.rear->next=s;
q.rear=s;
}
//出队列
void dequeue(linkqueue &q,bitree &p)
{
char data;
queueptr s;
s=q.front->next;
p=s->ch;
data=p->data;
q.front->next=s->next;
if(q.rear==s)
q.rear=q.front;
free(s);
printf("%c ",data);
}
//判断队列是否为空
int queueempty(linkqueue q)
{
if(q.front->next==NULL)
return 1;
return 0;
}
//按层次遍历树中结点
void traverse(bitree bt)
{
linkqueue q;
bitree p;
initqueue(q);
p=bt;
enqueue(q,p);
while(queueempty(q)!=1)
{
dequeue(q,p);
if(p->lchild!=NULL)
enqueue(q,p->lchild);
if(p->rchild!=NULL)
enqueue(q,p->rchild);
}
printf("/n");
}
//主函数
void main()
{
int n=0;
bitree bt;
createbitree(bt,n);
printf("该二叉树共有%d个结点./n",n);
printf("按层次遍历树中结点其输出顺序为: /n");
traverse(bt);
}
- 采用队列数据结构按层次遍历二叉树
- 采用队列数据结构按层次遍历二叉树
- 二叉树按层次遍历--队列实现
- 二叉树按层次遍历--队列实现
- 二叉树按层次遍历--队列实现
- 队列实现按层次遍历二叉树
- 数据结构-----层次遍历二叉树算法(利用队列实现)
- C++数据结构--按层次遍历二叉树
- 数据结构-层次遍历二叉树
- 数据结构--二叉树--层次遍历二叉树(链式遍历--队列)
- 使用队列实现二叉树按层次遍历(C++)
- 二叉树层次遍历与链队列
- 二叉树的层次遍历 队列
- 二叉树层次遍历(借助队列实现)
- 二叉树利用队列实现层次遍历
- 【数据结构】二叉树的层次遍历
- 【数据结构】二叉树的层次遍历2
- 数据结构 — 5.二叉树层次遍历
- 千万级架构设计诀窍(zz)(
- undefined reference to 'GetStockObject@4 linker error 的解决办法
- ci框架怎么读写操作mysql数据库
- 也谈学习程序设计
- C# MessageBox 使用总结
- 采用队列数据结构按层次遍历二叉树
- criteria
- java 的全面知识体系
- 解决设备管理器,控制面板中管理工具无法打开的问题
- 【转帖】Linux Cut 命令 与 Paste命令&Join命令&tr命令
- windows网上邻居互访基本条件
- GlassFish 替换 Tomcat
- 解决电脑能上QQ,却打不开网页的问题
- 是谁在我的心里打了个结(二十五)圈套