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 ...
随机推荐
- Codeforces Round #481 (Div. 3) F. Mentors (模拟,排序)
题意:有一个长度为\(n\)的序列\(a\),求这个序列中有多少比\(a_{i}\)小的数,如果某两个位置上的数有矛盾,则不能算小. 题解:用\(pair\)来记录序列中元素的位置和大小,将他们升序排 ...
- OpenStack Train版-2.安装keystone身份认证服务
安装 keystone 认证 mysql -uroot create database keystone; grant all privileges on keystone.* to 'keyston ...
- Linux-开机运行流程
目录 CentOS7开机流程 Linux运行级别 systemd进程管理 systemd的优势 systemd相关文件 systemd启动相关命令 systemd开机自启动相关命令 systemd服务 ...
- Spring-cloud-netflix-hystrix
服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务 畏怯还编写一个eureka-cl ...
- 【php代码审计】熊海cms1.0
0x01 环境安装 1. 熊海cms1.0 (http://js.down.chinaz.com/201503/xhcms_v1.0.rar) 2.seay代码审计工具 3. phpstudy (ph ...
- 硬盘测试工具fio用法总结
一 fio介绍 linux下的一种常用的磁盘测试工具,支持裸盘和文件形式进行测试 二 硬盘测试常用名词 延迟:io的发起到返回写入成功的时间成为延迟,fio中延迟分为lat,slat,clat ...
- Vue dynamic component All In One
Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...
- 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 ...
- Web Design Trends for 2017
Web Design Trends for 2017 https://www.awwwards.com/web-design-trends-for-2017.html https://usersnap ...
- Azure & FaaS in Action
Azure & FaaS in Action VSCode & Azure azure tenant select subscription Cloud Shell https://a ...