算法导论之平衡搜索树

示例


平衡搜索树示例

AVL.java源代码

package com.reign.gcld.chapter12;

/**
 * AVL树是一棵自平衡二叉搜索树,
 * 其中,每个节点的左右子树高度差不超过1
 */
public class AVL extends BST {
    public static void main(String[] args) {
        AVL tree = new AVL();
        //插入测试
        Entry entryG = new Entry("G","G");
        tree.insert(entryG);
        Entry entryE = new Entry("E","E");
        tree.insert(entryE);
        Entry entryR = new Entry("R","R");
        tree.insert(entryR);
        Entry entryB = new Entry("B","B");
        tree.insert(entryB);
        Entry entryN = new Entry("N","N");
        tree.insert(entryN);
        Entry entryY = new Entry("Y","Y");
        tree.insert(entryY);
        Entry entryK = new Entry("K", "K");
        tree.insert(entryK);
        //中序遍历
        Iterable nodes = tree.inOrder(tree.root);
        for (Node node : nodes) {
            System.out.println(node + "#balFac=" + tree.getBalFac(node));
        }
//        Entry entryM = new Entry("M", "M");
//        tree.insert(entryM);
//        //中序遍历
//        Iterable nodes1 = tree.inOrder(tree.root);
//        for (Node node : nodes1) {
//            System.out.println(node+"#balFac="+tree.getBalFac(node));
//        }
        tree.delete(entryY);
        //中序遍历
        Iterable nodes2 = tree.inOrder(tree.root);
        for (Node node : nodes2) {
            System.out.println(node + "#balFac=" + tree.getBalFac(node));
        }

    }

    @Override
    public void delete(Entry e) {
        Node x = search(e);
        if (x == null) {
            return ;
        }
        super.delete(x);
        for (Node z=super.hot; z != null; z=z.getParent()) {
            if (!avlBalanced(z)) {
                Node g = z;
                Node p = getTallerChild(g);
                Node v = getTallerChild(p);
                if (super.isLChild(p)) {
                    if (super.isLChild(v)) {
                        //left-left:一次右旋
                        p.setParent(g.getParent());
                        z = rightRotate(v, g, p, v.getLeft(), v.getRight(), p.getRight(), g.getRight());
                    } else {
                        //left_right:先左旋再右旋
                        v.setParent(g.getParent());
                        z = leftRightRotate(p, v, g, p.getLeft(), v.getLeft(), v.getRight(), g.getRight());

                    }
                } else {
                    if (super.isRChild(v)) {
                        //right-right:一次左旋
                        p.setParent(g.getParent());
                        z = leftRotate(g, p, v, g.getLeft(), p.getLeft(), v.getLeft(), v.getRight());
                    } else {
                        //right-left:先右旋,再左旋
                        v.setParent(g.getParent());
                        z = rightLeftRotate(g, v, g, g.getLeft(), v.getLeft(), v.getRight(), p.getRight());
                    }
                }
            }
            super.updateHeight(z);
        }
        return ;
    }

    @Override
    public Node insert(Entry e) {
        Node w = super.insert(e);
        for (Node z=super.hot; z != null; z=z.getParent()) {
            if (!avlBalanced(z)) {
                Node g = z;
                Node p = getTallerChild(g);
                Node v = getTallerChild(p);
                if (super.isLChild(p)) {
                    if (super.isLChild(v)) {
                        //left-left:一次右旋
                        p.setParent(g.getParent());
                        if (super.isLChild(g)) {
                            g.getParent().setLeft(p);
                        }else {
                            g.getParent().setRight(p);
                        }
                        z = rightRotate(v,g,p, v.getLeft(), v.getRight(), p.getRight(), g.getRight());
                    }else {
                        //left_right:先左旋再右旋
                        v.setParent(g.getParent());
                        if (super.isLChild(g)) {
                            g.getParent().setLeft(v);
                        }else {
                            g.getParent().setRight(v);
                        }
                        z = leftRightRotate(p, v, g, p.getLeft(), v.getLeft(), v.getRight(), g.getRight());

                    }
                }else {
                    if (super.isRChild(v)) {
                        //right-right:一次左旋
                        p.setParent(g.getParent());
                        if (super.isLChild(g)) {
                            g.getParent().setLeft(p);
                        }else {
                            g.getParent().setRight(p);
                        }
                        z = leftRotate(g,p,v, g.getLeft(), p.getLeft(), v.getLeft(), v.getRight());
                    }else {
                        //right-left:先右旋,再左旋
                        v.setParent(g.getParent());
                        if (super.isLChild(g)) {
                            g.getParent().setLeft(v);
                        }else {
                            g.getParent().setRight(v);
                        }
                        z = rightLeftRotate(g, v, g, g.getLeft(), v.getLeft(), v.getRight(), p.getRight());
                    }
                }
            }
            super.updateHeight(z);
        }
        return w;
    }

    private Node rightLeftRotate(Node x, Node y, Node z, Node T1, Node T2, Node T3, Node T4) {
        x.setLeft(T1);
        if (T1 != null) {
            T1.setParent(x);
        }
        x.setRight(T2);
        if (T2 != null) {
            T2.setParent(x);
        }
        updateHeight(x);

        z.setLeft(T3);
        if (T3 != null) {
            T3.setParent(z);
        }
        z.setRight(T4);
        if (T4 != null) {
            T4.setParent(z);
        }
        updateHeight(z);

        y.setLeft(x);
        x.setParent(y);
        y.setRight(z);
        z.setParent(y);
        updateHeight(y);
        return y;
    }

    private Node leftRotate(Node x, Node y, Node z, Node T1, Node T2, Node T3, Node T4) {
        x.setLeft(T1);
        if (T1 != null) {
            T1.setParent(x);
        }
        x.setRight(T2);
        if (T2 != null) {
            T2.setParent(x);
        }
        updateHeight(x);

        z.setLeft(T3);
        if (T3 != null) {
            T3.setParent(z);
        }
        z.setRight(T4);
        if (T4 != null) {
            T4.setParent(z);
        }
        updateHeight(z);

        y.setLeft(x);
        x.setParent(y);
        y.setRight(z);
        z.setParent(y);
        updateHeight(y);
        return y;
    }

    private Node leftRightRotate(Node x, Node y, Node z, Node T1, Node T2, Node T3, Node T4) {
        x.setLeft(T1);
        if (T1 != null) {
            T1.setParent(x);
        }
        x.setRight(T2);
        if (T2 != null) {
            T2.setParent(x);
        }
        updateHeight(x);

        z.setLeft(T3);
        if (T3 != null) {
            T3.setParent(z);
        }
        z.setRight(T4);
        if (T4 != null) {
            T4.setParent(z);
        }
        updateHeight(z);

        y.setLeft(x);
        x.setParent(y);
        y.setRight(z);
        z.setParent(y);
        updateHeight(y);
        return y;
    }

    private Node rightRotate(Node x, Node y, Node z, Node T1, Node T2, Node T3, Node T4) {
        x.setLeft(T1);
        if (T1 != null) {
            T1.setParent(x);
        }
        x.setRight(T2);
        if (T2 != null) {
            T2.setParent(x);
        }
        updateHeight(x);

        z.setLeft(T3);
        if (T3 != null) {
            T3.setParent(z);
        }
        z.setRight(T4);
        if (T4 != null) {
            T4.setParent(z);
        }
        updateHeight(z);

        y.setLeft(x);
        x.setParent(y);
        y.setRight(z);
        z.setParent(y);
        updateHeight(y);
        return y;
    }

    private Node getTallerChild(Node x) {
        int h1 = super.getStature(x.getLeft());
        int h2 = super.getStature(x.getRight());
        if (h1 > h2) {
            return x.getLeft();
        }else if (h1 < h2) {
            return x.getRight();
        }else {
            //等高时,选择与父亲同侧的
            if (super.isLChild(x)) {
                return x.getLeft();
            }else {
                return x.getRight();
            }
        }
    }

    private boolean avlBalanced(Node x) {
        boolean result = false;
        if (getBalFac(x) > -2 && getBalFac(x) < 2) {
            result = true;
        }
        return result;
    }

    private int getBalFac(Node x) {
        return getStature(x.getRight()) - getStature(x.getLeft());
    }

}

你可能感兴趣的:(算法导论之平衡搜索树)