红黑树add方法

public Node<T> add(Node<T> n) {
            if (n == null)
                return null;

            // 比根节点元素小
            
            if ((par.compare(n.value, this.value))>0) {
                if (this.left != null) {
                    this.left.add(n);
                } else {
                    // 如果为空直接添加
                    this.left = n;
                    n.parent = this;
                }
                // 比根节点元素大放右边
            } else if ((par.compare(n.value, this.value))==0) {
                return null;
            } else {
                if (this.right != null) {
                    this.right.add(n);
                } else {
                    // 如果为空直接添加
                    this.right = n;
                    n.parent = this;
                }
            }
        
            colorChange(n);

            size++;
            return n;
        }

 

posted @ 2020-09-25 21:33  蓝冰nine  阅读(162)  评论(0)    收藏  举报