平衡二叉树(AVL树)

class SelfBalancingBinarySearchTree

package DataStructures.tree;

/**
 * 平衡二叉树(AVL树)
 * @author coffee
 * @Date 2021-04-05 12:28
 */
public class SelfBalancingBinarySearchTree {
    public static void main(String[] args) {
        int[] arr = {4,3,6,5,7,8};//左旋
        //右旋{10,12,8,9,7,6}
        //双旋{10,11,7,6,8,9}
        SelfBalancingBinarySearchTree avlTree = new SelfBalancingBinarySearchTree();
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node3(arr[i]));
        }
        System.out.println("之前...");
        avlTree.infixOrder();
        System.out.println("树的高度" + avlTree.getRoot().height());
        System.out.println("左子树的高度" + avlTree.getRoot().leftHeight());
        System.out.println("右子树的高度" + avlTree.getRoot().rightHeight());


    }


    private Node3 root;

    public Node3 getRoot() {
        return root;
    }

    //删除节点
    public void delNode(int value){
        if (root == null){
            return;
        }else {
            Node3 target = search(value);//找到要删除的节点
            if (target == null){//没有找到要删除的节点
                return;
            }
            if (root.left == null && root.right == null){//只有根节点
                root = null;
                return;
            }
            Node3 parent = searchParent(value);//找到要删除的节点的父节点

            if (target.left == null && target.right == null) {//删除叶子节点
                if (parent.left != null && target == parent.left) {//当前节点是父节点的左子节点
                    parent.left = null;
                } else if (parent.right != null && target == parent.right) {//当前节点是父节点的右子节点
                    parent.right = null;
                }
            }else if (target.left == null || target.right == null){//删除只有一颗子树的节点
                if (target.right == null){//子节点是左子节点
                    if (parent == null){//当前节点为根节点,并有左子节点
                        root = target.left;
                    }
                    if (target == parent.left){//当前节点是父节点的左子节点
                        parent.left = target.left;
                    }else {//当前节点是父节点的右子节点
                        parent.right = target.left;
                    }
                }else{//子节点是右子节点
                    if (parent == null){//当前节点为根节点,并有右子节点
                        root = target.right;
                    }
                    if (target == parent.left){//当前节点是父节点的左子节点
                        parent.left = target.right;
                    }else {//当前节点是父节点的右子节点
                        parent.right = target.right;
                    }
                }
            } else {//删除有两颗子树的节点
                target.value = delRightTreeMin(target.right);
            }
        }
    }
    //返回以node为根节点的二叉排序树的最小节点的值
    //删除以node为根节点的二叉排序树的最小节点
    public int delRightTreeMin(Node3 node){
        while (node.left != null){//查找
            node = node.left;
        }
        delNode(node.value);//删除
        return node.value;
    }
    //查找要删除的节点的父节点
    public Node3 searchParent(int value) {
        return root.searchParent(value);
    }

    //查找要删除的节点
    public Node3 search(int value){
        return root.search(value);
    }
    //添加节点
    public void add(Node3 node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }
    //中序遍历
    public void infixOrder(){
        if (root != null){
            root.infixOrder();
        }else {
            System.out.println("空,不可遍历");
        }
    }
}
//节点
class Node3{
    int value;
    Node3 left;
    Node3 right;

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

    @Override
    public String toString() {
        return "Node2{" +
                "value=" + value +
                '}';
    }

    /**
     * 右旋转
     */
    public void rightRotate(){
        Node3 node = new Node3(value);//创建新的节点,以当前很节点的值
        node.right = right;//新节点的右子树射程为当前节点的右子树
        node.left = left.right;//新节点的左子树设置为当前节点的左子树的右子树
        value = left.value;//当前节点的值变为左子节点的值
        left = left.left;//当前节点的左子树设置为左子树的左子树
        right = node;//当前节点的右子树设置为新的节点
    }

    /**
    * 左旋转
    */
    public void leftRotate(){
        Node3 node = new Node3(value);//创建新的节点,以当前很节点的值
        node.left = left;//新节点的左子树设置成当前节点的左子树
        node.right = right.left;//新节点的右子树设置为当前节点的右子树的左子树
        value = right.value;//当前节点的值替换成右子节点的值
        right = right.right;//当前节点的右子树设置为当前节点右子树的右子树
        left = node;//当前节点的左子树设置成新节点
    }

    /**
     * //返回右子树的高度
     */
    public int rightHeight(){
        return right == null ? 0 :right.height();
    }

    /**
     * 返回左子树的高度
     */
    public int leftHeight(){
        return left == null ? 0 :left.height();
    }

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

    /**
     * 查找要删除的节点的父节点
     */
    public Node3 searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.left.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.right.value && this.right != null){
                return this.right.searchParent(value);
            }else {
                return null;
            }
        }
    }

    /**
     * 查找要删除的节点
     */
    public Node3 search(int value){
        if (this.value == value){
            return this;
        }else if (value < this.value){//向左递归
            if (this.left == null){
                return null;
            }
            return this.left.search(value);
        }else {
            if (this.right == null){//向右递归
                return null;
            }
            return this.right.search(value);
        }
    }

    /**
     * 添加节点
     */
    public void add(Node3 node){
        if (node == null){
            return;
        }
        if (node.value < this.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 (leftHeight() + 1< rightHeight()){
            if (right.leftHeight() > right.rightHeight()){//双旋
                rightRotate();
            }
            leftRotate();
        }else if (rightHeight() + 1 >leftHeight()){//双旋
            if(left.rightHeight() > left.leftHeight()){
                left.leftRotate();
            }
            rightRotate();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder(){
        if (this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值