zoukankan      html  css  js  c++  java
  • 数据结构 avl左旋和右旋

    左旋——自己变为右孩子的左孩子;右旋——自己变为左孩子的右孩子; 以上口诀+动图=完美

    高度平衡的搜索二叉树
    一棵平衡树,或是空树,或是具有以下性质的二叉搜索树:左子树和右子树都是AVL树,且左右子树的高度之差的绝对值不超过1。

    平衡化旋转
    AVL树相较于普通的二叉搜索树,自主要的就是做了平衡化处理,使得二叉树变的平衡,高度降低。
    在插入一个结点后应该沿搜索路径将路径上的结点平衡因子进行修改,当平衡因子大于1时,就需要进行平衡化处理。从发生不平衡的结点起,沿刚才回溯的路径取直接下两层的结点,如果这三个结点在一条直线上,则采用单旋转进行平衡化,如果这三个结点位于一条折线上,则采用双旋转进行平衡化。

    单旋转

    左单旋

    将右子树的左子树链接到父亲节点的右孩子结点,父亲节点作为ptr结点的左孩子结点便完成了旋转。

    右单旋

    右单旋是左单旋的镜像旋转.
    当前节点ptr,与父亲节点和当前节点的左孩子结点位于一条直线上时,使用右单旋进行平衡。

      // 左旋
        private void leftRotate()
        {
            //创建新的结点,以当前根结点的值
            Node newNode = new Node(value);
            //把新的结点的左子树设置成当前结点的左子树
            newNode.left = left;
            //把新的结点的右子树设置成带你过去结点的右子树的左子树
            newNode.right = right.left;
            //把当前结点的值替换成右子结点的值
            value = right.value;
            //把当前结点的右子树设置成当前结点右子树的右子树
            right = right.right;
            //把当前结点的左子树(左子结点)设置成新的结点
            left = newNode;
        }
    
       // 右旋
        private void rightRotate()
        {
            // 10  12 8 9 7 6 拿着转了玩
            Node newNode = new Node(value);
            newNode.right = right;
            newNode.left = left.right;
            value = left.value;
            left = left.left;
            right = newNode;
        }
      
    
    package tree.bst.avl;
    
    public class AVLTreeDemo {
        public static void main(String[] args) {
            //int[] arr = {4,3,6,5,7,8};
            int[] arr = {10,12,8,9,7,6};
            AvlTree avlTree = new AvlTree();
    
            for (int i = 0; i < arr.length; i++) {
                avlTree.addNode(new Node(arr[i]));
            }
    
            // 中序遍历显示
            avlTree.infixOrder();
    
            // 求高度
            System.out.println(avlTree.getRoot().height());
            System.out.println(avlTree.getRoot().leftHeight());
            System.out.println(avlTree.getRoot().rightHeight());
    
            avlTree.infixOrder();
    
        }
    }
    
    class AvlTree {
        public Node getRoot() {
            return root;
        }
    
        public void setRoot(Node root) {
            this.root = root;
        }
    
        private Node root;
    
        public int height()
        {
            if (root == null){
                return 0;
            } else {
                return root.height();
            }
        }
    
        public void addNode(Node node) {
            if (root == null) {
                root = node;
            } else {
                root.add(node);
            }
        }
    
        public void infixOrder() {
            if (root == null) {
                return;
            }
    
            root.infixOrder();
        }
    
        public Node search(int value) {
            if (root == null) {
                return null;
            } else {
                return root.search(value);
            }
        }
    
        public Node searchParent(int value) {
            if (root == null) {
                return null;
            } else {
                return root.searchParent(value);
            }
        }
    
        /**
         * 删除最小节点
         *
         * @param node 传入的节点 当做一颗二叉排序书的根节点
         * @return 以node为根节点的的最小节点的值
         */
        public int delRightTreeMin(Node node) {
            Node temp = node;
    
            // 循环查找左子节点找到最小值
            while (temp.left != null) {
                temp = temp.left;
            }
    
            // delete the mini node
            deleteNode(temp.value);
    
            return temp.value;
        }
    
        public void deleteNode(int value) {
            if (root == null) {
                return;
            }
    
            // find thi node
            Node targetNode = this.search(value);
            // 如果没有找到要删除的节点
            if (targetNode == null) {
                return;
            }
    
            // 没有父节点
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
    
            Node parent = searchParent(value);
    
            //叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //两颗子树
                int min = this.delRightTreeMin(targetNode.right);
                targetNode.value = min;
            } else { // 一颗子树
                //  if has left node
    
                if (targetNode.left != null) {
                    if (parent != null) {
                        // 如果targetNode是parent的左右
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
    
    
                } else {
                    if (parent != null) {
                        if (parent.right.value == value) {
                            parent.right = targetNode.right;
                        } else {
                            parent.left = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
    
                }
            }
        }
    
    }
    
    
    class Node {
        public int value;
        public Node left;
        public Node right;
    
        public Node(int value) {
            this.value = value;
        }
    
        public int leftHeight() {
            if (left == null) {
                return 0;
            } else {
                return left.height();
            }
        }
        // 左旋
        private void leftRotate()
        {
            //创建新的结点,以当前根结点的值
            Node newNode = new Node(value);
            //把新的结点的左子树设置成当前结点的左子树
            newNode.left = left;
            //把新的结点的右子树设置成带你过去结点的右子树的左子树
            newNode.right = right.left;
            //把当前结点的值替换成右子结点的值
            value = right.value;
            //把当前结点的右子树设置成当前结点右子树的右子树
            right = right.right;
            //把当前结点的左子树(左子结点)设置成新的结点
            left = newNode;
        }
    
        // 右旋
        private void rightRotate()
        {
            // 10  12 8 9 7 6 拿着转了玩
            Node newNode = new Node(value);
            newNode.right = right;
            newNode.left = left.right;
            value = left.value;
            left = left.left;
            right = newNode;
        }
    
        public int rightHeight() {
            if (right == null) {
                return 0;
            } else {
                return right.height();
            }
        }
    
        public int height() {
            return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1;
        }
    
        // find node
        public Node search(int value) {
            if (value == this.value) {
                return this;
            } else if (value < this.value) {
                // left tree find
                if (this.left == null) {
                    return null;
                }
                return this.left.search(value);
            } else {
                if (this.right == null) {
                    return null;
                }
    
                return this.right.search(value);
            }
    
        }
    
        // 查找要删除的节点的父节点
        public Node searchParent(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.searchParent(value);
                } else if (value >= this.value && this.right != null) {
                    return this.right.searchParent(value);
                } else {
                    return null;
                }
            }
        }
    
        // 添加节点
        public void add(Node node) {
            if (node == null) {
                return;
            }
    
            // 添加 left
            if (node.value < this.value) {
                if (this.left == null) {
                    this.left = node;
                } else {
                    this.left.add(node);
                }
            }
    
            // 添加到right
            if (node.value > this.value) {
                if (this.right == null) {
                    this.right = node;
                } else {
                    this.right.add(node);
                }
            }
    
            // 添加完一个节点后 如果柚子树-左子树的高度>1 左旋转
            if(rightHeight() - leftHeight() > 1) {
                leftRotate();
            }
    
            if(leftHeight() - rightHeight() > 1) {
               rightRotate();
            }
    
        }
    
        // 中序便利
        public void infixOrder() {
            if (this.left != null) {
                this.left.infixOrder();
            }
    
            System.out.println(this);
    
            if (this.right != null) {
                this.right.infixOrder();
            }
        }
    
        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    '}';
        }
    }
    
  • 相关阅读:
    tomcat自启动的最简单的方法
    Eclipse引入DTD文件
    MyBatis框架之基本知识介绍
    【转】Linux系统安装Redis详细过程
    Spring MVC + Spring + MyBatis 框架整合
    Spring框架之IoC和AOP
    Mysql 时间相关
    【转】Spring事务详解
    Spring的注解问题
    关于Calendar的一些用法总结
  • 原文地址:https://www.cnblogs.com/brady-wang/p/15148090.html
Copyright © 2011-2022 走看看