当前位置:网站首页>C语言实现树的底层遍历--超简代码
C语言实现树的底层遍历--超简代码
2022-08-03 06:34:00 【干饭小白】
//对树进行底层遍历时使用了队列的结构
基础类型:
typedef enum {FALSE = 0,TRUE = 1} Bool;
typedef enum {VOERFLOW = -2,UNDERFLOW = -1, ERROR = 0,OK = 1} Status;
树的二叉链表结点定义:
typedef struct Node{
char data;
struct Node *firstchild,*nextbrother;
}Node,*TreeNode;
//实现队列基本操作的函数原型表
void InitQueue(Queue *Q); //初始化队列
Bool IsEmpty(Queue Q); //判断队列是否为空,若是则返回TRUE,否则返回FALSE
void EnQueue(Queue *Q,TreeNode p); //元素入队列
void DeQueue(Queue *Q,TreeNode *p); //元素出队列
//函数代码
Status LevelTraverser(TreeNode root)
{
/*层序遍历树,树采用孩子-兄弟表示法,root是树根结点的指针*/
Queue tmpQ;
TreeNode ptr,brotherptr;
if( !root )
return ERROR;
InitQueue(&tmpQ);
EnQueue(tmpQ,root);
brotherptr = root->nextbrother;
while(brotherptr)
{
EnQueue(&tmpQ,brotherptr);
brotherptr=brotherptr->nextbrother;
}
while(!IsEmpty(tmpQ))
{
DeQueue(&tmpQ,&ptr);
printf("%c\t",ptr->data);
if(!ptr->firstchild) continue;
EnQueue(&tmpQ,ptr->firstchild);
brotherptr =ptr->brotherptr->nextbrother;
while(brotherptr)
{
EnQueue(&tmpQ,brotherptr);
brotherptr = brotherptr->nextbrother;
}
}
return OK;
}边栏推荐
猜你喜欢
随机推荐
安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
keepalived安装部署
unity 摄像机旋转拖拽缩放场景
信息学奥赛一本通T1450:Knight Moves
El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
测试用例设计方法之因果图详解
【卫朋】硬件创业:营销与开发同行
information_schema
nacos-2.0.3启动报错出现no datasource set的坑
Shell脚本之一键安装mysql
戳Web3的神话?戳到铁板。
CDGA|如何加强数字政府建设?
idea远程debug
【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
DIFM network, rounding and repetition
postman将接口返回结果生成csv文件到本地
用代码构建UI界面
Laravel 中使用子查询
【着色器实现HandDrawn简笔画抖动效果_Shader效果第十二篇】
【OpenCV】 - 显示图像API之imshow()对不同位深度(数据类型)的图像的处理方法









