Пример #1
0
 private void MidOrder(RBNode <T> current)
 {
     if (current == null)
     {
         return;
     }
     MidOrder(current.Lchild);
     orderString += current.Data.ToString() + " ";
     MidOrder(current.Rchild);
 }
Пример #2
0
        public void insert(RBNode <T> node)
        {
            RBNode <T> current = null;//表示最后node的父节点
            RBNode <T> x       = this.root;

            while (x != null)
            {
                current = x;
                int cmp = node.Data.CompareTo(x.Data);
                if (cmp < 0)
                {
                    x = x.Lchild;
                }
                else
                {
                    x = x.Rchild;
                }
            }
            node.Parent = current;
            if (current != null)
            {
                int cmp = node.Data.CompareTo(current.Data);
                if (cmp < 0)
                {
                    current.Lchild = node;
                }
                else
                {
                    current.Rchild = node;
                }
            }
            else
            {
                this.root = node;
            }
            insertFixUp(node);
        }
Пример #3
0
 public RBNode(T key)
 {
     Data   = key;
     Color  = true;
     Lchild = Rchild = Parent = null;
 }
Пример #4
0
        //node表示待修正的节点,即后继节点的子节点(因为后继节点被挪到删除节点的位置去了)
        private void removeFixUp(RBNode <T> node, RBNode <T> parent)
        {
            RBNode <T> other;

            while ((node == null || !node.Color) && (node != this.root))
            {
                if (parent.Lchild == node)
                {                          //node是左子节点,下面else与这里的刚好相反
                    other = parent.Rchild; //node的兄弟节点
                    if (other.Color)
                    {                      //case1: node的兄弟节点other是红色的
                        other.Color  = false;
                        parent.Color = true;
                        LeftRotate(parent);
                        other = parent.Rchild;
                    }

                    //case2: node的兄弟节点other是黑色的,且other的两个子节点也都是黑色的
                    if ((other.Lchild == null || !other.Lchild.Color) &&
                        (other.Rchild == null || !other.Rchild.Color))
                    {
                        other.Color = true;
                        node        = parent;
                        parent      = parentOf(node);
                    }
                    else
                    {
                        //case3: node的兄弟节点other是黑色的,且other的左子节点是红色,右子节点是黑色
                        if (other.Rchild == null || !other.Rchild.Color)
                        {
                            other.Lchild.Color = false;
                            other.Color        = true;
                            RightLotate(other);//RightRotate(other);
                            other = parent.Rchild;
                        }

                        //case4: node的兄弟节点other是黑色的,且other的右子节点是红色,左子节点任意颜色
                        other.Color        = parent.Color;
                        parent.Color       = false;
                        other.Rchild.Color = false;
                        LeftRotate(parent);
                        node = this.root;
                        break;
                    }
                }
                else
                { //与上面的对称
                    other = parent.Lchild;

                    if (other.Color)
                    {
                        // Case 1: node的兄弟other是红色的
                        other.Color  = false;
                        parent.Color = true;
                        RightLotate(parent);
                        other = parent.Lchild;
                    }

                    if ((other.Lchild == null || !other.Lchild.Color) &&
                        (other.Rchild == null || other.Rchild.Color))
                    {
                        // Case 2: node的兄弟other是黑色,且other的俩个子节点都是黑色的
                        other.Color = true;
                        node        = parent;
                        parent      = parentOf(node);
                    }
                    else
                    {
                        if (other.Lchild == null || !other.Lchild.Color)
                        {
                            // Case 3: node的兄弟other是黑色的,并且other的左子节点是红色,右子节点为黑色。
                            other.Rchild.Color = false;
                            other.Color        = true;
                            LeftRotate(other);
                            other = parent.Lchild;
                        }

                        // Case 4: node的兄弟other是黑色的;并且other的左子节点是红色的,右子节点任意颜色
                        other.Color        = parent.Color;
                        parent.Color       = false;
                        other.Lchild.Color = false;
                        RightLotate(parent);
                        node = this.root;
                        break;
                    }
                }
            }
            if (node != null)
            {
                node.Color = false;
            }
        }
Пример #5
0
 public RBNode <T> parentOf(RBNode <T> node)
 { //获得父节点
     return(node != null ? node.Parent : null);
 }
Пример #6
0
        private void remove(RBNode <T> node)
        {
            RBNode <T> child, parent;
            Boolean    color;

            //被删除的节点  左右子节点都不为空 的情况
            if ((node.Lchild != null) && (node.Rchild != null))
            {
                //先找到被删除节点的后继节点,用它来取代被删除节点的位置
                RBNode <T> replace = node;
                //1)获取后继节点
                replace = replace.Rchild;
                while (replace.Lchild != null)
                {
                    replace = replace.Lchild;
                }
                //  2). 处理“后继节点”和“被删除节点的父节点”之间的关系
                if (parentOf(node) != null)
                {
                    if (node == parentOf(node).Lchild)
                    {
                        parentOf(node).Rchild = replace;
                    }
                    else
                    {
                        parentOf(node).Rchild = replace;
                    }
                }
                else
                {
                    this.root = replace;
                }

                //3)处理 后继节点的子节点 和 被删除节点的子节点 之间的关系
                child  = replace.Rchild;//后继节点肯定不存在左子节点
                parent = parentOf(replace);
                color  = replace.Color;
                if (parent == node)
                {
                    parent = replace;
                }
                else
                {
                    if (child != null)
                    {
                        child.Parent = parent;
                    }
                    parent.Lchild      = child;
                    replace.Rchild     = node.Rchild;
                    node.Rchild.Parent = replace;
                }
                replace.Parent     = node.Parent;
                replace.Color      = node.Color;
                replace.Lchild     = node.Lchild;
                node.Lchild        = node.Lchild;
                node.Lchild.Parent = replace;
                if (!color)
                {
                    removeFixUp(child, parent);
                }
                node = null;
                return;
            }
        }
Пример #7
0
        private void insertFixUp(RBNode <T> node)
        {
            RBNode <T> parent, gparent;

            while (((parent = parentOf(node)) != null) && parent.Color)
            {
                gparent = parentOf(parent);
                if (parent == gparent.Lchild)
                {
                    RBNode <T> uncle = gparent.Rchild;
                    if (uncle != null && uncle.Color)
                    {
                        parent.Color  = false;
                        uncle.Color   = false;
                        gparent.Color = true;
                        //setBlack(parent);
                        //setBlack(uncle);
                        //setRed(gparent);
                        node = gparent;
                        continue;
                    }
                    if (node == parent.Rchild)
                    {
                        LeftRotate(parent);
                        RBNode <T> tmp = parent;
                        parent = node;
                        node   = tmp;
                    }
                    parent.Color  = false;
                    gparent.Color = true;
                    //setBlack(parent);
                    //setRed(gparent);
                    RightLotate(gparent);
                }
                else
                {
                    RBNode <T> uncle = gparent.Lchild;
                    if (uncle != null && uncle.Color)
                    {
                        parent.Color  = false;
                        uncle.Color   = false;
                        gparent.Color = true;
                        //setBlack(parent);
                        //setBlack(uncle);
                        //setRed(parent);
                        node = gparent;
                        continue;
                    }
                    if (node == parent.Lchild)
                    {
                        RightLotate(parent);
                        RBNode <T> tmp = parent;
                        parent = node;
                        node   = tmp;
                    }
                    parent.Color  = false;
                    gparent.Color = true;
                    //setBlack(parent);
                    //setRed(gparent);
                    LeftRotate(gparent);
                }
            }
            this.root.Color = false;
            //setBlack(this.root);
        }
Пример #8
0
        public void insert(T key)
        {
            RBNode <T> node = new RBNode <T>(key);

            insert(node);
        }