Ejemplo n.º 1
0
 public void InOrder(RedBlackNode node)
 {
     if (node != null)
     {
         InOrder(node.leftChild);
         // If the node has no parent it is the root
         if (node.parent == null)
         {
             Console.WriteLine(" | " + node.key.ToString() + " - " + node.color + " | - Root");
         }
         else
         {
             Console.WriteLine(" | " + node.key.ToString() + " - " + node.color);
         }
         if (node.rightChild == null && node.leftChild == null)
         {
             numOfLeafs++;
         }
         InOrder(node.rightChild);
     }
 }
Ejemplo n.º 2
0
        private void Rebalance(RedBlackNode node)
        {
            if (node != null)
            {
                // Always make sure the root is black. Brute force assigning works here because
                // this function does not act as if the local parameter is the root like the rotation functions do
                if (root.Red)
                {
                    root.SetColor("BLACK");
                }

                if (node.Red)
                {
                    if (node.parent.Red)
                    {
                        if (node.parent.rightChild != null && node == node.parent.rightChild)
                        {
                            if (node.parent.parent.rightChild != null && node.parent == node.parent.parent.rightChild)
                            {
                                if (node.parent.parent.leftChild == null || node.parent.parent.leftChild.Black)
                                {
                                    LeftRotation(node.parent.parent);
                                }
                                else if (node.parent.parent.leftChild.Red)
                                {
                                    ColorFlip(node);
                                }
                            }
                            else if (node.parent.parent.leftChild != null && node.parent == node.parent.parent.leftChild)
                            {
                                if (node.parent.parent.rightChild == null || node.parent.parent.rightChild.Black)
                                {
                                    LeftRightRotation(node.parent.parent);
                                }
                                else if (node.parent.parent.rightChild.Red)
                                {
                                    ColorFlip(node);
                                }
                            }
                        }
                        else if (node.parent.leftChild != null && node == node.parent.leftChild)
                        {
                            if (node.parent.parent.leftChild != null && node.parent == node.parent.parent.leftChild)
                            {
                                if (node.parent.parent.rightChild == null || node.parent.parent.rightChild.Black)
                                {
                                    RightRotation(node.parent.parent);
                                }
                                else if (node.parent.parent.rightChild.Red)
                                {
                                    ColorFlip(node);
                                }
                            }
                            else if (node.parent.parent.rightChild != null && node.parent == node.parent.parent.rightChild)
                            {
                                if (node.parent.parent.leftChild == null || node.parent.parent.leftChild.Black)
                                {
                                    RightLeftRotation(node.parent.parent);
                                }
                                else if (node.parent.parent.leftChild.Red)
                                {
                                    ColorFlip(node);
                                }
                            }
                        }
                    }
                }
            }
        }