创建二叉树.doc_第1页
创建二叉树.doc_第2页
创建二叉树.doc_第3页
创建二叉树.doc_第4页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论