种二叉平衡树,二叉平衡树的左旋转,右旋转

package org.structure.avlTree;

/**
 * 种一棵二叉平衡树(Avl树)
 * @author cjj_1
 * @date 2020-09-02 13:46
 */
public class AvlTreeDemo {
    public static void main(String[] args) {
//        int[] arr ={4,3,6,5,7,8};
        int[] arr={10,7,11,6,8,9};
        AvlTree tree = new AvlTree();
        for(int i:arr){
            tree.add(new Node(i));
        }
        System.out.println(tree.root);
        tree.root.infixOrder();
    }
}
class  AvlTree{
    Node root;
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }
}

class Node{
    Integer value;
    Node left;
    Node right;

    public Node(int value){
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    public  void add(Node node) {
        if (this.value > node.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }

        }
        if(left!=null && right!=null && left.getHight()-right.getHight()>1){
            if(left.right.getHight()>left.left.getHight()){
            //左子树左旋转
                left.leftRotate();
            }
            //向右旋转
            rightRotate();
        }
        if(left!=null && right!=null && right.getHight()-left.getHight()>1){
            if(right.left.getHight() > right.right.getHight()){//如果右子树的左子树高度》右子树的右子树
            //右子树右旋转
                right.rightRotate();
            }
            leftRotate();
        }
    }

    /**
     * 向左旋转
     */
    public  void  leftRotate(){
        Node newNode = new Node(value);
        newNode.left = left;
        newNode.right = right.left;
        value = right.value;
        left = newNode;
        right = right.right;
    }
    /**
     * 向右旋转
     */
    public  void  rightRotate(){
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

    /**
     * 当前节点为根节点的高度
     * @return
     */
    public int getHight(){
        return Math.max(left == null?0:left.getHight(),right ==null?0:right.getHight())+1;
    }
    /**
     * 中序遍历
     */
    public  void infixOrder(){
        if(this.value == null){
            return;
        }
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this.value);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值