• 8 AVL树的判断---来源刘H同学


    来源刘H同学

    来源刘H同学

    来源刘H同学

    来源刘H同学

    AVL树的判断

    作者: 冯向阳时间限制: 1S章节: 课程设计

    问题描述 :

    目的:设计并实现一个算法Judge_AVL,用来判断1棵给定的二叉排序树是否是平衡二叉树。

    提示:

    (1)若一棵二叉排序树中每个结点的左、右子树的深度之差(平衡因子)的绝对值不超过1,则称这样的二叉树为平衡二叉树(AVL树)。

    (2)在小组任务12的基础上,首先判断该二叉树是否是一棵二叉排序树。如不是,则可直接给出false(不是AVL树);如是,进入(3)

    (3)使用任意的遍历方式对该二叉树进行遍历(测试数据使用非递归的后序遍历得到,参考个人任务12),在visit函数中计算访问的结点的平衡因子。参照个人任务13,但要稍作修改(不要通过数据元素的值,而是直接通过遍历到的结点地址来定位)。

    (4)如有结点的平衡因子的绝对值超过1,则可给出false;如所有结点的平衡因子的绝对值都不超过1,则可给出true。

    注意:结点的数据域的类型为string

    参考函数原型:

    (1)判断是否是AVL树

    template
    bool Judge_AVL(BinaryTree &T);

    (2)visit函数

    template
    bool visit(BinaryTreeNode * root){
        int lheight, rheight, flag;   

        if(!root) return false;
        else{

            cout<data<<" ";
            lheight = GetBinaryTreeHeight_Cursive(root->LChild);
            rheight = GetBinaryTreeHeight_Cursive(root->RChild);
            flag = lheight - rheight;
            cout<         if( flag < -1 || flag > 1) return false;
        }
       
        return true;
    }   

    输入说明 :

    第一行:表示无孩子或指针为空的特殊分隔符

    第二行:二叉树的先序序列(结点元素之间以空格分隔)

    输出说明 :

                  如不是二叉排序树,直接输出false

                  否则,按照后序遍历

                   结点1的数据域的值 平衡因子(以空格分隔)

                   ....

                  (如遇到结点的平衡因子不在范围内,在下一行显示false,终止)

                  (如所有结点的平衡因子都在范围内,在下一行显示true,终止)

    -----------

    #
    06 04 02 01 # # 03 # # 05 # # 10 07 # 08 # 09 # # #

    ---------------

    01 0
    03 0
    02 0
    05 0
    04 1
    09 0
    08 -1
    07 -2
    false

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. bool flag = true;
    7. bool flag2 = 1;
    8. int k;
    9. string c[1000];
    10. vectordepart_string(string str)
    11. {
    12. vectorans;
    13. stringstream s(str);
    14. string temp;
    15. while (s >> temp)
    16. {
    17. ans.push_back(temp);
    18. }
    19. return ans;
    20. }
    21. template<class ElemType>
    22. struct Node
    23. {
    24. public:
    25. ElemType data;
    26. Node* lchild;
    27. Node* rchild;
    28. };
    29. template<class ElemType>
    30. class BinaryTree
    31. {
    32. public:
    33. Node* root;
    34. void setroot(Node* head)
    35. {
    36. root = head;
    37. }
    38. Node* createbinarytree(vectorans, ElemType empty, int& n)
    39. {
    40. ElemType x = ans[n++];
    41. if (x == empty)
    42. {
    43. return NULL;
    44. }
    45. else
    46. {
    47. Node* temp = new Node ;
    48. temp->data = x;
    49. temp->lchild = createbinarytree(ans, empty, n);
    50. temp->rchild = createbinarytree(ans, empty, n);
    51. return temp;
    52. }
    53. }
    54. bool postorderphyz(Node* root)
    55. {
    56. if (root == NULL)
    57. return false;
    58. Node* temp = root;
    59. if (temp)
    60. {
    61. postorderphyz(temp->lchild);
    62. postorderphyz(temp->rchild);
    63. int phyz = GetBinaryTreeHeight_Cursive(temp->lchild) - GetBinaryTreeHeight_Cursive(temp->rchild);
    64. if (flag)
    65. cout << temp->data << " " << phyz << endl;
    66. if (abs(phyz) > 1)
    67. {
    68. flag = false;
    69. cout << "false" << endl;
    70. exit(0);
    71. }
    72. }
    73. return true;
    74. }
    75. void InOrderTraverse(Node* T)
    76. {
    77. if (T)
    78. {
    79. InOrderTraverse(T->lchild);
    80. c[++k] = T->data;
    81. if (k > 1 && c[k - 1] > T->data)
    82. {
    83. flag2 = 0;
    84. return;
    85. }
    86. InOrderTraverse(T->rchild);
    87. }
    88. }
    89. };
    90. template<class ElemType>
    91. int GetBinaryTreeHeight_Cursive(Node* root)
    92. {
    93. int ldepth, rdepth;
    94. if (!root)
    95. {
    96. return 0;
    97. }
    98. else
    99. {
    100. ldepth = GetBinaryTreeHeight_Cursive(root->lchild);
    101. rdepth = GetBinaryTreeHeight_Cursive(root->rchild);
    102. return (ldepth > rdepth) ? (ldepth + 1) : (rdepth + 1);
    103. }
    104. }
    105. template<class ElemType>
    106. bool Judge_AVL(BinaryTree T)
    107. {
    108. Node* root = T.root;
    109. int left = GetBinaryTreeHeight_Cursive(root->lchild);
    110. int right = GetBinaryTreeHeight_Cursive(root->rchild);
    111. if (abs(left - right) <= 1)
    112. {
    113. return true;
    114. }
    115. else
    116. {
    117. return false;
    118. }
    119. }
    120. int main()
    121. {
    122. string empty;
    123. string s;
    124. getline(cin, empty);
    125. getline(cin, s);
    126. vectorans = depart_string(s);
    127. BinaryTreebt;
    128. int n = 0;
    129. bt.setroot(bt.createbinarytree(ans, empty, n));
    130. bt.InOrderTraverse(bt.root);
    131. if (!flag2)
    132. {
    133. cout << "false";
    134. return 0;
    135. }
    136. bool o = Judge_AVL(bt);
    137. bt.postorderphyz(bt.root);
    138. if (o && flag)
    139. {
    140. cout << "true";
    141. }
    142. return 0;
    143. }

  • 相关阅读:
    go语言GoFrame+Vue+ElementUI后台管理搭建教程
    QByteArray,char转float的方法以及计算机大小端判断
    docker-compose 搭建 单机版ELK
    138.【JUC并发编程- 03】
    Rust之常用集合(二):字符串(String)
    PDF 如何高效的转换成 markdown
    Prometheus字段解析
    优化您的Spring应用程序:缓存注解的精要指南
    ELECTRA:Pre-training Text Encoders as Discriminators Rather Than Generators
    CMake输出编译时间信息
  • 原文地址:https://blog.csdn.net/Ultravioletrays/article/details/126799798