Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2788502
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-09-13 15:14:35

平衡二叉树,又称AVL树。它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的高度之差之差的绝对值不超过1。

问题:判断一个二叉排序树是否是平衡二叉树这里是二叉排序树的定义解决方案:根据平衡二叉树的定义,如果任意节点的左右子树的深度相差不超过1,那这棵树就是平衡二叉树。首先编写一个计算二叉树深度的函数,利用递归实现。

下面是利用递归判断左右子树的深度是否相差1来判断是否是平衡二叉树的函数:

点击(此处)折叠或打开

  1. #include
  2. #include
  3. #include
  4. #include
  5. using namespace std;
  6. typedef int datatype;
  7. int countLeaves,nodes;
  8. typedef struct node
  9. {
  10.     datatype data;
  11.     struct node *lchild,*rchild;
  12. }bintnode;
  13. typedef bintnode *bintree;

  14.     void createbintree(bintree *t)
  15.     {
  16.     //输入二叉树的先序遍历序列,创建二叉链表
  17.         int ch;
  18.         //ch=getchar();
  19.         scanf("%d",&ch);
  20.         if(ch==-1)
  21.             *t=NULL;//如果读入空格字符,创建空树,T是指向指针的指针,*t就相当于一个bintree指针,专门指向bintnode;
  22.         else
  23.         {
  24.             (*t)=(bintnode*)malloc(sizeof(bintnode));
  25.             (*t)->data=ch;
  26.             createbintree(&(*t)->lchild);//根据先序遍历,继续创建左子树,让客户端继续输入
  27.             createbintree(&(*t)->rchild);//创建完左子树,继续创建右子树
  28.         } //递归调用,自动返回
  29.     }
  30.     
  31.     

  32.     //求树的深度
  33.     int deepLength(bintree t)
  34.     {
  35.         if(t==NULL)
  36.             return 0;
  37.         //递归返回左右子树较大的就是
  38.         else
  39.         {
  40.             int ldeep=deepLength(t->lchild);
  41.             int rdeep=deepLength(t->rchild);
  42.             if(ldeep
  43.                 return rdeep+1;
  44.             else
  45.                 return ldeep+1;
  46.         }
  47.     }
  48.     
  49.     //递归,逐个结点判断,返回值,不平衡:0,平衡:1
  50.     int isBalance(bintree t)
  51.     {
  52.         if(t==NULL)
  53.             return 1;//这个点是null没有左右子树,认为是平衡
  54.         int ldepth=deepLength(t->lchild);//左子树的高度
  55.         int rdepth=deepLength(t->rchild);//右子树的高度
  56.         int dis=ldepth-rdepth;
  57.         if(dis>1|| dis<-1)
  58.             return 0;//不平衡,直接跳出递归,返回0
  59.         else
  60.             return (isBalance(t->lchild) && isBalance(t->rchild)) ;//平衡,继续看它的左右子树
  61.     }
  62.     
  63. int main()
  64. {

  65. /*
  66. 这里的输入要严格按照正确的顺序才能结束.这里要用到二叉树的一个性质,
  67. 就是说对于有n个节点的二叉树,就有n+1个空域,在这里即为如果你输入了n个
  68. 元素,那么一定要有n+1个#才会结束迭代过程.
  69. */
  70.     bintree t=NULL;
  71.     createbintree(&t);//这样才能改变T,指向指针的指针

  72.     int height=deepLength(t);
  73.     printf("\n树的高度: %d",height);
  74.     if(isBalance(t))
  75.         printf("\n平衡");
  76.     else
  77.         printf("\n不平衡");

  78.     printf("\n");
  79.     getchar();
  80.     return 0;
  81. }



  82. /*
  83. 平衡:
  84. 1 2 4 8 -1 -1 9 -1 -1 5 10 -1 -1 11 -1 -1 3 6 -1 -1 7 -1 -1

  85. 树的高度: 4
  86. 平衡
  87. Press any key to continue


  88. 不平衡:
  89. 1 2 -1 4 8 -1 -1 9 -1 -1 5 10 -1 -1 11 -1 -1
  90. 树的高度: 4
  91. 不平衡
  92. Press any key to continue
  93. */

阅读(1269) | 评论(0) | 转发(0) |
0

上一篇:非递归访问二叉树

下一篇:ARP and RARP

给主人留下些什么吧!~~