栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

平衡二叉树的旋转

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

平衡二叉树的旋转

package sy4;


public class Test1 {
    public static void main(String[] args) {
//        int[] arr = {4, 3, 6, 5, 7, 8};
        int[] arr = {10, 11, 7, 6, 8, 9};
        C c = new C();
        for (int i = 0; i < arr.length; i++) {
            c.add(new AvlNode(arr[i]));
        }
        c.order();
        System.out.println();
        System.out.println(c.getRoot().height());
        System.out.println(c.getRoot().leftHeight());
        System.out.println(c.getRoot().rightHeight());
    }
}

class C {
    private AvlNode root;

    public AvlNode getRoot() {
        return root;
    }


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

    public void order() {
        if (root != null) {
            root.order();
        } else {
            System.out.println("无法遍历");
        }
    }

    public AvlNode search(int val) {
        if (root == null) {
            return null;
        }
        return root.search(val);
    }

    public AvlNode searchParent(int val) {
        if (root == null) {
            return null;
        }
        return root.searchParent(val);
    }

    
    public int delRightTreeMin(AvlNode node) {
        AvlNode target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.val);
        return target.val;
    }

    
    public void delNode(int val) {
        if (root == null) {
            return;
        } else {
            AvlNode targetNode = search(val);
            if (targetNode == null) {
                return;
            }
            //只有一个节点
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            AvlNode parent = searchParent(val);
            //如果是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //判断是parent的左是右
                if (parent.left != null && parent.left.val == val) { //是左节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.val == val) { //是右节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) { //删除有两颗子树的节点
                int minVal = delRightTreeMin(root.right);
                targetNode.val = minVal;
            } else { //删除只有一个子树的节点
                //如过果删除的是左孩子节点
                if (targetNode.left != null) {
                    if (parent != null) {
                        // 如果 targetNode 是 parent 的左子结点
                        if (parent.left.val == val) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {
                    if (parent != null) {
                        if (parent.right.val == val) {
                            parent.right = targetNode.right;
                        } else {
                            parent.left = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }

        }
    }
}

class AvlNode {
    int val;
    AvlNode left;
    AvlNode right;

    public AvlNode() {
    }

    public AvlNode(int val) {
        this.val = val;
    }

    
    public void add(AvlNode node) {
        if (node == null) {
            return;
        }
        if (node.val < this.val) {
            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 (rightHeight() - leftHeight() > 1) {
            if (right != null && right.leftHeight() > right.rightHeight()) {  //当前右子树左边比右边高
                right.rightRotate();  //当前右子树右旋
                leftRotate();  //整棵树左旋
            } else {
                leftRotate(); //左旋
            }
            return;
        }
        //左比右高
        if (leftHeight() - rightHeight() > 1) {
            if (left != null && left.rightHeight() > left.leftHeight()) {  //左子树的右边比左边高
                left.leftRotate(); //左子树左旋转
                rightRotate();
            } else {
                rightRotate();
            }
        }
    }

    
    public void order() {
        if (this.left != null) {
            this.left.order();
        }
        System.out.print(this.val + " ");
        if (this.right != null) {
            this.right.order();
        }

    }

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

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

    
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    
    public AvlNode search(int val) {
        if (this.val == val) {
            return this;
        } else if (val < this.val) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(val);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(val);
        }
    }

    
    public AvlNode searchParent(int val) {
        if (this.left != null && this.left.val == val || this.right != null && this.right.val == val) {
            return this;
        } else {
            if (val < this.val && this.left != null) {
                return this.left.searchParent(val);
            } else if (val > this.val && this.right != null) {
                return this.right.searchParent(val);
            } else {
                return null;
            }
        }

    }

    
    private void leftRotate() {
        AvlNode newNode = new AvlNode(val);
        newNode.left = left;
        newNode.right = right.left;
        val = right.val;
        right = right.right;
        left = newNode;
    }

    
    private void rightRotate() {
        //以当前的节点的值创建一个新的节点
        AvlNode newNode = new AvlNode(val);
        //新的节点的右节点指向当前节点的右节点
        newNode.right = right;
        //新节点的右节点指向当前左节点的右节点
        newNode.left = left.right;
        //当前节点的值更改为当前左节点的值
        val = left.val;
        //当前组左节点更改为当前左节点的左节点
        left = left.left;
        //右节点更改为新的节点
        right = newNode;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号