Пример #1
0
        private RedBlackNode <T> insert(T data, RedBlackNode <T> h)
        {
            if (h == null)
            {
                return(new RedBlackNode <T>(data));
            }
            int c = comp.Compare(data, h.data);

            if (c < 0)
            {
                h.left = insert(data, h.left);
            }
            else if (c > 0)
            {
                h.right = insert(data, h.right);
            }
            else
            {
                h.data = data;
            }

            // Now for the rotating
            if (isRed(h.right) && !isRed(h.left))
            {
                h = rotateLeft(h);
            }

            if (isRed(h.left) && isRed(h.left.left))
            {
                h = rotateRight(h);
            }

            if (isRed(h.left) && isRed(h.right))
            {
                flipColors(h);
            }
            return(h);
        }
Пример #2
0
 private void flipColors(RedBlackNode <T> h)
 {
     h.left.isRed  = false;
     h.right.isRed = false;
     h.isRed       = true;
 }
Пример #3
0
 public RedBlackBST(Comparer <T> comp)
 {
     this.root = null;
     this.comp = comp;
 }