PAT-1066(Root of AVL Tree)Java语言实现
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语言实现的更多相关文章
- 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, ...
- 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 ...
- PAT 1066 Root of AVL Tree
#include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- 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 ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
随机推荐
- hdu4291 A Short problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)
题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- 1.rabbitmq 集群安装及负载均衡设置
标题 : 1.rabbitmq 集群安装及负载均衡设置 目录 : RabbitMQ 序号 : 1 vim /etc/pam.d/login #对于64位系统,在文件中添加如下行 session req ...
- 关于vmwaretools
今天安装Ubuntu16.04-i386,vmware15.5,使用的快速安装,然后安装vmwaretools出现问题:无法复制粘贴,顶部管理"重新安装vmware-tools"选 ...
- Gym 101464C - 计算几何+二分(uva1463)
不是很难,但是我觉得对代码能力的要求还是挺高的. 注意模块化. 因为是浮点数,所以二分用的很多很多. 参考 https://blog.csdn.net/njupt_lyy/article/detail ...
- BZOJ 3676 回文串(回文树)题解
题意: 一个回文的价值为长度 * 出现次数,问一个串中的子串的最大回文价值 思路: 回文树模板题,跑PAM,然后计算所有节点出现次数. 参考: 回文串问题的克星--Palindrome Tree(回文 ...
- FTP 与 SSH 的安全性对比, 以及FTP,SSH,SFTP,SCP 的关系简单解析!
FTP 与 SSH 的安全性对比? ftP: http://baike.baidu.com/subview/369/6149695.htm TCP/IP协议中,FTP标准命令TCP端口号为21,Por ...
- JavaScript 设计模式: 发布者-订阅者模式
JavaScript 设计模式: 发布者-订阅者模式 发布者-订阅者模式 https://github.com/Kelichao/javascript.basics/issues/22 https:/ ...
- Vue 3 In Action
Vue 3 In Action $ yarn add vue https://v3.vuejs.org demos refs https://v3.vuejs.org/guide/migration/ ...