当前位置: 首页 > news >正文

408之cpp(二叉树的广度优先遍历)

       二叉树的广度优先遍历(又称层序遍历)需要使用队列(通常用链表实现)来完成。以下是实现的代码,分为三个文件:function.h、queue.cpp 和 main.cpp。

1.function.h

#include<stdio.h>
#include<stdlib.h>
//二叉树的函数声明
typedef char elemtype;
typedef struct bitree {elemtype tdata;struct bitree* lchild, * rchild;
}bitree, * pbitree;
typedef struct tag {pbitree p;//指向某一个二叉树指针struct tag* next;
}tag, * ptag;//辅助队列函数声明
typedef struct node{pbitree data;struct node *next;
}node;
typedef struct sq_queue {node* front, * rear;
}sq_queue;
void init_queue(sq_queue& Q);
bool is_empty(sq_queue Q);
void en_queue(sq_queue &Q,pbitree x);
void de_queue(sq_queue &Q, pbitree &keep);

2.queue.cpp(辅助队列的实现)

#include"function.h"
void init_queue(sq_queue& Q)
{Q.front = Q.rear = (node*)calloc(1, sizeof(node));Q.front->next = NULL;
}
bool is_empty(sq_queue Q)
{if (Q.rear == Q.front){return true;}return false;
}
void en_queue(sq_queue &Q,pbitree x)
{node* s = (node*)calloc(1, sizeof(node));s->data = x;s->next = NULL;Q.rear->next = s;Q.rear = s;
}
void de_queue(sq_queue& Q, pbitree& keep)
{node* p = Q.front->next;keep = p->data;Q.front->next = p->next;if (Q.rear == p){Q.rear = Q.front;}free(p);
}

3.main.cpp(层序遍历的实现)

#include"function.h"
void level_order(pbitree tree)
{sq_queue Q;init_queue(Q);pbitree keep;en_queue(Q, tree);//将根节点入队while (!is_empty(Q)){de_queue(Q,keep);putchar(keep->tdata);if (keep->lchild != NULL){en_queue(Q, keep->lchild);//将左孩子入队}if (keep->rchild != NULL){en_queue(Q, keep->rchild);//将右孩子入队}}
}
int main()
{//二叉树建树pbitree pnew;pbitree tree = NULL;elemtype c;ptag head = NULL, tail = NULL, pcur = NULL, link_new = NULL;while (scanf("%c", &c)){if (c == '\n'){break;}pnew = (pbitree)calloc(1, sizeof(bitree));pnew->tdata = c;link_new = (ptag)calloc(1, sizeof(tag));link_new->p = pnew;if (tree == NULL){tree = pnew;head = link_new; tail = link_new;pcur = link_new; link_new = link_new;continue;}else{tail->next = link_new;tail = tail->next;}if (pcur->p->lchild == NULL){pcur->p->lchild = pnew;}else if(pcur->p->rchild == NULL){pcur->p->rchild = pnew;pcur = pcur->next;}}level_order(tree);//层序遍历(广度优先遍历)}

http://www.dtcms.com/a/438897.html

相关文章:

  • 保定市网站销售和设计中国各大网站开发语言
  • 本地常州微信网站建设做网站税率
  • 网页设计 做网站的代码公司网站开发项目管理制度
  • EFlat-LoRA 的严格数学推导
  • 【面板数据】全国分省教育支出水平数据集(2007-2023年)
  • 做网站工商局要不要备案呢wordpress主题多页面
  • 济南网站建设服务公司wordpress 远程插件安装 ftp
  • 济南网站建设公司按需定制保险网站建设的目标
  • 深入剖析:boost::intrusive_ptr 与 std::shared_ptr 的性能边界和实现哲学
  • 聊城制作手机网站公司网站建设需要的条件
  • SQL 子查询与多表 JOIN 用法大全(速查版)
  • Leetcode 239. 滑动窗口最大值 优先队列 / 双向单调队列
  • Nacos 工作原理及流量走向
  • 夏津网站建设茂名企业建站程序
  • OSPF 单区域实验 概念及题目
  • 建立一个门户网站WordPress域名后问号英文
  • 自上而下VS自下而上:设计哲学全解
  • 【开题答辩全过程】以 SpringCloud家乡美旅行交流博客平台为例,包含答辩的问题和答案
  • 2015优先中文公司官网wordpress模板
  • 国外优秀企业网站网络空间的竞争归根结底是
  • 哪些外贸网站可以做soho求网站2021给个网址
  • 2022年网站能用的兰州企业网站制作
  • 网页设计与网站建设实战大全推荐好的网站或网页
  • 查看网站是否做百度推广如果在网上接网站建设项目
  • 如何用源码搭建网站源码网站搭建规划
  • 【办公类-117-01】20250924通义万相视频2.5——三个小人(幼儿作品动态化)
  • PBS, 以太坊的棘刺雕猴
  • 【未来】智能体互联时代的商业模式变化和挑战:从HOM到AOM
  • 域名免费注册网站网站模板凡建站
  • 关键词挖掘站长c 教程如何做网站