这里先判断了二叉树是否为二叉排序树
#include<stdio.h>
#include<stdlib.h>
typedef int status;
typedef struct BiTNode
{
int data;
struct BiTNode* lchild, *rchild;
}BiTNode, *BiTree;
BiTree creat()
{
BiTree root = (BiTree)malloc(sizeof(BiTNode));
int x;
scanf("%d", &x);
if(x==-1)
root = NULL;
else
{
root->data = x;
root->lchild = creat();
root->rchild = creat();
}
return root;
}
//判断二叉树是否是二叉排序树
int judge_BST(BiTree bt)
{
if(bt==NULL) return 1;
if(bt->lchild!=NULL && bt->lchild->data>bt->data) return 0;
if(bt->rchild!=NULL && bt->rchild->data<bt->data) return 0;
if(judge_BST(bt->lchild)&&judge_BST(bt->rchild)) return 1;
else return 0;
}
//判断一棵二叉排序树是否是平衡二叉树
int depth(BiTree bt)
{
if(bt==NULL) return 0;
else
{
int h1 = 1+depth(bt->lchild);
int h2 = 1+depth(bt->rchild);
return (h1>h2?h1:h2);
}
}
int judge_AVL(BiTree bt)
{
if(bt==NULL) return 1;
if(depth(bt->lchild)>depth(bt->rchild)+1 || depth(bt->lchild)<depth(bt->rchild)-1) return 0;
if(judge_AVL(bt->lchild)&&judge_AVL(bt->rchild)) return 1;
else return 0;
}
int main()
{
BiTree bt = creat();
if(!judge_BST(bt)) printf("不是");
else if(judge_AVL(bt)) printf("是的");
else printf("不是");
return 0;
}