免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include stdafx.h#include iostream#include fstream#include queue#include stack#include Windows.husing namespace std;typedef struct _Node int data; struct _Node *left; struct _Node *right; _Node() data = 0; left = NULL; right = NULL; Node, *_PNode;/*层次遍历的举例*begin/创建二叉树利用先序创建/* / 3 / / 5 6 / / 8 9 10 11 / 13*/void CreateBitree(_PNode pNode, fstream fin) int dat; findat; if(dat=0) pNode = NULL; else pNode = new Node(); pNode-data=dat; CreateBitree(pNode-left, fin); CreateBitree(pNode-right, fin); /二叉树的自下而上,自右向左的遍历方法void ReverseLevel(_PNode pRoot) queue_PNode q; stack_PNode s; _PNode pNode = NULL; if (NULL = pRoot) return; q.push(pRoot); while (!q.empty() pNode = q.front(); q.pop(); s.push(pNode); if (NULL != pNode-left) q.push(pNode-left); if (NULL != pNode-right) q.push(pNode-right); while (!s.empty() pNode = s.top(); s.pop(); coutpNode-data ; coutendl;/统计二叉树中度为1的节点的个数,层次遍历int CountOneDepth(_PNode pRoot) queue_PNode q; _PNode pNode = NULL; int count = 0; if (NULL = pRoot) return count; q.push(pRoot); while (!q.empty() pNode = q.front(); q.pop(); if (NULL != pNode-left NULL = pNode-right) | (NULL = pNode-left NULL != pNode-right) count+; if (NULL != pNode-left) q.push(pNode-left); if (NULL != pNode-right) q.push(pNode-right); return count;/查找二叉树最大宽度int FindMaxWidth(_PNode pRoot) _PNode q100 = 0; _PNode pNode = NULL; int front = 0; int rear = 0; int last = 0; int width = 0; int temp = 0; if (NULL = pRoot) return width; qrear = pRoot; while (front = last) pNode = qfront+; temp+; if (NULL != pNode-left) q+rear = pNode-left; if (NULL != pNode-right) q+rear = pNode-right; if (front last) last = rear; if (temp width) width = temp; temp = 0; return width;/*层次遍历的举例*end/辅助函数,设置控制台的颜色void SetConsoleTextColor(WORD dwColor) HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (INVALID_HANDLE_VALUE = handle) return; SetConsoleTextAttribute(handle, dwColor);int _tmain(int argc, _TCHAR* argv) _PNode pRoot1 = NULL; SetConsoleTextColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY); fstream fin(level.txt); CreateBitree(pRoot1, fin); coutendl*层次遍历举例*endl; coutendl二叉树的自下而上,自右向左的遍历endl; ReverseLevel(pRoot1); co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 螺旋桩机租赁合同范本
- 荣誉证书制作合同范本
- 运输车辆合伙合同范本
- 美甲物品购买合同范本
- 邢台租赁合同协议模板
- 监控维修补充合同范本
- 羊奶粉供货合同协议书
- 网红定制产品合同范本
- 维修房屋承揽合同范本
- 活动19 关爱社区“空巢老人”教学设计-2025-2026学年小学劳动五年级北师大·深圳报业版《劳动实践指导手册》(主编:韩震)
- 2025年国有企业投资管理制度
- 规范足球训练计划内容
- 公司团建活动总结
- 2025兼职劳动合同简易范本下载
- 2025四川蜀道高速公路集团有限公司招聘工作人员笔试考试参考试题及答案解析
- 2025下半年四川省自然资源投资集团社会招聘考试笔试备考题库及答案解析
- 安全生产监督员考试题库及答案解析
- 读书活动彩排活动方案
- 2025年神经外科手术室护士术前准备与术后护理模拟考核试题及答案解析
- 法学概论(第七版)课件全套谷春德第1-7章我国社会主义法的基本理论-国际法
- 2026年大连职业技术学院单招职业技能考试题库附答案
评论
0/150
提交评论