예제 #1
0
 void Solve_RBT_Violation(ref Red_Black_Tree_Node Node_To_Be_Inserted)
 {
     // If Parent Is A Black Node
     if (Node_To_Be_Inserted.Parent != null && Node_To_Be_Inserted.Parent.Color == true)
     {
         return;
     }
     // If Parent & Uncle/Sibling Both Are Red Nodes Then Re-Coloring The Entire RBT
     // Node_To_Be_Inserted.Parent.Parent.Left_Child.Color - > Left Sibling/Uncle Color ( Can Be Parent Also In Mirror Images )
     // Node_To_Be_Inserted.Parent.Parent.Right_Child.Color - > Right Sibling/Uncle Color ( Can Be Parent Also In Mirror Images )
     if ((Node_To_Be_Inserted.Parent != null) && (Node_To_Be_Inserted.Parent.Parent != null) && (Node_To_Be_Inserted.Parent.Parent.Left_Child == null || Node_To_Be_Inserted.Parent.Parent.Left_Child.Color == false) && (Node_To_Be_Inserted.Parent.Parent.Right_Child == null || Node_To_Be_Inserted.Parent.Parent.Right_Child.Color == false))
     {
         if (Node_To_Be_Inserted.Parent.Parent.Left_Child != null)
         {
             Node_To_Be_Inserted.Parent.Parent.Left_Child.Color = true;
         }
         if (Node_To_Be_Inserted.Parent.Parent.Right_Child != null)
         {
             Node_To_Be_Inserted.Parent.Parent.Right_Child.Color = true;
         }
         if (Node_To_Be_Inserted.Parent.Parent != null)
         {
             Node_To_Be_Inserted.Parent.Parent.Color = false;
             Solve_RBT_Violation(ref Node_To_Be_Inserted.Parent.Parent);
         }
         return;
     }
     // If Parent Is Red & Uncle/Sibling Is Black
     if ((Node_To_Be_Inserted.Parent != null) && (Node_To_Be_Inserted.Parent.Parent != null) && Node_To_Be_Inserted.Parent.Parent.Left_Child.Color == true && Node_To_Be_Inserted.Parent.Color == false)
     {
         return;
         // Perform Rotations
     }
 }
예제 #2
0
        public Red_Black_Tree_Node Insert(ref Red_Black_Tree_Node Current_Node, ref int Value, ref Red_Black_Tree_Node Parent)
        {
            if (Root_Node == null)
            {
                // Root Should Be Black
                Current_Node = new Red_Black_Tree_Node(Value, true, ref Parent);
                return(Current_Node);
            }
            if (Current_Node == null)
            {
                // If Equal To Null Means It's The Position Where The New Node Is To Be Inserted
                Current_Node = new Red_Black_Tree_Node(Value, false, ref Parent);
                Solve_RBT_Violation(ref Current_Node);
                return(Current_Node);
            }
            if (Value > Current_Node.Data)
            {
                Current_Node.Right_Child = Insert(ref Current_Node.Right_Child, ref Value, ref Current_Node);
                return(Current_Node);
            }
            if (Value < Current_Node.Data)
            {
                Current_Node.Left_Child = Insert(ref Current_Node.Left_Child, ref Value, ref Current_Node);
                return(Current_Node);
            }

            return(Current_Node);
        }
예제 #3
0
 public Red_Black_Tree_Node(int Data_, bool Color_, ref Red_Black_Tree_Node Parent_)
 {
     Parent      = Parent_;
     Color       = Color_;
     Data        = Data_;
     Left_Child  = null;
     Right_Child = null;
 }
예제 #4
0
 public void In_Order_Traversal(ref Red_Black_Tree_Node Current_Node)
 {
     if (Current_Node != null)
     {
         In_Order_Traversal(ref Current_Node.Left_Child);
         Console.WriteLine("{0} ", Current_Node.Data);
         In_Order_Traversal(ref Current_Node.Right_Child);
     }
     else
     {
         return;
     }
 }
예제 #5
0
 public void Insert(int Value)
 {
     Root_Node = Insert(ref Root_Node, ref Value, ref Root_Node);
 }
예제 #6
0
 public Red_Black_Tree()
 {
     Root_Node = null;
 }
예제 #7
0
 public void Delete(ref Red_Black_Tree_Node Current_Node, int Value)
 {
 }