• 【无标题】


    04-树5 Root of AVL Tree

    分数 25

    作者 陈越

    单位 浙江大学

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the root of the resulting AVL tree in one line.

    Sample Input 1:

    1. 5
    2. 88 70 61 96 120

    Sample Output 1:

    70
    

    Sample Input 2:

    1. 7
    2. 88 70 61 96 120 90 65

    Sample Output 2:

    88

     

    主要就是对AVL树的插入维护:

    1. #include
    2. #include
    3. using namespace std;
    4. typedef struct TNode* BinTree;
    5. struct TNode
    6. {
    7. int data;
    8. BinTree lchild,rchild;
    9. int height;
    10. };
    11. BinTree NewNode(int val);
    12. int getHeight(BinTree root);
    13. void updateHeight(BinTree& root);
    14. int getBalanceFactor(BinTree root);
    15. void L(BinTree& root);
    16. void R(BinTree& root);
    17. void Insert(BinTree &BST,int val);
    18. int main()
    19. {
    20. int n;
    21. scanf("%d",&n);
    22. BinTree BST=NULL;
    23. for(int i=0;i
    24. {
    25. int val;
    26. scanf("%d",&val);
    27. Insert(BST,val);
    28. }
    29. printf("%d\n",BST->data);
    30. return 0;
    31. }
    32. BinTree NewNode(int val)
    33. {
    34. BinTree node=new TNode;
    35. node->data=val;
    36. node->height=1;
    37. node->lchild=node->rchild=NULL;
    38. return node;
    39. }
    40. int getHeight(BinTree root)
    41. {
    42. if(root==NULL)
    43. return 0;
    44. else
    45. return root->height;
    46. }
    47. void updateHeight(BinTree& root)
    48. {
    49. root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
    50. }
    51. int getBalanceFactor(BinTree root)
    52. {
    53. return getHeight(root->lchild) - getHeight(root->rchild);
    54. }
    55. void L(BinTree& root)
    56. {
    57. BinTree temp=root->rchild;
    58. root->rchild=temp->lchild;
    59. temp->lchild=root;
    60. updateHeight(root);
    61. updateHeight(temp);
    62. root=temp;
    63. }
    64. void R(BinTree& root)
    65. {
    66. BinTree temp=root->lchild;
    67. root->lchild=temp->rchild;
    68. temp->rchild=root;
    69. updateHeight(root);
    70. updateHeight(temp);
    71. root=temp;
    72. }
    73. void Insert(BinTree &BST,int val)
    74. {
    75. if(!BST)
    76. {
    77. BST=NewNode(val);
    78. return;
    79. }
    80. if(valdata)
    81. {
    82. Insert(BST->lchild,val);
    83. updateHeight(BST);
    84. if(getBalanceFactor(BST)==2)
    85. {
    86. if(getBalanceFactor(BST->lchild)==1)
    87. R(BST);
    88. else if(getBalanceFactor(BST->lchild)==-1)
    89. {
    90. L(BST->lchild);
    91. R(BST);
    92. }
    93. }
    94. }
    95. else
    96. {
    97. Insert(BST->rchild,val);
    98. updateHeight(BST);
    99. if(getBalanceFactor(BST)==-2)
    100. {
    101. if(getBalanceFactor(BST->rchild)==-1)
    102. L(BST);
    103. else if(getBalanceFactor(BST->rchild))
    104. {
    105. R(BST->rchild);
    106. L(BST);
    107. }
    108. }
    109. }
    110. }

  • 相关阅读:
    2022前端面试—CSS篇
    C语言学习笔记(十六)
    【汽修帮手】数据采集,爬虫,根据pdf文件流合并pdf文件
    【AI视野·今日NLP 自然语言处理论文速览 第五十三期】Thu, 12 Oct 2023
    Android 系统865虚拟化集成无源码apk示例
    ora-02063紧接着line起自
    LeetCode刷题(6)
    《向量数据库指南》——向量数据库 大模型的“海马体”
    选择编码节点的最佳数量和位置研究(Matlab代码实现)
    Java之String之equals和intern区别
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126107642