红黑树add方法

时间:2020-09-25 22:08:22   收藏:0   阅读:59
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;
        }

 

原文:https://www.cnblogs.com/lanbingnie/p/13732731.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!