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 ...
随机推荐
- A - Promotions
题目详见http://7xjob4.com1.z0.glb.clouddn.com/3f644de6844d64706eb36baa3a0c27b0 这题是普通的拓扑排序,要把每一层的都保存下来. # ...
- python爬虫笔记Day01
python爬虫笔记第一天 Requests库的安装 先在cmd中pip install requests 再打开Python IDM写入import requests 完成requests在.py文 ...
- Prometheus监控k8s企业级应用
Prometheus架构图 常见的镜像 pod 备注 kube-state-metric 用来收集K8S基本状态信息的监控代理 node-exporter 专门用来收集K8S运算节点基础信息,需要部署 ...
- MySQL——时间、字符串、时间戳相互转换
一.时间转字符串 select data_format(now(),'%Y-%m-%d %H:%i:%s'); 二.时间转时间戳 select unix_timestamp(now()); 三.字符串 ...
- Leetcode(144)-二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 二叉树的前序遍历有递归 ...
- Python+OpenCV+图片旋转并用原底色填充新四角
import cv2 from math import fabs, sin, cos, radians import numpy as np from scipy.stats import mode ...
- how to get svg text tspan x,y position value in js
how to get svg text tspan x,y position value in js <svg xmlns="http://www.w3.org/2000/svg&qu ...
- js 反应&行动
反应 class Reaction { _page = 1; get page() { return this._page; } set page(newValue) { this._page = n ...
- 算法型稳定币USDN是如何保持稳定的?
数据显示,2019年稳定币市场总市值25亿美元,在整个加密货币市场占比 1.3%.可别小瞧了看似微小的1.3%这个数据,它其实是一个庞大的市场.稳定币不仅仅是货币的电子化,它还是一种可编程的加密货币, ...
- Baccarat是如何运用去中心化治理模式的?
区块链的出现,让大家看到了去中心化的可能.去中心化的数字资产从最初的默默无闻,一路起起伏伏发展了十年,逐渐成为了大众认可的价值存储方式.去中心化的金融,使数字资产的生态建设者意识到,即使没有中心化的金 ...