判斷完全二叉樹

判斷完全二叉樹

 

#include<stdio.h>
#include<malloc.h>
#define OVERFLOW 0;
#define ERROR O;
#define OK 1;
typedef char TElemType;
typedef int Status;
typedef struct BiTNode{
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
	char ch;
	fflush(stdin); 
	scanf("%c",&ch); 
	fflush(stdin); 
	if(ch==' ')T=NULL;
	else{		
		if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))return OVERFLOW;
		T->data=ch;
		printf("請輸入 %c 的左節點:\n",T->data);
		CreateBiTree(T->lchild);
		printf("請輸入 %c 的右節點:\n",T->data);
		CreateBiTree(T->rchild);
	}
}
int fullBiTree(BiTree T) 
{
	BiTree queue[100],p;
	int first=0,rear=0,bj=1,cm=1; 
	if(T!=NULL) 
	{
	  	rear++; 
	  	queue[rear]=T; 
	  	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==NULL) bj=0; 
			    else 
			    { 
				     rear++; 
				     queue[rear]=p->rchild; 
			    } 
   			} 
	   	} 
  		return cm; 
	} 
 return 1; 
} 
int main()
{
	BiTree T;
	printf("請輸入樹根:\n"); 
	CreateBiTree(T);
	int cm=fullBiTree(T); 
	if(cm)printf("此二叉樹爲完全二叉樹\n"); 
	else printf("此二叉樹不是完全二叉樹\n"); 
	return 0;	
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章