二叉树的层次非递归遍历运用队列
来源:互联网 发布:ncbi下载基因组数据 编辑:程序博客网 时间:2024/06/12 00:10
#include <stdlib.h>#include <stdio.h>#define ElemType chartypedef struct BiTNode *BiTree;struct BiTNode{ ElemType data; BiTree lchild,rchild;};BiTree CreateBiTree(){ ElemType ch; BiTree T; scanf("%c",&ch); if(ch=='#')T=NULL; else{ T = (BiTree)malloc(sizeof *T); T->data = ch; T->lchild = CreateBiTree(); T->rchild = CreateBiTree(); } return T;}typedef struct QUEUEnode* link;struct QUEUEnode{BiTree item ;link next;};static link head , tail;link NEW(BiTree item, link next){link x = (link) malloc(sizeof *x);x->item = item;x->next = next;return x;}void QUEUEinit(int maxN){head = NULL;}int QUEUEempty(){return head == NULL;} void QUEUEput(BiTree item){if(head == NULL){head =(tail = NEW(item,head)) ;return;}tail->next = NEW(item,tail->next);tail = tail->next;}BiTree QUEUEget(){BiTree item = head->item;link t = head->next;free(head);head = t;return item;} void levelTraverse(BiTree T){QUEUEinit(40);QUEUEput(T);while(!QUEUEempty()){T = QUEUEget();printf("%c",T->data);if(T->lchild!=NULL)QUEUEput(T->lchild);if(T->rchild!=NULL)QUEUEput(T->rchild);}}main(){ BiTree T; T = CreateBiTree(); //先序建立二叉树 levelTraverse(T);// DBA##C##FE##G##}
运行结果图
<img src="http://img.blog.csdn.net/20150813134456713?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
0 0
- 二叉树的层次非递归遍历运用队列
- 二叉树的非递归层次遍历
- 二叉树的遍历(递归+非递归+层次遍历)
- 二叉树的前序中序后序遍历,非递归遍历 层次遍历
- 二叉树遍历(层次,递归,非递归)
- 二叉树的递归,层次,以及非递归遍历
- 二叉树的各种遍历(递归,非递归,层次)
- 二叉树的遍历(递归,非递归,层次)
- 二叉树的递归、层次与非递归遍历
- 二叉树的遍历(递归,非递归,层次)
- 二叉树的非递归遍历及层次遍历
- 二叉树的非递归遍历以及层次遍历
- 二叉树的非递归遍历,层次遍历
- 二叉树前中后序遍历的递归版本和非递归版本、队列实现的层次遍历
- [二叉树专题]:广度优先:按层次遍历二叉树的非递归实现||使用队列实现层次遍历二叉树
- 非递归按层次遍历二叉树
- 非递归二叉树层次遍历算法
- 二叉树非递归层次遍历
- I学霸官方免费教程四十 :Java基础教程之线程同步
- Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
- UVA - 10305 - Ordering Tasks(拓扑排序)
- 黑马程序员——OC笔记之内存管理
- 使用druid连接池,配置sql防火墙发现的sql注入问题
- 二叉树的层次非递归遍历运用队列
- 适配器模式
- Rotate List
- 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第一节:3D时代来临!
- Linux chmod +755和chmod +777 各是什么意思呢?
- /dev/mem可没那么简单
- HTML 相对路径表示方法
- 设置应用程序的图标(Setting the Application Icon)— Mac OS X
- SDN开源,从你的全世界路过