java实现平衡二叉树

java实现平衡二叉树

和普通的二叉排序树,在节点的添加方法中进行了修改

/**
 * 节点
 */
class Node {
    int value;
    Node left;
    Node right;

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

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

    private void rightRotate() {
        // 创建一个新节点
        Node newNode = new Node(this.value);
        // 把新节点的右子树设置成当前节点的右子树
        newNode.right = this.right;
        // 把新节点的左子树设置成当前节点左子树的右子树
        newNode.left = this.left.right;
        // 把当前节点的值换成左子节点的值
        this.value = this.left.value;
        // 将当前的左子树换成左子树的左子树
        this.left = this.left.left;
        // 将当前右子树换成新节点
        this.right = newNode;
    }

    /**
     * 左旋转
     */
    private void leftRotate() {
        // 创建一个新的节点
        Node newNode = new Node(this.value);
        // 把新的节点的左子树设置成当前节点的左子树
        newNode.left = this.left;
        // 把新节点的右子树设置成当前节点右子树的左子树
        newNode.right = this.right.left;
        // 把当前节点的值换成右子节点的值
        this.value = this.right.value;
        // 把当前节点的右子树变成当前系欸但右子树的右子树
        this.right = this.right.right;
        // 把当前节点的左子树设置成新的节点
        this.left = newNode;
    }


    /**
     * 左子树高度
     */
    public int getLeftHeight() {
        if (this.left == null) {
            return 0;
        }
        return this.left.getHeight();
    }

    /**
     * 右子树高度
     */
    public int getRightHeight() {
        if (this.right == null) {
            return 0;
        }
        return this.right.getHeight();
    }

    /**
     * 返回树的高度
     */
    public int getHeight() {
        return Math.max((left == null ? 0 : left.getHeight()), (right == null ? 0 : right.getHeight())) + 1;
    }

    /**
     * 找到希望删除的节点
     *
     * @return 没找到为null
     */
    public Node findDelete(int value) {
        if (this.value == value) {
            return this;
        } else {

            if (value < this.value) {
                if (this.left == null) {
                    return null;
                }
                return this.left.findDelete(value);
            } else {
                if (this.right == null) {
                    return null;
                }
                return this.right.findDelete(value);
            }
        }
    }

    /**
     * 返回查找节点的父节点
     *
     * @return
     */
    public Node findDeleteParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.findDeleteParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.findDeleteParent(value);
            } else {
                return null;
            }
        }
    }

    /**
     * 添加节点的方法
     */
    public void add(Node 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 ((this.getRightHeight() - this.getLeftHeight()) > 1) {
            // 如果右子树的左子树高度大于右子树
            if (this.right != null && this.right.getLeftHeight() > this.right.getRightHeight()) {
                this.right.rightRotate();
            }
            this.leftRotate();

        } else if ((this.getLeftHeight() - this.getRightHeight()) > 1) {
            // 如果左子树的右子树高度大于左子树
            if (this.left != null && this.left.getRightHeight() > this.left.getLeftHeight()) {
                this.left.leftRotate();
            }
            this.rightRotate();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}
posted @ 2022-04-23 18:27  CoderCatIce  阅读(115)  评论(0编辑  收藏  举报