1066 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 (≤) which is the total number of keys to be inserted. Then Ndistinct 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:

  1. 70

Sample Input 2:

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

Sample Output 2:

  1. 88

题意:

  1. 将输入调整为平衡二叉树(AVL),输出根结点元素

题解:

  1. 判断插入结点对现有结点的平衡因子的影响,进而进行LLLRRLRR旋转
    假设三个结点连接关系为A->B->C,C为新插入结点并使得A的平衡因子==2
    CA的左孩子的左子树上,则对AB进行LL旋转
    CA的左孩子的右子树上,则对ABC进行LR旋转,可分解为首先对BC进行RR旋转,再对AC进行LL旋转
    CA的右孩子的右子树上,则对AB进行RR旋转
    CA的右孩子的左子树上,则对ABC进行RL旋转,可分解为首先对BC进行LL旋转,再对AC进行RR旋转

平衡二叉树选择详解:

4种平衡调整如下(结点的数字仅作标记作用):

(图中数字仅用于区分节点的不同,不用来表示节点的数值大小)

①LL:

对于根节点:左边比右边多

对于左节点:左边比右边多

右单旋转

  

②RR:

对于根节点:边比左边多

对于节点:边比边多

左单旋转

  

③LR平衡旋转:

对于根节点:左边比右边多

对于节点:右边比左边多

先左后右(先处理节点,再处理节点)

  

④RL平衡旋转:

对于根节点:右边比左边多

对于右节点:左边比右边多

先右后左(先处理右节点,再处理节点)

  

AC代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int n;
  5. struct node{
  6. int data;
  7. node *lchild,*rchild;
  8. };
  9. node *Newnode(int x){//新建一个结点
  10. node* newnode=new node;
  11. newnode->data=x;
  12. newnode->lchild=newnode->rchild=NULL;
  13. return newnode;
  14. }
  15. int Height(node* root){//返回高度
  16. if(root==NULL) return ;
  17. else return max(Height(root->lchild),Height(root->rchild))+;
  18. }
  19. int getbalance(node* root){//检查是否平衡
  20. return Height(root->lchild)-Height(root->rchild);
  21. }
  22. void R(node*&root){//右旋
  23. //左节点成为根节点
  24. node* temp=root->lchild;
  25. root->lchild=root->rchild;//根的左边换成了左节点的右节点
  26. temp->rchild=root;//根自己成为了原来左节点的右节点
  27. root=temp;
  28. }
  29. void L(node*&root){//左旋
  30. //右节点成为根节点
  31. node *temp=root->rchild;
  32. root->rchild=temp->lchild;//根的右边换成了右节点的左节点
  33. temp->lchild=root;//根自己成为了原来右节点的左节点
  34. root=temp;
  35. }
  36. void insert(node*&root,int x){
  37. if(root==NULL){
  38. root=Newnode(x);
  39. return;
  40. }
  41. if(x<root->data){
  42. insert(root->lchild,x);
  43. if(getbalance(root)==){//左边必比右边高2
  44. if(getbalance(root->lchild)==){//左节点的左边比右边高1
  45. R(root);//右单旋
  46. }else if(getbalance(root->lchild)==-){//左节点的右边比左边高1
  47. L(root->lchild);//对于左节点左旋
  48. R(root);//再跟节点右旋
  49. }
  50. }
  51. }else{
  52. insert(root->rchild,x);
  53. if(getbalance(root)==-){//右边必比左边高2
  54. if(getbalance(root->rchild)==){//右节点的左边比右边高1
  55. R(root->rchild);//对于右节点右旋
  56. L(root);//再跟节点左旋
  57. }else if(getbalance(root->rchild)==-){//右节点的右边比左边高1
  58. L(root);//左单旋
  59. }
  60. }
  61. }
  62. }
  63. int main(){
  64. scanf("%d",&n);
  65. node *root = NULL;
  66. for(int i=;i<n;i++){
  67. int x;
  68. scanf("%d",&x);
  69. insert(root,x);
  70. }
  71. printf("%d",root->data);//输出处理好的平衡二叉树的根节点
  72. return ;
  73. }

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***的更多相关文章

  1. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  2. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  3. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  4. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  5. PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  6. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  7. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  8. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  9. PAT 1066. 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 sub ...

随机推荐

  1. memset()函数的使用

    1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...

  2. 图中长度为k的路径的计数

    题意 给出一个有向图,其中每条边的边长都为1.求这个图中长度恰为 $k$ 的路劲的总数.($1 \leq n \leq 100, 1 \leq k\leq 10^9$) 分析 首先,$k=1$ 时答案 ...

  3. Java【基础学习】 之调用构造方法顺序【坑】

    解释:这里的super()仅仅是用来占位的,实际上,必须是严格按照分层初始化的过程:1.先初始化父类X的成员变量,即初始化成员变量Y,打印出:Y2.初始化父类X的构造方法,打印出:X3.父类初始化完成 ...

  4. 五.划分LVM逻辑卷

    作用:    1.整合分散的空间    2.空间可以进行扩大   零散空闲存储 ---- 整合的虚拟磁盘 ---- 虚拟的分区   由众多的物理卷(PV)组合成卷组(VG),从卷组中划分多个逻辑卷(L ...

  5. RookeyFrame 线下 添加Model

    1.在Model层添加一个类,继承BaseEntity,如: (将就demo里面的类改了一下) using Rookey.BusSys.Model.Base; using Rookey.BusSys. ...

  6. NOIP2015 D1 解题报告

    T1 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1 ...

  7. AtCoder Grand Contest 003题解

    传送门 \(A\) 咕咕 const int N=1005; char s[N];int val[N],n; int main(){ scanf("%s",s+1),n=strle ...

  8. 洛谷P5017摆渡车

    题目 一道做法多种多样的题,DP做法的状态也很多. 我用\(dp[i]\)表示在第i秒发车的时间和,然后dp方程就很好写了 \(dp[i] = dp[j] + i车的等待时间\)j属于i-2m ~ i ...

  9. 【一起来烧脑】一步学会CSS3体系

    [外链图片转存失败(img-yfi1VPyy-1563434266398)(https://upload-images.jianshu.io/upload_images/11158618-fc8784 ...

  10. 1054 The Dominant Color (20)(20 分)

    Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...