java红黑树手写

本文详细介绍了红黑树的特性,并提供了Java实现的红黑树插入方法,包括插入节点后如何保持红黑树的平衡,通过左旋、右旋和颜色调整等操作来修复树的平衡状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

特点

  1. 节点颜色有红色和黑色
  2. 根节点必为黑色
  3. 所有叶子节点都是黑色
  4. 不会有连续的红色节点

代码

package tree.rbt;
/**
 * @ClassName RBTree
 * @Description 红黑树
 * @Author guobenqi
 * @Date 2021/8/26 17:04
 */
public class RBTree  <K extends Comparable<K>,V>{
    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private  RBNode root;
    /**
     * 中序打印二叉树
     * */
    public void inOrderPrint(){
        inOrderPrint(this.root);
    }
    private void inOrderPrint(RBNode root){
        if (root != null){
            inOrderPrint(root.left);
            System.out.println("key:"+root.key+",value:"+root.value);
            inOrderPrint(root.right);
        }
    }

    public RBNode getRoot() {
        return root;
    }

    /**
     * 插入方法
     * */
    public void insert(K key,V value){
        RBNode node = new RBNode();
        node.setKey(key);
        node.setValue(value);
        /*新节点一定红色*/
        node.setColor(RED);

        insert(node);


    }
    private void insert(RBNode node){
        RBNode parent = null;
        RBNode x = this.root;

        while(x != null){
            parent = x;
            int cmp = node.key.compareTo(x.key);
            if (cmp > 0){
                x = x.right;
            }else if(cmp < 0){
                x = x.left;
            }else{
                x.setValue(node.getValue());
                return;
            }
        }
        node.parent = parent;
        if (parent != null){
            int cmp = node.key.compareTo(parent.key);
            if (cmp > 0){
                parent.right = node;
            }else{
                parent.left = node;
            }
        }else{
            this.root = node;
        }
        //重排树
        insertFixup(node);
    }
    /**’插入后修复红黑树平衡
     *
     * 修复各种平衡 的情况
     * 1. 红黑树为空,直接将根节点染黑
     * 2. 插入节点已经存在,不需要处理
     * 3. 插入节点的父节点是黑的,不需要处理
     * 4. 插入节点的父节点是红色的<需要处理的情况>
     *       4.1 叔-父 双红,将爸爸和叔叔都染黑,将爷爷染红。再以爷爷为当前节点,进行下一轮处理
     *       4.2 叔叔节点不存在,或者叔叔为黑,父节点为爷爷节点的左子树
     *              4.2.1:插入节点是父节点的左子树,则需要将爸爸染黑,将爷爷染红,以爷爷右旋,
     *              4.2.2:插入节点是父节点的右子树,需要将爸爸左旋,然后得到4.2.1情况指定爸爸节点进行下一轮处理
     *       4.3 叔叔节点不存在,或者叔叔为黑,父节点为爷爷节点的右子树
     *              4.3.1:插入节点是父节点的右子树,则需要将爸爸染黑,将爷爷染红,以爷爷左旋,
     *      *       4.3.2:插入节点是父节点的左子树,需要将爸爸右旋,然后得到4.3.1情况指定爸爸节点进行下一轮处理
     * */
    public void insertFixup(RBNode node){
        this.root.setColor(BLACK);
        RBNode parent = parentOf(node);
        RBNode gparent = parentOf(parent);

        if (parent != null && isRed(parent)){
            RBNode uncle = null;

            if (parent == gparent.left){
                uncle = gparent.right;
                //  4.1 叔-父 双红,将爸爸和叔叔都染黑,将爷爷染红。再以爷爷为当前节点,进行下一轮处理
                if (uncle != null && isRed(uncle)){
                    setBlack(uncle);
                    setBlack(parent);
                    setRed(gparent);
                    insertFixup(gparent);
                    return;
                }
                //4.2 叔叔节点不存在,或者叔叔为黑,父节点为爷爷节点的左子树
                if (uncle == null || isBlack(uncle)){
                    //4.2.1:插入节点是父节点的左子树,则需要将爸爸染黑,将爷爷染红,以爷爷右旋,
                    if (node == parent.left){
                        setBlack(parent);
                        setRed(gparent);
                        rightRotate(gparent);
                        return;
                    }
                    //4.2.2:插入节点是父节点的右子树,需要将爸爸左旋,然后得到4.2.1情况指定爸爸节点进行下一轮处理
                    if (node == parent.right){
                        leftRotate(parent);
                        insertFixup(parent);
                        return;
                    }

                }
            }else{//父节点为爷爷节点的you子树
                uncle = gparent.left;
                //  4.1 叔-父 双红,将爸爸和叔叔都染黑,将爷爷染红。再以爷爷为当前节点,进行下一轮处理
                if (uncle != null && isRed(uncle)){
                    setBlack(uncle);
                    setBlack(parent);
                    setRed(gparent);
                    insertFixup(gparent);
                    return;
                }

                if (uncle == null || isBlack(uncle)){
                    //4.3.1:插入节点是父节点的右子树,则需要将爸爸染黑,将爷爷染红,以爷爷左旋,
                    if (node == parent.right){
                        setBlack(parent);
                        setRed(gparent);
                        leftRotate(gparent);
                        return;
                    }
                    // 4.3.2:插入节点是父节点的左子树,需要将爸爸右旋,然后得到4.3.1情况指定爸爸节点进行下一轮处理
                    if (node == parent.left){
                        rightRotate(parent);
                        insertFixup(parent);
                        return;
                    }

                }
            }
        }
    }
    /**
     *
     * 左旋
     * 1. 以X为基点逆时针旋转
	 *	2. X的父节点被x原来的右孩子Y取代
	 *	3. c保持不变
	 *	4. Y节点原来的左孩子c变成X的右孩子
     * */
    private void leftRotate(RBNode x){
        RBNode y = x.right;
        x.right = y.left;
        if (y.left != null){
            y.left.parent = x;
        }
        if (x.parent != null){
            y.parent = x.parent;
            if (x == x.parent.left){
                x.parent.left = y;
            }else{
                x.parent.right = y;
            }
        }else{
            this.root = y;
        }
        x.parent = y;
        y.left = x;
    }
    /**
     * 右旋
     * */
    private void rightRotate(RBNode y){
        RBNode x = y.left;
       y.left = x.right;
       if (x.right != null){
           x.right.parent = y;
       }
       if (y.parent != null){
           if (y.parent.left == y){
               y.parent.left = x;
           }else {
               y.parent.right = x;
           }
       }else{
           this.root = x;
       }
       y.parent = x;
       x.right = y;
    }
    //获取当前节点的父节点
    private RBNode parentOf(RBNode node){
        if (node != null){
            return node.parent;
        }
        return null;
    }
    //节点是否是红色
    private boolean isRed(RBNode node){
        if (node != null){
            return node.color == RED;
        }
        return false;
    }
    //节点是否为黑色
    private boolean isBlack(RBNode node){
        if (node != null){
            return node.color == BLACK;
        }
        return false;
    }
    //设置红色
    private void setRed(RBNode node){
        if (node != null){
            node.color = RED;
        }
    }
    private void setBlack(RBNode node){
        if (node != null){
            node.color = BLACK;
        }
    }
    static class RBNode <K extends Comparable<K>,V>{
        private RBNode parent;
        private RBNode left;
        private RBNode right;
        private boolean color;
        private K key;
        private V value;

        public RBNode() {
        }


        public RBNode(RBNode parent, RBNode left, RBNode right, boolean color, K key, V value) {
            this.parent = parent;
            this.left = left;
            this.right = right;
            this.color = color;
            this.key = key;
            this.value = value;
        }

        public RBNode getParent() {
            return parent;
        }

        public void setParent(RBNode parent) {
            this.parent = parent;
        }

        public RBNode getLeft() {
            return left;
        }

        public void setLeft(RBNode left) {
            this.left = left;
        }

        public RBNode getRight() {
            return right;
        }

        public void setRight(RBNode right) {
            this.right = right;
        }

        public boolean isColor() {
            return color;
        }

        public void setColor(boolean color) {
            this.color = color;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值