试编写一个判断给定的二叉树是否为完全二叉树的程序过程。

来源:百度知道 编辑:UC知道 时间:2024/09/22 14:56:46

#include "stdafx.h"
#include "iostream"
#define Max 100
using namespace std;

typedef struct Node
{ char data;
struct Node * LChild,*RChild;}BiTNode,*BiTree;

void CreateBiTree(BiTree * bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
* bt=(BiTree)malloc(sizeof(BiTNode));
(* bt)->data=ch;
CreateBiTree(&((* bt)->LChild));
CreateBiTree(&((* bt)->RChild));
}
}

int fullBiTree(BiTree b)
{ //complete binary tree:完全二叉树
//full:满
BiTree queue[Max],p;
int first=0,rear=0,bj=1,cm=1;
if(b!=NULL)
{
rear++;
queue[rear]=b;
while(first!=rear)
{
first++;
p=queue[first];
if(p->LChild==NULL)
{
bj=0;
if(p->RChild!=NULL) cm=0;
}
else
{
cm=bj;
rear++;queue[rear]=p->LChild;
if(p->RChild