数据结构与算法-135-145 -平衡二叉树

135 平衡二叉树介绍

二叉树可能的问题,一个数列是{1,2,3,4,5,6},这样的数列在二叉树中就变为了队列,整个树就是一个条直线

在这里插入图片描述

基本介绍:

  • 平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree) 又被称为 AVL 树,可以保证查询效率较高
  • 具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一颗平衡二叉树,平衡二叉树的常用实现方法有红黑树、AVL(这是一种算法,不是avl树)、替罪羊树、Treap、伸展树等

136 AVL树左旋转思路图解

在这里插入图片描述

137 AVL树高度求解

package com.old.Tree.avl_137;

public class AVLTreeDemo {
    public static void main(String[] gd) {
        int[] arr = {4, 3, 6, 5, 7, 8};
        //创建 avl 树
        AVLTree avlTree = new AVLTree();
        for (int i : arr) {
            avlTree.add(new Node(i));
        }
        avlTree.infixOrder();
        System.out.println("根节点高度:" + avlTree.height());
        System.out.println("左子树高度:" + avlTree.leftHeight());
        System.out.println("右子树高度:" + avlTree.rightHeight());
        System.out.println();
        System.out.println("树的高度为:" + avlTree.getRoot().height());
        System.out.println("左子树高度:" + avlTree.getRoot().leftHeight());
        System.out.println("右子树高度:" + avlTree.getRoot().rightHeight());
        System.out.println();

    }
}

/**
 * 创建 avl 树
 */
class AVLTree {
    private Node root;

    public int height() {
        return root == null ? 0 : root.height();
    }

    public int leftHeight() {
        if (root == null) {
            return -1;
        }
        return root.left == null ? 0 : root.left.height();
    }

    public int rightHeight() {
        if (root == null) {
            return -1;
        }
        return root.right == null ? 0 : root.right.height();
    }

    public Node getRoot() {
        return root;
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        }
        return root.search(value);
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        }
        return root.searchParent(value);
    }

    /**
     * 1.返回以 node 为根节点的二叉排序树的最小结点的值
     * 2.删除 node 为根节点的二叉排序树的最小结点
     *
     * @param node 传入的节点 (当做二叉排序树的根节点)
     * @return 返回以 node 为根节点的二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环查找左节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这时 target 就指向最小结点
        //删除最小结点
        int value = target.value;
        delNode(value);

        return value;
    }

    public void delNode(int value) {
        if (root == null) {
            return;
        }
        Node targetNode = search(value);
        if (targetNode == null) {
            //没有要删除的节点
            System.out.println("没有要删除的节点");
            return;
        }
        //如果发现当前这颗二叉排序树只有一个结点
        if (root.left == null && root.right == null) {
            System.out.println("只有一个节点");
            root = null;
            return;
        }
        //去找到 targetNode父结点
        Node parentNode = searchParent(value);
        System.out.println("找到的父节点" + parentNode);
        //如果要删除的结点是叶子节点
        if (targetNode.left == null && targetNode.right == null) {
            System.out.println("删除的是叶子节点,没有子节点");
            if (parentNode.left != null && parentNode.left.value == value) {
                parentNode.left = null;
                System.out.println("删除左节点");
            } else if (parentNode.right != null && parentNode.right.value == value) {
                parentNode.right = null;
                System.out.println("删除右节点");
            }
            return;
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的节点

            int minValue = delRightTreeMin(targetNode.right);
            targetNode.value = minValue;
        } else {
            //删除只有一颗节点的子树

            //删除只有一颗节点的子树 并且 如果要删除的节点有左子节点,
            if (targetNode.left != null) {
                if (parentNode != null) {
                    //如果 targetNode 是 parent 的左子节点
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.left;
                    } else {
                        //如果是 parent 的右子节点
                        parentNode.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                //删除只有一颗节点的子树 并且 要删除的节点有右子节点,
                if (parentNode != null) {
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.right;
                    } else {
                        parentNode.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,无法遍历");
        }
    }
}


class Node {

    int value;

    Node left;

    Node right;

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

    public int leftHeight() {
        if (left == null) {
            return 0;
        } else {
            return left.height();
        }
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        } else {
            return right.height();
        }
    }

    /**
     * 返回当前节点的高度,以该节点为根节点树的高度
     */
    public int height() {
        /**
         * Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height())
         * 这里只是统计出该节点下面的层数,本身还有一层,所以需要加1
         */
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }


    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    /**
     * 查找要删除结点的父结点
     *
     * @param value 要查找的节点的值
     * @return 要删除的节点的父节点
     */
    public Node searchParent(int value) {
        //如果当前节点就是要删除结点的父结点,就返回
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            System.out.println("当前节点就是父结点:" + this);
            return this;
        } else {
            //如果要查找的值小于当前结点的值,并且当前结点的左子节点不为空
            if (value < this.value && this.left != null) {
                System.out.println("向左子树递归查找");
                //向左子树递归查找
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                //向右子树递归查找
                System.out.println("向右子树递归查找");
                return this.right.searchParent(value);
            } else {
                //没有找到父节点
                return null;
            }
        }
    }

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

    /**
     * 添加节点的方法
     * 递归的形式添加结点,注意需要满足二叉排序树的要求
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入结点的值,和当前子树的根结点的值关系
        if (node.value < this.value) {
            //如果当前结点为空
            if (this.left == null) {
                this.left = node;
                return;
            } else {
                this.left.add(node);
            }
        } else if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
                return;
            } else {
                this.right.add(node);
            }
        }

    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);

        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}



138 AVL树左旋转代码实现

package com.old.Tree.avl_137;

public class AVLTreeDemo {
    public static void main(String[] gd) {
        int[] arr = {4, 3, 6, 5, 7, 8};
        //创建 avl 树
        AVLTree avlTree = new AVLTree();
        for (int i : arr) {
            avlTree.add(new Node(i));
        }
        avlTree.infixOrder();
        System.out.println("根节点高度:" + avlTree.height());
        System.out.println("左子树高度:" + avlTree.leftHeight());
        System.out.println("右子树高度:" + avlTree.rightHeight());
        System.out.println();
        System.out.println("树的高度为:" + avlTree.getRoot().height());
        System.out.println("左子树高度:" + avlTree.getRoot().leftHeight());
        System.out.println("右子树高度:" + avlTree.getRoot().rightHeight());
        System.out.println();

    }
}

/**
 * 创建 avl 树
 */
class AVLTree {
    private Node root;

    public int height() {
        return root == null ? 0 : root.height();
    }



    public int leftHeight() {
        if (root == null) {
            return -1;
        }
        return root.left == null ? 0 : root.left.height();
    }

    public int rightHeight() {
        if (root == null) {
            return -1;
        }
        return root.right == null ? 0 : root.right.height();
    }

    public Node getRoot() {
        return root;
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        }
        return root.search(value);
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        }
        return root.searchParent(value);
    }

    /**
     * 1.返回以 node 为根节点的二叉排序树的最小结点的值
     * 2.删除 node 为根节点的二叉排序树的最小结点
     *
     * @param node 传入的节点 (当做二叉排序树的根节点)
     * @return 返回以 node 为根节点的二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环查找左节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这时 target 就指向最小结点
        //删除最小结点
        int value = target.value;
        delNode(value);

        return value;
    }

    public void delNode(int value) {
        if (root == null) {
            return;
        }
        Node targetNode = search(value);
        if (targetNode == null) {
            //没有要删除的节点
            System.out.println("没有要删除的节点");
            return;
        }
        //如果发现当前这颗二叉排序树只有一个结点
        if (root.left == null && root.right == null) {
            System.out.println("只有一个节点");
            root = null;
            return;
        }
        //去找到 targetNode父结点
        Node parentNode = searchParent(value);
        System.out.println("找到的父节点" + parentNode);
        //如果要删除的结点是叶子节点
        if (targetNode.left == null && targetNode.right == null) {
            System.out.println("删除的是叶子节点,没有子节点");
            if (parentNode.left != null && parentNode.left.value == value) {
                parentNode.left = null;
                System.out.println("删除左节点");
            } else if (parentNode.right != null && parentNode.right.value == value) {
                parentNode.right = null;
                System.out.println("删除右节点");
            }
            return;
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的节点

            int minValue = delRightTreeMin(targetNode.right);
            targetNode.value = minValue;
        } else {
            //删除只有一颗节点的子树

            //删除只有一颗节点的子树 并且 如果要删除的节点有左子节点,
            if (targetNode.left != null) {
                if (parentNode != null) {
                    //如果 targetNode 是 parent 的左子节点
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.left;
                    } else {
                        //如果是 parent 的右子节点
                        parentNode.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                //删除只有一颗节点的子树 并且 要删除的节点有右子节点,
                if (parentNode != null) {
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.right;
                    } else {
                        parentNode.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,无法遍历");
        }
    }
}


class Node {

    int value;

    Node left;

    Node right;

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

    public int leftHeight() {
        if (left == null) {
            return 0;
        } else {
            return left.height();
        }
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        } else {
            return right.height();
        }
    }

    /**
     * 返回当前节点的高度,以该节点为根节点树的高度
     */
    public int height() {
        /**
         * Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height())
         * 这里只是统计出该节点下面的层数,本身还有一层,所以需要加1
         */
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转
     */
    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 Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    /**
     * 查找要删除结点的父结点
     *
     * @param value 要查找的节点的值
     * @return 要删除的节点的父节点
     */
    public Node searchParent(int value) {
        //如果当前节点就是要删除结点的父结点,就返回
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            System.out.println("当前节点就是父结点:" + this);
            return this;
        } else {
            //如果要查找的值小于当前结点的值,并且当前结点的左子节点不为空
            if (value < this.value && this.left != null) {
                System.out.println("向左子树递归查找");
                //向左子树递归查找
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                //向右子树递归查找
                System.out.println("向右子树递归查找");
                return this.right.searchParent(value);
            } else {
                //没有找到父节点
                return null;
            }
        }
    }

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

    /**
     * 添加节点的方法
     * 递归的形式添加结点,注意需要满足二叉排序树的要求
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入结点的值,和当前子树的根结点的值关系
        if (node.value < this.value) {
            //如果当前结点为空
            if (this.left == null) {
                this.left = node;
                return;
            } else {
                this.left.add(node);
            }
        } else if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
                return;
            } else {
                this.right.add(node);
            }
        }
        //当添加完一个结点,如果右子树的高度 - 左子树的高度 > 1 就要左旋转
        if (Math.abs(this.rightHeight() - this.leftHeight()) > 1) {
            if (right != null && right.rightHeight() < right.leftHeight()) {
                //先 对右子树进行旋转
            }
            this.leftRotate();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);

        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}


139 AVL树右旋图解和实现

数列: [10,12,8,9,7,6]

问题:当插入6时 leftHeight() - rightHeight() > 1 成立,此时,不再是一颗 av 树了
需要右旋转,【即降低左子树的高度】,这里是将 9 这个节点通过右旋转,到右子树

  1. 创建一个新的节点 newNode (以 10 这个值创建),创建一个新的节点,值等于当前根节点的值
  2. newNode.right = right; 把新节点的右子树设置为当前节点的右子树
  3. newNode.left = left.right; 把新节点的左子树设置为当前节点的左子树的右子树
  4. value = left.value; 把当前节点的值换为左子节点的值
  5. left = left.left 把当前节点的左子树设置为左子树的左子树
  6. right = newLeft; 把当前节点的右子树设置为新节点

在这里插入图片描述

package com.old.Tree.avl_137;

public class AVLTreeDemo {
    public static void main(String[] gd) {
//        int[] arr = {4, 3, 6, 5, 7, 8};
        int[] arr = {10, 12, 8, 9, 7, 6};
        //创建 avl 树
        AVLTree avlTree = new AVLTree();
        for (int i : arr) {
            avlTree.add(new Node(i));
        }
        avlTree.infixOrder();
        System.out.println("根节点高度:" + avlTree.height());
        System.out.println("左子树高度:" + avlTree.leftHeight());
        System.out.println("右子树高度:" + avlTree.rightHeight());
        System.out.println();
        System.out.println("树的高度为:" + avlTree.getRoot().height());
        System.out.println("左子树高度:" + avlTree.getRoot().leftHeight());
        System.out.println("右子树高度:" + avlTree.getRoot().rightHeight());
                System.out.println("当前的根节点:" + avlTree.getRoot());
        System.out.println();

    }
}

/**
 * 创建 avl 树
 */
class AVLTree {
    private Node root;

    public int height() {
        return root == null ? 0 : root.height();
    }


    public int leftHeight() {
        if (root == null) {
            return -1;
        }
        return root.left == null ? 0 : root.left.height();
    }

    public int rightHeight() {
        if (root == null) {
            return -1;
        }
        return root.right == null ? 0 : root.right.height();
    }

    public Node getRoot() {
        return root;
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        }
        return root.search(value);
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        }
        return root.searchParent(value);
    }

    /**
     * 1.返回以 node 为根节点的二叉排序树的最小结点的值
     * 2.删除 node 为根节点的二叉排序树的最小结点
     *
     * @param node 传入的节点 (当做二叉排序树的根节点)
     * @return 返回以 node 为根节点的二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环查找左节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这时 target 就指向最小结点
        //删除最小结点
        int value = target.value;
        delNode(value);

        return value;
    }

    public void delNode(int value) {
        if (root == null) {
            return;
        }
        Node targetNode = search(value);
        if (targetNode == null) {
            //没有要删除的节点
            System.out.println("没有要删除的节点");
            return;
        }
        //如果发现当前这颗二叉排序树只有一个结点
        if (root.left == null && root.right == null) {
            System.out.println("只有一个节点");
            root = null;
            return;
        }
        //去找到 targetNode父结点
        Node parentNode = searchParent(value);
        System.out.println("找到的父节点" + parentNode);
        //如果要删除的结点是叶子节点
        if (targetNode.left == null && targetNode.right == null) {
            System.out.println("删除的是叶子节点,没有子节点");
            if (parentNode.left != null && parentNode.left.value == value) {
                parentNode.left = null;
                System.out.println("删除左节点");
            } else if (parentNode.right != null && parentNode.right.value == value) {
                parentNode.right = null;
                System.out.println("删除右节点");
            }
            return;
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的节点

            int minValue = delRightTreeMin(targetNode.right);
            targetNode.value = minValue;
        } else {
            //删除只有一颗节点的子树

            //删除只有一颗节点的子树 并且 如果要删除的节点有左子节点,
            if (targetNode.left != null) {
                if (parentNode != null) {
                    //如果 targetNode 是 parent 的左子节点
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.left;
                    } else {
                        //如果是 parent 的右子节点
                        parentNode.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                //删除只有一颗节点的子树 并且 要删除的节点有右子节点,
                if (parentNode != null) {
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.right;
                    } else {
                        parentNode.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,无法遍历");
        }
    }
}


class Node {

    int value;

    Node left;

    Node right;

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

    public int leftHeight() {
        if (left == null) {
            return 0;
        } else {
            return left.height();
        }
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        } else {
            return right.height();
        }
    }

    /**
     * 返回当前节点的高度,以该节点为根节点树的高度
     */
    public int height() {
        /**
         * Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height())
         * 这里只是统计出该节点下面的层数,本身还有一层,所以需要加1
         */
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转
     */
    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 void rightRetate() {
        //创建新的节点 newNode,值等于当前根节点的值
        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;

    }


    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    /**
     * 查找要删除结点的父结点
     *
     * @param value 要查找的节点的值
     * @return 要删除的节点的父节点
     */
    public Node searchParent(int value) {
        //如果当前节点就是要删除结点的父结点,就返回
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            System.out.println("当前节点就是父结点:" + this);
            return this;
        } else {
            //如果要查找的值小于当前结点的值,并且当前结点的左子节点不为空
            if (value < this.value && this.left != null) {
                System.out.println("向左子树递归查找");
                //向左子树递归查找
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                //向右子树递归查找
                System.out.println("向右子树递归查找");
                return this.right.searchParent(value);
            } else {
                //没有找到父节点
                return null;
            }
        }
    }

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

    /**
     * 添加节点的方法
     * 递归的形式添加结点,注意需要满足二叉排序树的要求
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入结点的值,和当前子树的根结点的值关系
        if (node.value < this.value) {
            //如果当前结点为空
            if (this.left == null) {
                this.left = node;
                return;
            } else {
                this.left.add(node);
            }
        } else if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
                return;
            } else {
                this.right.add(node);
            }
        }
        //当添加完一个结点,如果右子树的高度 - 左子树的高度 > 1 就要左旋转
        if (this.rightHeight() - this.leftHeight() > 1) {
            if (right != null && right.rightHeight() < right.leftHeight()) {
                //先 对右子树进行旋转
            }
            this.leftRotate();
        }

        //当添加完一个结点,如果左子树的高度 - 右子树的高度 > 1 就要右旋转
        if ((this.leftHeight() - this.rightHeight()) > 1) {
            this.rightRetate();
        }


    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);

        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}


输出结果
在这里插入图片描述

140 AVL 树双旋转图解和实现

之前的数列进行单旋转(即一次旋转)就可以将非平衡二叉树转成平衡二叉树,但是某些情况下,单旋转不能完成平衡二叉树的转换。比如数列:int[] arr = {10,11,7,6,8,9};运行原来的代码可以看到,并没有转成 AVL 树。int[] arr = {2,1,6,5,7,3};运行原来的代码可以看到,并没有转成 AVL树

在这里插入图片描述

在这里插入图片描述

package com.old.Tree.avl_137;

public class AVLTreeDemo {
    public static void main(String[] gd) {
//        int[] arr = {4, 3, 6, 5, 7, 8};
//        int[] arr = {10, 12, 8, 9, 7, 6};
        int[] arr = {10,11,7,6,8,9};
        //创建 avl 树
        AVLTree avlTree = new AVLTree();
        for (int i : arr) {
            avlTree.add(new Node(i));
        }
        avlTree.infixOrder();
        System.out.println("根节点高度:" + avlTree.height());
        System.out.println("左子树高度:" + avlTree.leftHeight());
        System.out.println("右子树高度:" + avlTree.rightHeight());
        System.out.println();
        System.out.println("树的高度为:" + avlTree.getRoot().height());
        System.out.println("左子树高度:" + avlTree.getRoot().leftHeight());
        System.out.println("右子树高度:" + avlTree.getRoot().rightHeight());
        System.out.println("当前的根节点:" + avlTree.getRoot());
        System.out.println("当前的根节点的左子节点:" + avlTree.getRoot().left);
        System.out.println("当前的根节点的右子节点:" + avlTree.getRoot().right);
        System.out.println();

    }
}

/**
 * 创建 avl 树
 */
class AVLTree {
    private Node root;

    public int height() {
        return root == null ? 0 : root.height();
    }


    public int leftHeight() {
        if (root == null) {
            return -1;
        }
        return root.left == null ? 0 : root.left.height();
    }

    public int rightHeight() {
        if (root == null) {
            return -1;
        }
        return root.right == null ? 0 : root.right.height();
    }

    public Node getRoot() {
        return root;
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        }
        return root.search(value);
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        }
        return root.searchParent(value);
    }

    /**
     * 1.返回以 node 为根节点的二叉排序树的最小结点的值
     * 2.删除 node 为根节点的二叉排序树的最小结点
     *
     * @param node 传入的节点 (当做二叉排序树的根节点)
     * @return 返回以 node 为根节点的二叉排序树的最小结点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环查找左节点,就会找到最小值
        while (target.left != null) {
            target = target.left;
        }
        //这时 target 就指向最小结点
        //删除最小结点
        int value = target.value;
        delNode(value);

        return value;
    }

    public void delNode(int value) {
        if (root == null) {
            return;
        }
        Node targetNode = search(value);
        if (targetNode == null) {
            //没有要删除的节点
            System.out.println("没有要删除的节点");
            return;
        }
        //如果发现当前这颗二叉排序树只有一个结点
        if (root.left == null && root.right == null) {
            System.out.println("只有一个节点");
            root = null;
            return;
        }
        //去找到 targetNode父结点
        Node parentNode = searchParent(value);
        System.out.println("找到的父节点" + parentNode);
        //如果要删除的结点是叶子节点
        if (targetNode.left == null && targetNode.right == null) {
            System.out.println("删除的是叶子节点,没有子节点");
            if (parentNode.left != null && parentNode.left.value == value) {
                parentNode.left = null;
                System.out.println("删除左节点");
            } else if (parentNode.right != null && parentNode.right.value == value) {
                parentNode.right = null;
                System.out.println("删除右节点");
            }
            return;
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的节点

            int minValue = delRightTreeMin(targetNode.right);
            targetNode.value = minValue;
        } else {
            //删除只有一颗节点的子树

            //删除只有一颗节点的子树 并且 如果要删除的节点有左子节点,
            if (targetNode.left != null) {
                if (parentNode != null) {
                    //如果 targetNode 是 parent 的左子节点
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.left;
                    } else {
                        //如果是 parent 的右子节点
                        parentNode.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                //删除只有一颗节点的子树 并且 要删除的节点有右子节点,
                if (parentNode != null) {
                    if (parentNode.left.value == value) {
                        parentNode.left = targetNode.right;
                    } else {
                        parentNode.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }
        }
    }

    //添加结点的方法
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空,无法遍历");
        }
    }
}


class Node {

    int value;

    Node left;

    Node right;

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

    public int leftHeight() {
        if (left == null) {
            return 0;
        } else {
            return left.height();
        }
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        } else {
            return right.height();
        }
    }

    /**
     * 返回当前节点的高度,以该节点为根节点树的高度
     */
    public int height() {
        /**
         * Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height())
         * 这里只是统计出该节点下面的层数,本身还有一层,所以需要加1
         */
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转
     */
    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 void rightRetate() {
        //创建新的节点 newNode,值等于当前根节点的值
        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;

    }


    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    /**
     * 查找要删除结点的父结点
     *
     * @param value 要查找的节点的值
     * @return 要删除的节点的父节点
     */
    public Node searchParent(int value) {
        //如果当前节点就是要删除结点的父结点,就返回
        if ((this.left != null && this.left.value == value)
                || (this.right != null && this.right.value == value)) {
            System.out.println("当前节点就是父结点:" + this);
            return this;
        } else {
            //如果要查找的值小于当前结点的值,并且当前结点的左子节点不为空
            if (value < this.value && this.left != null) {
                System.out.println("向左子树递归查找");
                //向左子树递归查找
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                //向右子树递归查找
                System.out.println("向右子树递归查找");
                return this.right.searchParent(value);
            } else {
                //没有找到父节点
                return null;
            }
        }
    }

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

    /**
     * 添加节点的方法
     * 递归的形式添加结点,注意需要满足二叉排序树的要求
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入结点的值,和当前子树的根结点的值关系
        if (node.value < this.value) {
            //如果当前结点为空
            if (this.left == null) {
                this.left = node;
                return;
            } else {
                this.left.add(node);
            }
        } else if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
                return;
            } else {
                this.right.add(node);
            }
        }
        //当添加完一个结点,如果右子树的高度 - 左子树的高度 > 1 就要左旋转
        if (this.rightHeight() - this.leftHeight() > 1) {
            //如果当前结点的右结点不为空,并且当前节点的的右结点的左子树高度 大于 当前结点的右结点的右高度
            if (right != null &&  right.leftHeight() > right.rightHeight()) {
                //再对当前结点进行右旋转
                this.right.rightRetate();
                //先对当前结点的左结点,左旋转
                this.leftRotate();
            } else {
                this.leftRotate();
            }
            //必须,因为这里做了,已经实现了一次旋转。下面再判断没有意义
            return;
        }

        //当添加完一个结点,如果左子树的高度 - 右子树的高度 > 1 就要右旋转
        if ((this.leftHeight() - this.rightHeight()) > 1) {
            //如果它的左子树的右子树的高度大于它的左子树的高度
            if ((left != null &&
                    this.left.rightHeight() > this.left.leftHeight())) {
                //先对当前结点的左结点,左旋转
                this.left.leftRotate();
                //再对当前结点进行右旋转
                this.rightRetate();
            } else {
                this.rightRetate();
            }
        }


    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }

        System.out.println(this);

        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}


在这里插入图片描述

141 平衡二叉树小结

142 多叉树原理图解

二叉树和b树
二叉树的操作效率高,但是也存在问题

二叉树需要加载到内存,如果二叉树的节点少,没有什么问题,但是如果二叉树的节点节点很多就存在问题:

  • 在构建二叉树时,需要多次进行 i/o操作(少量数据存在数据库或文件中),节点海量,构建二叉树时,速度有影响
  • 节点海量,也会造成二叉树的高度很大,会降低操作速度

多叉树

  • 在二叉树中,每个节点有数据项,最多有两个子节点。如果允许每个节点可以有更多的数据项和更多的子节点,就是多叉树(multiway tree)
  • 后面讲解的 2-3 树,2-3-4树就是多叉树,多叉树通过重新组织节点,减少树的高度,能对二叉树进行优化

在这里插入图片描述

B树
b树通过重新组织节点,降低树的高度,并且减少i/o读写次数来提升效率。

在这里插入图片描述

  • 如图,b树通过重新组织节点,降低了树的高度
  • 文件系统及数据库系统设计者利用了磁盘预读原理,将一个节点的大小设为等于一个页(页的大小通常为4k),这样每个节点只需要一次i/o就可以完全载入
  • 将树的度 M 设置为 1024 ,在 600 亿个元素中最多只需要4次 i/o 就可以读取到想要的元素,B树(B+)广泛应用于文件存储系统以及数据库系统中

节点的度:节点的下面子节点的个数
树的度:所有节点里面,节点的度最大的值

143 2-3树原理图解

2-3树是最简单的b树结构,具有如下特点:

  • 2-3树的所有叶子节点都在同一层(只要是 B 树都满足这个条件)
  • 有两个子节点的节点叫二节点,二节点要么没有子节点,要么有两个子节点
  • 有三个子节点的节点叫三节点,三节点要么没有子节点,要么有三个子节点
  • 2-3树是由二节点和三节点构成的树

在这里插入图片描述

144 B树和B加树原理图解

除了23树,还有234树,概念和23树类似,也是珞b树

在这里插入图片描述


B树、B+树和B*树
B-tree树即B树,B即 Balanced ,平衡的意思。有人把B-tree翻译成 B-(减)树,容易让人产生误解。会以为B-树是一种树,而B树又是另一种树。实际上 B-tree就是指的B树

B树

介绍
Mysql的某种类型的索引是基于B树或者B+树的。

  • B树的阶:节点最多子节点个数。比如2-3树的阶是3,2-3-4树的阶是4
  • B树的搜索,从根节点开始,对结点内的关键字(有序)序列进行二分查找,如果命中则结束,否则进入查询关键字所属范围的儿子结点;重复,直到所对应的儿子指针为空,或已经是叶子节点。
  • 关键字集合分布在整颗树中,即叶子节点和非叶子节点都存放数据
  • 搜索有可能非叶子结点结束
  • 其搜索性能等价于在关键字全集内做一次二分查找

在这里插入图片描述

B+树

介绍:
B+树是B树的变体,也是一种多路搜索树

  • B+树的搜索与B树也基本相同,区别是B+树只有达到叶子节点才命中(B树可以在非叶子结点命中),其性能也等价于在关键字全集做一次二分查找
  • 所有关键字都出现在叶子结点的链表中(即数据只能在叶子节点(也叫稠密索引),且链表中的关键字(数据)恰好是有序的
  • 不可能在非叶子节点命中的
  • 非叶子结点相当于是叶子结点的索引(稀疏索引),叶子结点相当于是存储(关键字)数据的数据层
  • 更适合文件索引系统
  • B树和B+树各有自己的应用场景,不能说B+树完全比B树,反之亦然

在这里插入图片描述

145 B星树和多路查找树小结

介绍:
B*树是B+树的变体,在B+树的非根和非叶子结点再增加指向兄弟的指针

  • B*树定义了非叶子结点关键字个数至少为(2/3)*M,即块的最低使用率为2/3,而B+树的块最低使用率为B+树的1/2
  • 从第1个特点可以看出,B*树分配新结点的概率比B+树要低,空间使用率更高

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值