编写算法判别给定二叉树是否为完全二叉树_第1页
编写算法判别给定二叉树是否为完全二叉树_第2页
编写算法判别给定二叉树是否为完全二叉树_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、名称:6. 49/功能:编写算法判别给定二又树是否为完全二又树/作者:薛小超日期:2012.10.26f /甘include using namespace std;typedef char TElemType;typedef struct BiTNode/定义声明结构体 BiTNode(TElemType data;BiTNode *lchild, *rchild;*BiTree;typedef struct QNode/定义声明结构体 QNode(BiTree data;QNode *next;*LQueuePtr;struct LQueue(LQueuePtr front, rear;;

2、void LQueuelnit (LQueue&Q) /初始化队列 (Q. front=Q. rear=new QNode;Q. front-next=NULL:void Enqueue (LQueue&Q, BiTree e)/入队(LQueuePtr p;p=new QNode;p-data=e;Q.rear-next=p;Q.rear=p;p-next=NULL;bool Dequeue (LQueue&Q, BiTree &e)出队 (LQueuePtr p;if (Q. front=Q. rear)return false;p=Q front;Q.front=p-next:e=Q.

3、front-data;delete p;return true;void visit (TElemType z)(coutz;void CreateBiTree(BiTree &T,char S,int &i)(i+;if(Si= tf)(T二NULL;return;)T=new BiTNode;T-data=Si;CreateBiTree(T-lchild,S, i);CreateBiTree(T-rchild,S, i);void CreateBiTree(BiTree &T, char S)/创建二叉树(int i=-l;CreateBiTree (T,S,i);void preorde

4、rlists (BiTree T, void visit (TElemType)/广义表输出二义树(if(!T)(return;)visit(Tdata);if(T-lchild!=NULL|T-rchild!=NULL)(cout,(;preorderlists(T-lchild, visit);cout,,;preorderlists(T-rchild, visit);cout,);bool Complete (BiTree T)/层次遍历二义树是否为完全二又树 (bool f二false;BiTree p=T;LQueue Q;LQueuelnit (Q);if(!T)return false;Enqueue(Q, p);while(Dequeue(Q, p)(if(!p)f=true;else if (f)return false;else(Enqueue(Q,p-lchild);Enqueue(Q, p-rchild);)return true;主函数int main ()(BiTree T1;/char a = ABttD#G#FmT;char =CreateBiTree (Tl, a);创建二义树cout*又树为:,zendl:preorderlists (Tl, visit) ;/广义表输出二又树coutendl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论