判断是否为AVL树~

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct BTree{
	int Data;
	struct BTree * lchild, *rchild;
}BTree, *Root;

int GetDepth(Root root)
	{
		int depth;
		int ld, rd;
		if(!root)
			return 0;
		else{
			ld = GetDepth(root->lchild);
			rd = GetDepth(root->rchild);		
			depth = ld > rd  ? ld :rd;
			return depth+1;
		}	
	}	
int IsAVL(Root root)
	{
		if(!root)
			return 1;
		int ldepth = GetDepth(root->lchild);
		int rdepth = GetDepth(root->rchild);	
		int abs_depth = abs(ldepth-rdepth);
		return (abs_depth <= 1) && IsAVL(root->lchild)&& IsAVL(root->rchild);		
	} 
Root CreateTree() //版本1  无形参 只有返回值 
	{
		Root T;
		char X;
		scanf("%c",&X);
		if(X == '#')
			T = NULL;  //此处 不return ; 
		else{
			T = (Root)malloc(sizeof(struct BTree));
			T->Data = X; 
			T->lchild = CreateTree();
			T->rchild = CreateTree();
		}	
		
		return T;
	}

void PreTrv(Root T)
	{
		if(T){
			printf("%c ",T->Data);  //  一定要注意 控制格式符 
			PreTrv(T->lchild);
			PreTrv(T->rchild);
			
		}	
	}	
	
int main()
	{
		Root T;
		T = CreateTree();
		//PreTrv(T);

		//printf("\n");
		if(IsAVL(T))
			printf("是AVL");
		else
			printf("不是AVL");	 
		
		return 0;	
	}
	
int CountLeaves(Root T)  //叶节点 个数哇 
	{
		if(T == NULL)
			return 0;
		if(T->lchild == NULL && T->rchild == NULL){
			return 1;
		}	
		return CountLeaves(T->lchild) + CountLeaves(T->rchild);
	}	
int CountNode(Root T)   //求 节点个数 
	{
		if(T == NULL)
			return 0;
		return CountNode(T->lchild) + CountNode(T->rchild) +1;		
	}		
int CountFull(Root T)  //精辟的  求 满节点个数 
	{
		if(T == NULL)
			return 0;
		return (T->lchild && T->rchild) + CountFull(T->lchild) + CountFull(T->rchild);
	}		



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值