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;
    }
}


posted @   Garrett_Wale  阅读(139)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
历史上的今天:
2019-09-05 POJ-2253(最短路变形+dijikstra算法+求解所有路径中所有最长边中的一个最小值)
2019-09-05 POJ-1062(原始dijiksra算法+思维)
2019-09-05 HDOJ-3416(最大流+最短路+ISAP算法+向前星dijikstra算法+如何判断一条边是否在最短路中)
2019-09-05 HDOJ-4725(Dijikstra算法+拆点求最短路)
点击右上角即可分享
微信分享提示