一天写了RBTree,感觉自己好废柴啊

package com.mesnac.tree;

public class RBTree {
    // 根节点
    public RBNode root;

    // 空节点
    // public RBNode nil = new RBNode(null,null,null,false);

    /**
     * 插入节点
     * 
     * @param node
     */
    public void insert(RBNode node) {
        // 判断树是否为空
        if (root == null) {
            root = node;
            root.isRed = false;
            root.left = new RBNode(null, null, null, false);
            root.right = new RBNode(null, null, null, false);
            root.left.parent = root;
            root.right.parent = root;
            return;
        }
        RBNode now = root;
        // 寻找要插入的位置
        while (now.val != null) {
            if (now.val > node.val)
                now = now.left;
            else if (now.val < node.val)
                now = now.right;
            else
                return;
        }
        // 这样我们找到了相应的最后的点,将这个点替换成我们的点
        now.val = node.val;
        now.isRed = true;
        now.left = new RBNode(null, null, null, false);
        now.right = new RBNode(null, null, null, false);
        now.left.parent = now;
        now.right.parent = now;
        // 然后进行树的修正
        insertFix(now);
    }

    public void insertFix(RBNode now) {
        // 如果是根节点,上面应经修正过了,不处理
        System.out.println("现在处理" + now.val);
        if (now == root) {
            if (now.isRed) {
                now.isRed = false;
            }
            return;
        }
        // 如果插入节点的父节点是黑,不处理
        if (!now.parent.isRed) {
            return;
        }
        // 插入的节点父节点红
        if (now.parent.isRed) {
            // 判断父节点是左还是右
            // 父节点是祖父左子
            if (now.parent.parent.left.val == now.parent.val) {
                // 叔叔是红色
                if (now.parent.parent.right.isRed) {
                    now.parent.isRed = false;
                    now.parent.parent.right.isRed = false;
                    now.parent.parent.isRed = true;
                    // 当前节点变成父节点
                    
                    insertFix(now.parent.parent);
                }
                // 叔叔黑色
                else {

                    // 当前节点是右子
                    if (now.parent.right.val == now.val) {
                        now = now.parent;
                        // 左旋
                        LRC(now);
                        insertFix(now);
                    }// 当前节点左子
                    else {
                        now.parent.isRed = false;
                        now.parent.parent.isRed = true;
                        // 祖父右旋
                        //System.out.println("右旋开始了" + now.parent.parent.val);
                        LLC(now.parent.parent);
                    }
                }
            }
            // 父节点是祖父右子
            else {
                // 叔叔是红色
                if (now.parent.parent.left.isRed) {
                    now.parent.isRed = false;
                    now.parent.parent.isRed = true;
                    now.parent.parent.left.isRed = false;
                    System.out.println("变色");
                    insertFix(now.parent.parent);
                    
                }
                // 叔叔黑色
                else {
                    // 当前节点是左子
                    if (now.parent.left.val == now.val) {
                        now = now.parent;
                        // 左旋
                        System.out.println("左旋开始" + now.val);
                        RLC(now);
                        insertFix(now);
                    }// 当前节点左子
                    else {
                        now.parent.isRed = false;
                        now.parent.parent.isRed = true;
                        // 祖父右旋
                        //System.out.println("右旋开始" + now.val);
                        RRC(now.parent.parent);
                    }
                }

            }
        }

    }

    // (父节点是祖父节点的左节点,当前节点是父节点右子),传入父节点
    public void LRC(RBNode node) {
        if(node.val == 15){
            System.out.println("LRC"+node.val);
        }
        RBNode gfather = node.parent;
        gfather.left = node.right;
        node.right = node.right.left;
        gfather.left.left = node;
        
        
        gfather.left.parent = gfather;
        node.parent = gfather.left;
        node.right.parent = node;    
        
        

    }

    // (父节点是祖父节点的右孩子,当前节点是左子),传入父节点
    public void RLC(RBNode node) {
        System.out.println("RLC"+node.val);
        RBNode gfather = node.parent;
        System.out.println("RLC"+gfather.val);
        gfather.right = node.left;
        node.left = node.left.right;
        gfather.right.right = node;

        gfather.right.parent = gfather;
        node.parent = gfather.right;
        node.left.parent = node;
        System.out.println("RLCgather"+gfather.val);
        System.out.println("RLC"+node.parent.val);

    }

    // (父节点是祖父节点的左孩子,当前节点是左子)(变色),传入祖父 
    public void LLC(RBNode node) {
        // 左还是右
        //
        System.out.println("祖父节点"+node.val);
        if (node == root) {
            this.root = node.left;
            node.left = node.left.right;
            root.right = node;
            root.left.parent = root;
            root.right.parent = root;
            node.left.parent = node;
            return;
        }
        RBNode gfather = node.parent;
        System.out.println("gf"+gfather.val);
        RBNode father = node.left;
        if(gfather.left.val == node.val)
            gfather.left = node.left;
        else
            gfather.right = node.left;
        node.left = node.left.right;
        father.right = node;

        father.parent = gfather;
        node.parent = father;
        node.left.parent = node;
    }
    //父节点是祖父的右孩子,当前节点是右子(变色),传入祖父
    public void RRC(RBNode node){
        if(node == root){
            this.root = node.right;
            node.right = node.right.left;
            root.left = node;
            
            root.left.parent = node;
            root.right.parent =node;
            node.right.parent = node;
            return;
        }
        System.out.println("RRC"+node.val);
        RBNode gfather = node.parent;
        RBNode father = node.right;
        if(gfather.left.val == node.val)
            gfather.left = node.right;
        else
            gfather.right = node.right;
        node.right = node.right.left;
        father.left = node;
        
        father.parent = gfather;
        node.parent = father;
        node.right.parent = node;
        
    }

    public void remove(RBNode node) {

    }

    // 先序遍历
    public void MLR(RBNode now) {

        System.out.println(now.val);
        if (now.left.val != null)
            MLR(now.left);
        if (now.right.val != null)
            MLR(now.right);
    }

    // 中序遍历
    public void LMR(RBNode now) {
        if (now.left.val != null)
            LMR(now.left);
        System.out.println(now.val);
        if (now.right.val != null)
            LMR(now.right);
    }

    // 后序遍历
    public void LRM(RBNode now) {
        if (now.left != null)
            LRM(now.left);
        if (now.right != null)
            LRM(now.right);
        if (now.val != null)
            System.out.println(now.val);
    }

    public static void main(String args[]) {
        
        RBTree a = new RBTree();
        RBNode node = new RBNode(null, null, 12, true);
        a.insert(node);
        node = new RBNode(null, null, 1, true);
        a.insert(node);
        node = new RBNode(null, null, 9, true);
        a.insert(node);
        
        node = new RBNode(null, null, 2, true);
        a.insert(node);
        
        
        node = new RBNode(null, null, 0, true);
        a.insert(node);
        
        
        node = new RBNode(null, null, 11, true);
        a.insert(node);
        node = new RBNode(null, null, 7, true);
        a.insert(node);
        
        
        node = new RBNode(null, null, 19, true);
        a.insert(node);
        node = new RBNode(null, null, 4, true);
        a.insert(node);    
        
        node = new RBNode(null, null, 15, true);
        a.insert(node);
        //
        node = new RBNode(null, null, 18, true);
        a.insert(node);
        //System.out.println("=="+a.root.right.right.right.val);
        
        node = new RBNode(null, null, 5, true);
        a.insert(node);
        node = new RBNode(null, null, 14, true);
        a.insert(node);
        
        node = new RBNode(null, null, 13, true);
        a.insert(node);
        
        node = new RBNode(null, null, 10, true);
        a.insert(node);
        System.out.println("========begin");
        
        node = new RBNode(null, null, 16, true);
        a.insert(node);
        
        node = new RBNode(null, null, 6, true);
        a.insert(node);
        node = new RBNode(null, null, 3, true);
        a.insert(node);
        node = new RBNode(null, null, 8, true);
        a.insert(node);
        node = new RBNode(null, null, 17, true);
        a.insert(node);
        
        
        a.LMR(a.root);
        
    }

}
package com.mesnac.tree;

/**
 * 红黑树节点
 * @author 1
 *
 */
public class RBNode {
    public RBNode left;
    public RBNode right;
    public RBNode parent;
    public Integer val;
    public boolean isRed;
    
    public RBNode(){
    }
    
    public RBNode(RBNode left,RBNode right,Integer val,boolean isRed){
        this.left = left;
        this.right = right;
        this.val = val;
        this.isRed = isRed;
    }
}

 

posted @ 2012-12-14 17:36  庸蛹  阅读(228)  评论(0编辑  收藏  举报