平衡二叉树(AVL)

package com.monster.AVL;

/**
 * @author Monster
 * @version v1.0
 * @time 04-17-2021 11:12:49
 * @description:
 */
public class AVLTreeDemo {
    public static void main(String[] args) {
        //int[] arr = {4, 3, 6, 5, 7, 8};
        //int[] arr = {10, 8 ,12, 7, 9, 6};
        int[] arr = {7, 6, 10, 8, 11, 9};
        AVLTree avlTree = new AVLTree();
        for (int data : arr) {
            avlTree.add(new Node(data));
        }

        System.out.println("左子树的高度为:" + avlTree.leftTreeHeight());
        System.out.println("右子树的高度为:" + avlTree.rightTreeHeight());
    }
}

class AVLTree {
    private Node root;

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public int leftTreeHeight() {
        if (root == null) {
            return 0;
        }
        return root.leftTreeHeight();
    }

    public int rightTreeHeight() {
        if (root == null) {
            return 0;
        }
        return root.rightTreeHeight();
    }
}

class Node {
    int data;
    Node left;
    Node right;

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

    // 左旋转方法
    public void leftRotate() {

        // 创建新的节点,保存当前节点的值,用于新树的左子节点
        Node newNode = new Node(this.data);
        // 此方法为左旋转法,所以右子树的高度大于左子树,故把右子树的左子节点当做新节点的右子节点
        newNode.left = this.left;
        newNode.right = this.right.left;
        // 因为要用右子节点当做新树的根节点,所以把当前树的右子节点的值赋值给当前节点的值,
        this.data = this.left.data;
        this.left = newNode;
        this.right = this.right.right;
    }

    // 右旋转方法
    public void rightRotate() {

        Node newNode = new Node(this.data);
        newNode.right = this.right;
        newNode.left = this.left.right;

        this.data = this.left.data;
        this.left = this.left.left;
        this.right = newNode;
    }

    // 返回当前节点左子树的高度
    public int leftTreeHeight() {
        if (this.left == null) {
            return 0;
        }
        return this.left.height();
    }

    // 返回当前节点右子树的高度
    public int rightTreeHeight() {
        if (this.right == null) {
            return 0;
        }
        return this.right.height();
    }

    // 返回以当前节点为根节点的树的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    // 添加节点
    public void add(Node node) {
        if (this.data > node.data) {
            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 (rightTreeHeight() - leftTreeHeight() > 1) {
            if (this.right.leftTreeHeight() > this.right.rightTreeHeight()) {
                this.right.rightRotate();
            }
            leftRotate();
        } else if (leftTreeHeight() - rightTreeHeight() > 1) {
            if (this.left.rightTreeHeight() > this.left.leftTreeHeight()) {
                this.left.leftRotate();
            }
            rightRotate();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                '}';
    }

    // 中序遍历
    public void inOrder() {
        if (this.left != null) {
            this.left.inOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.inOrder();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值