数据结构之判断给定的一棵二叉树是否是完全二叉树

#include <iostream>


using namespace std;


struct BitNode 
{
char data;
BitNode *lchild;
BitNode *rchild;
};


struct LinkNode
{
BitNode *data;
LinkNode *next;
};


struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};


void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;
Q->front->next=NULL;
Q->rear=Q->front;
}


bool isEmpty(LinkQueue *Q)
{
return Q->rear==Q->front;
}


void push(LinkQueue *&Q,BitNode *e)
{
LinkNode *p=new LinkNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}


void pop(LinkQueue *&Q)
{
if(!isEmpty(Q))
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}
}


BitNode *get(LinkQueue *Q)
{
if(!isEmpty(Q))
return Q->front->next->data;
}


void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}


void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}
}


bool isCompleteTree(BitNode *T)
{
LinkQueue *Q;
init(Q);
push(Q,T);
while(1)
{
BitNode *t=get(Q);
if(t==NULL)
break;
pop(Q);
push(Q,t->lchild);
push(Q,t->rchild);
}
while(!isEmpty(Q))
{
BitNode *t=get(Q);
if(t!=NULL)
return false;
pop(Q);
}
return true;
}


int main()
{
BitNode *T;
create(T);

        

        if(isCompleteTree(T)) 
cout<<"ok"<<endl;
        else                  
cout<<"no"<<endl;



return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值