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 ...
随机推荐
- 【uva 1610】Party Games(算法效率--构造 dfs)
题意:有一个N个字符串(N≤1000,N为偶数)的集合,要求找一个长度最短的字符串(可不在集合内)S,使得集合中恰好一半的串小于等于S,另一半大于S.如果有多解,要求输出字典序最小的解. 解法:本来我 ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a lin ...
- Python 往Excel写数据
一.需求描述: 1.一张人员信息表中生成人员信息,某些列的字段要进行递增操作: 2.一个组织节点下存在1000人的限制要求: 3.一张Excel表格生成45000条数据: 二.Excel表格的表头如下 ...
- c#记两个变量进行值交换
今天腊月二十九啦,无心上班,专注划水.然后就在那里翻帖子消磨时光. 看到了这样一个问题,有人提问为什么 a=b+(b=a)*0 ??? 第一眼看上去,我也有点蒙,仔细推敲了一下,嗯~的确是交换了 ...
- RuntimeError already started
Env: os: Ubuntu python3 pytorch vscode Desc 在上述环境中运行A3C多进程模型,使用命令行时没问题,使用vscode时出现 'RuntimeError: al ...
- 利用设置新数据存储结构解决vue中折叠面板双向绑定index引起的问题
问题背景是,在进行机器性能可视化的前端开发时,使用折叠面板将不同机器的性能图表画到不同的折叠面板上去.而机器的选择利用select下拉选项来筛选. 由于在折叠面板中,通过 如下v-model双向绑定了 ...
- Pycharm+任务栏悬浮+docked mode
先点下所想改变模式的模块, 然后: Window -> Activate tool window -> docked mode.
- Ubuntu16.04+wineQQ+解决版本过低
[参考1:] http://blog.csdn.net/sinat_32079337/article/details/72771078? [参考2:] http://blog.csdn.net/qq_ ...
- 使用 Promise 实现请求自动重试
使用 Promise 实现请求自动重试 "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqf ...
- Xcode 切换 iOS 模拟器的 Dark 模式
Xcode 切换 iOS 模拟器的 Dark 模式 SwiftUI // // ContentView.swift // MemorizeGame // // Created by 夏凌晨 on 20 ...