• PAT 甲级 A1066 Root of AVL Tree


    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(平衡二叉)树,依次给出n个插入结点的权,求建完树后的根节点的权值是多少?

    思路:
    直接建立一颗AVL树,AVL树比二叉查找树多了一个高度,还有调平衡。

    1. #include
    2. using namespace std;
    3. const int maxn=55;
    4. int weight[maxn];
    5. int n;
    6. struct node {
    7. int height;
    8. int data;
    9. node* lchild;
    10. node* rchild;
    11. };
    12. node* newNode(int x){
    13. node* Node=new node;
    14. Node->height=1;//新节点初始化高度为1,空结点高度为0;
    15. Node->data=x;
    16. Node->lchild=NULL;
    17. Node->rchild=NULL;
    18. return Node;
    19. }
    20. int getHeight(node* root){
    21. if(root == NULL) return 0;//空树的高度为0;
    22. return root->height;
    23. }
    24. void updateHeight(node* root){//算出该结点的高度;
    25. root->height = max(getHeight(root->lchild),getHeight(root->rchild)) + 1;
    26. return ;
    27. }
    28. int getBalanceFactor(node* root){//算出该结点的平衡因子;
    29. return getHeight(root->lchild) - getHeight(root->rchild);
    30. }
    31. void L(node* &root){//左旋;
    32. node* temp = root->rchild;
    33. root->rchild = temp->lchild;
    34. temp->lchild = root;
    35. updateHeight(root);//旋转后记得更新旋转后结点的高度;
    36. updateHeight(temp);
    37. root = temp;
    38. }
    39. void R(node* &root){
    40. node* temp = root->lchild;
    41. root->lchild = temp->rchild;
    42. temp->rchild = root;
    43. updateHeight(root);
    44. updateHeight(temp);
    45. root = temp;
    46. }
    47. void insert(node* &root,int x){
    48. if(root == NULL){
    49. root = newNode(x);
    50. return;
    51. }
    52. if(x data){
    53. insert(root->lchild,x);
    54. updateHeight(root);
    55. if(getBalanceFactor(root) == 2){
    56. if(getBalanceFactor(root->lchild) == 1){
    57. R(root);
    58. }
    59. else if(getBalanceFactor(root->lchild) == -1){
    60. L(root->lchild);
    61. R(root);
    62. }
    63. }
    64. }
    65. else{
    66. insert(root->rchild,x);
    67. updateHeight(root);
    68. if(getBalanceFactor(root) == -2){
    69. if(getBalanceFactor(root->rchild) == -1){
    70. L(root);
    71. }
    72. else if(getBalanceFactor(root->rchild) == 1){
    73. R(root->rchild);
    74. L(root);
    75. }
    76. }
    77. }
    78. }
    79. //左旋右旋和插入都是需要改变二叉树结构的,所以需要用到引用;
    80. int main( ){
    81. node* root = NULL;
    82. scanf("%d",&n);
    83. for(int i=0;i"%d",&weight[i]);//pat里面数组尽量不要叫做data;
    84. for(int i=0;i
    85. insert(root,weight[i]);
    86. }
    87. printf("%d",root->data);
    88. return 0;
    89. }

     

  • 相关阅读:
    晨曦记账本记账,如何自定义收支类别
    数据湖:数据同步工具NiFi
    博弈论之SG函数
    【机器学习】无监督学习的概念,使用无监督学习发现数据的特点
    myeclipse怎么打开server窗口
    案例分享|企小码为博尔迈生物实现会话存档私有化,助生物医药产业管理优化
    Oracle 存储过程游标遍历查询信息,并打印输出异常信息
    20221204
    轻量级 K8S 环境 安装minikube
    工业交换机常见的故障有哪些?
  • 原文地址:https://blog.csdn.net/m0_51711089/article/details/125885770