AVL树(考研的同学需要注意其定义)的判定(某公司2012年校园招聘笔试题目)

    平衡二叉树又叫AVL(人名的简称)树,在不同的教材中,对AVL树的定义是不同的。考研指定的教材是严奶奶编写的教材,我们来看看该书上(P233)是如何定义的:

       AVL树或者是一棵空树,或者满足以下条件:

      (1). 其左子树和右子树都为AVL树

      (2). 左子树和右子树的高度之差的绝对值不超过1


       然而,有很多教材不是这么定义的,而是把AVL树定义在BST基础之上,比如在李春葆的2012版的《数据结构联考辅导教程》中(P174)就定义在BST基础之上,但是,该书第184页码的judgeAVL函数就没有考虑AVL树的BST性质。


       在这里,我们依然按照严奶奶的定义来办事。


       下面对AVL树进行判定:

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. typedef struct node  
  5. {  
  6.     int key;  
  7.     struct node *lChild, *rChild;  
  8. }Node;  
  9.   
  10. // 生成一个结点  
  11. Node *createNode(int i)  
  12. {  
  13.     Node * q = new Node;  
  14.     q->lChild = NULL;  
  15.     q->rChild = NULL;  
  16.     q->key = i;  
  17.   
  18.     return q;  
  19. }  
  20.   
  21. // 非AVL树  
  22. Node *createNotAVLTree()  
  23. {  
  24.     Node *p1 = createNode(1);  
  25.     Node *p2 = createNode(2);  
  26.     Node *p3 = createNode(3);  
  27.     Node *p4 = createNode(4);  
  28.   
  29.     p1->lChild = p2;  
  30.     p1->rChild = NULL;  
  31.       
  32.     p2->lChild = p3;  
  33.     p2->rChild = p4;  
  34.   
  35.     p3->lChild = p3->rChild = NULL;  
  36.     p4->lChild = p4->rChild = NULL;  
  37.   
  38.     return p1;  
  39. }  
  40.   
  41. // AVL树  
  42. Node *createAVLTree()  
  43. {  
  44.     Node *p1 = createNode(1);  
  45.     Node *p2 = createNode(2);  
  46.     Node *p3 = createNode(3);  
  47.     Node *p4 = createNode(4);  
  48.   
  49.     p1->lChild = p2;  
  50.     p1->rChild = p4;  
  51.       
  52.     p2->lChild = p3;  
  53.     p2->rChild = NULL;  
  54.   
  55.     p3->lChild = p3->rChild = NULL;  
  56.     p4->lChild = p4->rChild = NULL;  
  57.   
  58.     return p1;  
  59. }  
  60.   
  61. void AVLTest(Node *T, int &isAVL, int &height)  
  62. {  
  63.     int bLeft, bRight;  
  64.     int hLeft, hRight;  
  65.     if(NULL == T)  
  66.     {  
  67.         height = 0;  
  68.         isAVL = 1;  
  69.     }  
  70.     else if(NULL == T->lChild && NULL == T->lChild)  
  71.     {  
  72.         height = 1;  
  73.         isAVL = 1;  
  74.     }  
  75.     else  
  76.     {  
  77.         AVLTest(T->lChild, bLeft, hLeft);  
  78.         AVLTest(T->rChild, bRight, hRight);  
  79.         height = (hLeft < hRight ? hRight : hLeft) + 1; // 树高  
  80.         if(abs(hLeft - hRight) < 2) // AVL树的要求  
  81.         {  
  82.             isAVL = bLeft & bRight; // 同时为1才是AVL树  
  83.         }  
  84.         else  
  85.         {  
  86.             isAVL = 0;  
  87.         }  
  88.   
  89.     }     
  90. }  
  91.   
  92. bool isAVLTree(Node *T)  
  93. {  
  94.     int isAVL = 0;  
  95.     int height = 0;  
  96.     AVLTest(T, isAVL, height);  
  97.   
  98.     if(1 == isAVL)  
  99.         return true;  
  100.   
  101.     return false;  
  102. }  
  103.   
  104. void printJudge(Node *T)  
  105. {  
  106.     if(isAVLTree(T))  
  107.         cout << "yes" << endl;  
  108.     else  
  109.         cout << "no" << endl;  
  110. }  
  111.   
  112. int main()  
  113. {  
  114.     Node *T = NULL;  
  115.     printJudge(T);  // yes  
  116.   
  117.     T = createNotAVLTree();  
  118.     printJudge(T);  // no  
  119.   
  120.     T = createAVLTree();  
  121.     printJudge(T);  // yes  
  122.   
  123.     return 0;  
  124. }  

      上面的程序比较巧妙,需要好好体会。另外,需要感叹一下:在写程序的时候,经常把lChild和rChild写错(写反),所以,在以后的程序中,尽量把lChild写成leftChild, 把rChild写成rightChild.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值