Пример #1
0
 /// <summary>
 /// Generates a leaf
 /// </summary>
 public RBNode(RBNode parent)
 {
     this.color       = Color.Black;
     this.leaf        = Leaf.LEAF;
     this.RBNodeLeft  = null;
     this.RBNodeRight = null;
     this.parent      = parent;
 }
Пример #2
0
        public RBNode AddNode(RBNode root, int key)
        {
            RBNode newNode = new RBNode(key, Color.Black, null);

            InsertNode(root, newNode);

            RepairTree(newNode);
            return(newNode);
        }
Пример #3
0
 public RedBlackTree(int[] data)
 {
     this.root   = AddNode(null, data[0]);
     this.output = new List <int>();
     for (int i = 1; i < data.Length; i++)
     {
         AddNode(this.root, data[i]);
     }
 }
Пример #4
0
 public RBNode(int key, Color color, RBNode parent)
 {
     this.RBNodeLeft  = new RBNode(parent);
     this.RBNodeRight = new RBNode(parent);
     this.parent      = parent;
     this.key         = key;
     this.color       = color;
     this.leaf        = Leaf.not;
 }
Пример #5
0
        public void InOrder(RBNode node)
        {
            if (node == null)
            {
                return;
            }

            if (node.leaf != Leaf.LEAF)
            {
                InOrder(node.RBNodeLeft);
                this.output.Add(node.Key);
                InOrder(node.RBNodeRight);
            }
            else
            {
                return;
            }
        }
Пример #6
0
 public RBNode Search(int key, RBNode startRBNode)
 {
     while (startRBNode != null)
     {
         if (startRBNode.Key == key)
         {
             return(startRBNode);
         }
         else if (startRBNode.Key < key)
         {
             startRBNode = startRBNode.RBNodeRight;
         }
         else
         {
             startRBNode = startRBNode.RBNodeLeft;
         }
     }
     return(null);
 }
Пример #7
0
        private void RotateLeft(RBNode node)
        {
            RBNode pivot  = node;
            RBNode root   = node.Parent;
            RBNode parent = root.Parent;

            pivot.Parent     = parent;
            root.RBNodeRight = pivot.RBNodeLeft;
            if (parent != this.root.Parent)
            {
                parent.RBNodeLeft = pivot;
            }
            else
            {
                this.root = pivot;
            }
            pivot.RBNodeLeft = root;
            root.Parent      = pivot;
        }
Пример #8
0
        private void Case2(RBNode node)
        {
            if (node == node.Grandparent().RBNodeLeft.RBNodeRight)
            {
                RotateLeft(node.Parent);
                if (node.RBNodeLeft.leaf != Leaf.LEAF)
                {
                    node = node.RBNodeLeft;
                }
            }
            else if (node == node.Grandparent().RBNodeRight.RBNodeLeft)
            {
                RotateRight(node.Parent);
                if (node.RBNodeRight.leaf != Leaf.LEAF)
                {
                    node = node.RBNodeRight;
                }
            }



            if (node == node.Parent.RBNodeLeft)
            {
                RotateRight(node.Parent);
            }
            else
            {
                RotateLeft(node.Parent);
            }
            node.Parent.Color = Color.Black;

            if (node.Grandparent() != null)
            {
                node.Grandparent().Color = Color.Red;
            }
        }