Root of AVL Tree

PAT-1066

  • 这是关于AVL即二叉平衡查找树的基本操作,包括旋转和插入
  • 这里的数据结构主要在原来的基础上加上节点的高度信息。
  1. import java.util.*;
  2. /**
  3. * @Author WaleGarrett
  4. * @Date 2020/9/5 10:41
  5. */
  6. public class PAT_1066 {
  7. public static void main(String[] args) {
  8. Scanner scanner=new Scanner(System.in);
  9. int n=scanner.nextInt();
  10. AVLNode root=null;
  11. while(n!=0){
  12. int value=scanner.nextInt();
  13. root=insert(root,value);
  14. // printTree(root);
  15. n--;
  16. }
  17. System.out.println(root.value);
  18. }
  19. static void printTree(AVLNode root){
  20. List<AVLNode> list=new ArrayList<>();
  21. list.add(root);
  22. while(list.size()!=0){
  23. AVLNode temp=list.remove(0);
  24. System.out.print(temp.value+" ");
  25. if(temp.left!=null)
  26. list.add(temp.left);
  27. if(temp.right!=null)
  28. list.add(temp.right);
  29. }
  30. System.out.println();
  31. }
  32. /**
  33. * 顺时针旋转
  34. * @param root
  35. * @return
  36. */
  37. public static AVLNode rightRotate(AVLNode root){
  38. AVLNode temp=root.left;
  39. root.left=temp.right;
  40. temp.right=root;
  41. temp.updateHeight();
  42. root.updateHeight();
  43. return temp;
  44. }
  45. /**
  46. * 逆时针旋转
  47. * @param root
  48. * @return
  49. */
  50. public static AVLNode leftRotate(AVLNode root){
  51. AVLNode temp=root.right;
  52. root.right=temp.left;
  53. temp.left=root;
  54. temp.updateHeight();
  55. root.updateHeight();
  56. return temp;
  57. }
  58. /**
  59. * 向平衡二叉排序树里插入一个节点
  60. * @param value
  61. */
  62. public static AVLNode insert(AVLNode root,int value){
  63. if(root==null){
  64. root=new AVLNode(null,null,value,1);
  65. return root;
  66. }
  67. if(value<root.value){
  68. root.left=insert(root.left,value);//插入根节点的左子树中
  69. root.updateHeight();
  70. if(root.getBalanceFactor()>1){//当前节点不平衡
  71. if(root.left.getBalanceFactor()>0){//LL插入
  72. root=rightRotate(root);
  73. }else if(root.left.getBalanceFactor()<0){//LR插入
  74. root.left=leftRotate(root.left);
  75. root=rightRotate(root);
  76. }
  77. }
  78. }else if(value>root.value){
  79. root.right=insert(root.right,value);
  80. root.updateHeight();
  81. if(root.getBalanceFactor()<-1){//当前节点不平衡
  82. if(root.right.getBalanceFactor()<0){//RR插入
  83. root=leftRotate(root);
  84. }else if(root.right.getBalanceFactor()>0){//RL插入
  85. root.right=rightRotate(root.right);
  86. root=leftRotate(root);
  87. }
  88. }
  89. }
  90. return root;
  91. }
  92. }
  93. class AVLNode{
  94. AVLNode left;
  95. AVLNode right;
  96. int value;
  97. private int height;//该结点的高度
  98. public AVLNode(){
  99. left=right=null;
  100. value=-1;
  101. height=0;
  102. }
  103. public AVLNode(AVLNode left,AVLNode right,int value,int height){
  104. this.value=value;
  105. this.left=left;
  106. this.right=right;
  107. this.height=height;
  108. }
  109. public int getHeight() {
  110. return height;
  111. }
  112. public int getBalanceFactor(){
  113. int leftHeight,rightHeight;
  114. if(left==null)
  115. leftHeight=0;
  116. else leftHeight=left.getHeight();
  117. if(right==null)
  118. rightHeight=0;
  119. else rightHeight=right.getHeight();
  120. return leftHeight-rightHeight;
  121. }
  122. void updateHeight(){
  123. int leftHeight,rightHeight;
  124. if(left==null)
  125. leftHeight=0;
  126. else leftHeight=left.getHeight();
  127. if(right==null)
  128. rightHeight=0;
  129. else rightHeight=right.getHeight();
  130. height=Math.max(leftHeight,rightHeight)+1;
  131. }
  132. }

PAT-1066(Root of AVL Tree)Java语言实现的更多相关文章

  1. PAT 1066 Root of AVL Tree[AVL树][难]

    1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...

  2. 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 ...

  3. PAT 1066 Root of AVL Tree

    #include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; ...

  4. PAT甲级1066. Root of AVL Tree

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

  5. 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 ...

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

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

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

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  8. PTA (Advanced Level) 1066 Root of AVL Tree

    Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...

  9. PAT 甲级 1066 Root of AVL Tree

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

  10. 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 ...

随机推荐

  1. Codeforces Round #481 (Div. 3) F. Mentors (模拟,排序)

    题意:有一个长度为\(n\)的序列\(a\),求这个序列中有多少比\(a_{i}\)小的数,如果某两个位置上的数有矛盾,则不能算小. 题解:用\(pair\)来记录序列中元素的位置和大小,将他们升序排 ...

  2. OpenStack Train版-2.安装keystone身份认证服务

    安装 keystone 认证 mysql -uroot create database keystone; grant all privileges on keystone.* to 'keyston ...

  3. Linux-开机运行流程

    目录 CentOS7开机流程 Linux运行级别 systemd进程管理 systemd的优势 systemd相关文件 systemd启动相关命令 systemd开机自启动相关命令 systemd服务 ...

  4. Spring-cloud-netflix-hystrix

    服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务 畏怯还编写一个eureka-cl ...

  5. 【php代码审计】熊海cms1.0

    0x01 环境安装 1. 熊海cms1.0 (http://js.down.chinaz.com/201503/xhcms_v1.0.rar) 2.seay代码审计工具 3. phpstudy (ph ...

  6. 硬盘测试工具fio用法总结

    一  fio介绍 linux下的一种常用的磁盘测试工具,支持裸盘和文件形式进行测试   二  硬盘测试常用名词 延迟:io的发起到返回写入成功的时间成为延迟,fio中延迟分为lat,slat,clat ...

  7. Vue dynamic component All In One

    Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...

  8. JavaScript var, let, const difference All In One

    JavaScript var, let, const difference All In One js var, let, const 区别 All In One 是否存在 hoisting var ...

  9. Web Design Trends for 2017

    Web Design Trends for 2017 https://www.awwwards.com/web-design-trends-for-2017.html https://usersnap ...

  10. Azure & FaaS in Action

    Azure & FaaS in Action VSCode & Azure azure tenant select subscription Cloud Shell https://a ...