第5章 树与二叉树
5.3 二叉树的遍历和线索二叉树
综合应用题 第7题
![在这里插入图片描述](https://img-blog.csdnimg.cn/3c5798b9992748b8bb0e02987ba846f4.png)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MaxSize 100
using namespace std;
typedef int ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
bool InitBiTree(BiTree &B){
B=NULL;
return true;
}
BiTree BiTreeCreatT(ElemType *a,BiTree &B) {
InitBiTree(B);
B=(BiTree)malloc(sizeof(BiTNode));
B->lchild=NULL;
B->rchild=NULL;
B->data=a[0];
BiTNode *p,*s;
s=B;
p = (BiTNode *)malloc(sizeof(BiTNode));
p->data = a[1];
p->lchild=NULL;
p->rchild=NULL;
s->lchild=p;
p = (BiTNode *)malloc(sizeof(BiTNode));
p->data = a[2];
p->lchild=NULL;
p->rchild=NULL;
s->rchild=p;
s=s->lchild;
p = (BiTNode *)malloc(sizeof(BiTNode));
p->data = a[3];
p->lchild=NULL;
p->rchild=NULL;
s->rchild=p;
return B;
}
typedef struct{
BiTNode* queue[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue &Q)
{
Q.rear=Q.front=-1;
}
int EnQueuel(SqQueue &Q,BiTNode* x)
{
if(Q.rear==MaxSize)
return 0;
Q.queue[Q.rear]=x;
Q.rear++;
return 1;
}
BiTNode* DeQueuel(SqQueue &Q)
{
BiTNode* x;
if(Q.front==Q.rear)
return 0;
x=Q.queue[Q.front];
Q.front++;
return x;
}
bool QueueEmpty(SqQueue &Q)
{
if(Q.front==Q.rear)
return true;
else
return false;
}
bool IsComplete(BiTree &T)
{
SqQueue Q;
InitQueue(Q);
if(!T)
return true;
BiTNode *p=T;
EnQueuel(Q,p);
while(!QueueEmpty(Q))
{
p=DeQueuel(Q);
if(p)
{
EnQueuel(Q,p->lchild);
EnQueuel(Q,p->rchild);
}
else
while(!QueueEmpty(Q))
{
p=DeQueuel(Q);
if(p)
return false;
}
}
return true;
}
int main() {
int a[4]={1,2,3,5};
BiTree B;
BiTreeCreatT(a,B);
cout<<"是否是完全二叉树?"<<endl;
if(IsComplete(B))
cout<<"是完全二叉树"<<endl;
else
cout<<"不是完全二叉树"<<endl;
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/1d10b7027a5d4b9f897c95f1bf9ebb6c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAb25lIGRheV8xOTAzMDQ=,size_16,color_FFFFFF,t_70,g_se,x_16)