Root of AVL Tree

PAT-1066

  • 这是关于AVL即二叉平衡查找树的基本操作,包括旋转和插入
  • 这里的数据结构主要在原来的基础上加上节点的高度信息。
import java.util.*;

/**
* @Author WaleGarrett
* @Date 2020/9/5 10:41
*/
public class PAT_1066 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
AVLNode root=null;
while(n!=0){
int value=scanner.nextInt();
root=insert(root,value);
// printTree(root);
n--;
}
System.out.println(root.value);
}
static void printTree(AVLNode root){
List<AVLNode> list=new ArrayList<>();
list.add(root);
while(list.size()!=0){
AVLNode temp=list.remove(0);
System.out.print(temp.value+" ");
if(temp.left!=null)
list.add(temp.left);
if(temp.right!=null)
list.add(temp.right);
}
System.out.println();
} /**
* 顺时针旋转
* @param root
* @return
*/
public static AVLNode rightRotate(AVLNode root){
AVLNode temp=root.left;
root.left=temp.right;
temp.right=root;
temp.updateHeight();
root.updateHeight();
return temp;
} /**
* 逆时针旋转
* @param root
* @return
*/
public static AVLNode leftRotate(AVLNode root){
AVLNode temp=root.right;
root.right=temp.left;
temp.left=root;
temp.updateHeight();
root.updateHeight();
return temp;
}
/**
* 向平衡二叉排序树里插入一个节点
* @param value
*/
public static AVLNode insert(AVLNode root,int value){
if(root==null){
root=new AVLNode(null,null,value,1);
return root;
}
if(value<root.value){
root.left=insert(root.left,value);//插入根节点的左子树中
root.updateHeight();
if(root.getBalanceFactor()>1){//当前节点不平衡
if(root.left.getBalanceFactor()>0){//LL插入
root=rightRotate(root);
}else if(root.left.getBalanceFactor()<0){//LR插入
root.left=leftRotate(root.left);
root=rightRotate(root);
}
}
}else if(value>root.value){
root.right=insert(root.right,value);
root.updateHeight();
if(root.getBalanceFactor()<-1){//当前节点不平衡
if(root.right.getBalanceFactor()<0){//RR插入
root=leftRotate(root);
}else if(root.right.getBalanceFactor()>0){//RL插入
root.right=rightRotate(root.right);
root=leftRotate(root);
}
}
}
return root;
}
}
class AVLNode{
AVLNode left;
AVLNode right;
int value;
private int height;//该结点的高度
public AVLNode(){
left=right=null;
value=-1;
height=0;
}
public AVLNode(AVLNode left,AVLNode right,int value,int height){
this.value=value;
this.left=left;
this.right=right;
this.height=height;
}
public int getHeight() {
return height;
}
public int getBalanceFactor(){
int leftHeight,rightHeight;
if(left==null)
leftHeight=0;
else leftHeight=left.getHeight();
if(right==null)
rightHeight=0;
else rightHeight=right.getHeight();
return leftHeight-rightHeight;
}
void updateHeight(){
int leftHeight,rightHeight;
if(left==null)
leftHeight=0;
else leftHeight=left.getHeight();
if(right==null)
rightHeight=0;
else rightHeight=right.getHeight();
height=Math.max(leftHeight,rightHeight)+1;
}
}

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. 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)

    由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...

  2. VUE 3.0 初体验之路

    在2020年9月中旬,vue.js发布了3.0正式版,在不久的将来,VUE3.0 也终将成为大前端的必然趋势, 环境搭建 node 版本要求: Node.js8.9 或更高版本 ,输入 node -v ...

  3. Web安全之SQL注入(原理,绕过,防御)

    首先了解下Mysql表结构 mysql内置的information_schema数据库中有三个表非常重要1 schemata:表里包含所有数据库的名字2 tables:表里包含所有数据库的所有的表,默 ...

  4. windows7 更新失败,无法开机处理方法

    记录一次今天同事笔记本无法开机的故障处理 windows7系统,安装更新失败,无法开机,卡在配置更新界面 处理方法,强制关机,开机按F8,进入安全模式,安全模式还会卡在配置更新界面,但是卡一会会进入系 ...

  5. centos7+腾讯云服务器搭建wordpress

    title: centos7+腾讯云服务器搭建wordpress date: 2020-03-04 categories: web tags: [wordpress] 分两部分:1.搭建LEMP环境 ...

  6. leetcode8 字符串转换整数

    <cctype> isdigit(char) 问题:在做乘法,加法前,先判断是否溢出 &&优先级大于== 然后教训: 考虑情况不周.比如3.14这样 然后解决办法 多自己搞 ...

  7. Leetcode(83)-删除排序链表中的重复元素

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3-&g ...

  8. CSS字体大小: em与px、pt、百分比之间的对比

      CSS样式最混乱的一个方面是应用程序中文本扩展的font-size属性.在CSS中,你可以用四个不同的单位度量来显示在web浏览器中的文本大小.这四个单位哪一种最适合Web? 这个问题引起了广泛的 ...

  9. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  10. Chrome & targetText

    Chrome & targetText target text http://www.ruanyifeng.com/blog/2019/03/weekly-issue-47.html http ...