完全二叉树判断问题--数据结构

来源:百度知道 编辑:UC知道 时间:2024/06/25 17:36:36
完全二叉树判断问题
基本功能与要求
1 编写一个创建二叉树的算法
2 编写算法判断一个给定的二叉树是否为完全二叉树
3 编写一个测试主函数,要求测试主函数中要包括是完全二叉树和不是完全二叉树两种情况

typedef struct node {
char data;
struct node *leftchile,*rightchile;
}Bitree;

int wanqutree(Bitree *bt)
{
Bitree *p;
Bitree *Q[MAXSIZE]; //定义队列,用于存取树结点的地址
int front,rear;
if(bt==NULL)return;
front=0;
rear=0;
Q[rear++]=bt; //初始化队列
p=bt;
while((front!=rear)&&(p->leftchile!=NULL)&&(p->rightchile!=NULL))
{ //此循环找到其左右孩子均为空的结点
Q[rear++]=p->leftchile;
Q[rear++]=p->rightchile;
p=Q[front];
}
while(front!=rear) //此循环判断上循环找到的结点以后的
{ 结点其左右孩子是不是都为空,如果是
p=Q[front++]; 则返回0,此树是完全二叉树
if((p->leftchile!=NULL)||(p->rightchile!=NULL))
return 0;