#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); }
判断是否为AVL树~
最新推荐文章于 2022-03-16 20:02:26 发布