Beispiel #1
0
        public RBTreeNode FindNode(int value)
        {
            RBTreeNode node = Root;

            while (IsNil(node))
            {
                if (node.Value == value)
                {
                    return(node);
                }

                if (value <= node.Value)
                {
                    node = node.Left;
                }
                else
                {
                    node = node.Right;
                }
            }

            return(null);
        }
Beispiel #2
0
 public Nil(RBTreeNode parent)
 {
     Value  = 0;
     Color  = RBTreeNodeColors.Black;
     Parent = parent;
 }
Beispiel #3
0
        private void FixDeledTreeBalance(RBTreeNode delNode)
        {
            if (delNode == null || delNode.Parent == null)
            {
                return;
            }


            RBTreeNode fahter = delNode.Parent;
            RBTreeNode bro    = fahter.Left;
            bool       isLeft = (fahter.Left == delNode);

            if (delNode == fahter.Left)
            {
                bro = delNode.Parent.Right;
            }

            //Case 1: father is red
            if (fahter.Color == RBTreeNodeColors.Red)
            {
                fahter.Color = RBTreeNodeColors.Black;
                bro.Color    = RBTreeNodeColors.Red;

                return;
            }

            //Case 2:parent is black,brother is red
            if (bro.Color == RBTreeNodeColors.Red)
            {
                fahter.Color = RBTreeNodeColors.Red;
                bro.Color    = RBTreeNodeColors.Black;
                if (isLeft)
                {
                    LeftRotation(fahter);
                }
                else
                {
                    RightRotation(fahter);
                }

                return;
            }

            //case 3: parent is black,brohter is black,brother right is red
            if (bro.Right != null && bro.Right.Color == RBTreeNodeColors.Red)
            {
                LeftRotation(bro);
                FixDeledTreeBalance(delNode);

                return;
            }
            //case 4: parent is black,brohter is black,brother left is red
            if (bro.Left != null && bro.Left.Color == RBTreeNodeColors.Red)
            {
                RightRotation(bro);
                FixDeledTreeBalance(delNode);

                return;
            }

            //case 5: parent is black,brohter is black,sons of brother  is black
            bro.Color = RBTreeNodeColors.Red;
            FixDeledTreeBalance(fahter);
        }
Beispiel #4
0
 private bool IsNil(RBTreeNode node)
 {
     return(node is Nil);
 }
Beispiel #5
0
        private void RB_Delete_Fixup(RBTreeNode x)
        {
            RBTreeNode bro = null;

            while (x != Root && x.Color == RBTreeNodeColors.Black)
            {
                if (x == x.Parent.Left)
                {
                    bro = x.Parent.Right;
                    if (bro.Color == RBTreeNodeColors.Red)
                    {
                        bro.Color      = RBTreeNodeColors.Black;
                        x.Parent.Color = RBTreeNodeColors.Red;
                        LeftRotation(x.Parent);
                        bro = x.Parent.Right;
                    }

                    if (bro.Left.Color == RBTreeNodeColors.Black &&
                        bro.Right.Color == RBTreeNodeColors.Black)
                    {
                        bro.Color = RBTreeNodeColors.Red;
                        x         = x.Parent;
                    }
                    else
                    {
                        if (bro.Left.Color == RBTreeNodeColors.Red)
                        {
                            bro.Left.Color = RBTreeNodeColors.Black;
                            bro.Color      = RBTreeNodeColors.Red;
                            RightRotation(bro);
                            bro = x.Parent.Right;
                        }

                        bro.Color       = x.Parent.Color;
                        x.Parent.Color  = RBTreeNodeColors.Black;
                        bro.Right.Color = RBTreeNodeColors.Black;
                        LeftRotation(x.Parent);
                        x = Root;
                    }
                }
                else
                {
                    bro = x.Parent.Left;
                    if (bro.Color == RBTreeNodeColors.Red)
                    {
                        bro.Color      = RBTreeNodeColors.Black;
                        x.Parent.Color = RBTreeNodeColors.Red;
                        RightRotation(x.Parent);
                        bro = x.Parent.Left;
                    }

                    if (bro.Left.Color == RBTreeNodeColors.Black &&
                        bro.Right.Color == RBTreeNodeColors.Black)
                    {
                        bro.Color = RBTreeNodeColors.Red;
                        x         = x.Parent;
                    }
                    else
                    {
                        if (bro.Right.Color == RBTreeNodeColors.Red)
                        {
                            bro.Left.Color = RBTreeNodeColors.Black;
                            bro.Color      = RBTreeNodeColors.Red;
                            LeftRotation(bro);
                            bro = x.Parent.Right;
                        }

                        bro.Color       = x.Parent.Color;
                        x.Parent.Color  = RBTreeNodeColors.Black;
                        bro.Right.Color = RBTreeNodeColors.Black;
                        RightRotation(x.Parent);
                        x = Root;
                    }
                }
            }

            x.Color = RBTreeNodeColors.Black;
        }