Ejemplo n.º 1
0
 private static int GetNodeID(RedBlackTreeNode<DirectoryEntry> node)
 {
     if (node == RedBlackTreeNode<DirectoryEntry>.Nil)
     {
         return -1;
     }
     else
     {
         return node.Data.ID;
     }
 }
Ejemplo n.º 2
0
 private void AssertRoot(RedBlackTreeNode <int, int> root)
 {
     Assert.AreEqual(root.Parent, null);
 }
        private void BalanceRedBlack(RedBlackTreeNode node)
        {
            var parentNode = node.Parent as RedBlackTreeNode;

            if (parentNode == null)
            {
                //We are inserting root node
                node.Color = TreeNodeColor.Black;
            }
            else if (parentNode.Color != TreeNodeColor.Black)
            {
                //Note: parent's color is Red means there must be a grand parent there.
                //Otherwise the parent will be the root node,
                //it will break the rule: 'Root node is always Black'.
                //Parent is also Red, we need to look at Uncle's color
                var uncleNode   = (RedBlackTreeNode)GetUncleTreeNode(node);
                var grandParent = parentNode.Parent as RedBlackTreeNode;
                if (uncleNode != null && uncleNode.Color != TreeNodeColor.Black)
                {
                    //New Node, parent and Uncle are both Red, push grand parent's color down
                    grandParent.Color = TreeNodeColor.Red;
                    uncleNode.Color   = TreeNodeColor.Black;
                    parentNode.Color  = TreeNodeColor.Black;
                    //When grand parent change its color, it may break the rule
                    //Still need to balance the tree from grand parent to top
                    BalanceRedBlack(grandParent);
                }
                else
                {
                    //New node and parent are both Red, make rotation based on below rules:
                    //If New node is a LEFT child and Parent node is a LEFT child
                    //If New node is a LEFT child and Parent node is a RIGHT child
                    //If New node is a RIGHT child and Parent node is a LEFT child
                    //If New node is a RIGHT child and Parent node is a RIGHT child
                    //Swap colors between parent and new node after rotation
                    if (node.Value < parentNode.Value)
                    {
                        if (parentNode.Value < grandParent.Value)
                        {
                            //LEFT + LEFT -> Right rotation
                            RotationRight(grandParent);
                        }
                        else
                        {
                            //LEFT + RIGHT -> Right rotation + Left rotation
                            RotationRight(parentNode);
                            RotationLeft(grandParent);
                        }
                    }
                    else
                    {
                        if (parentNode.Value > grandParent.Value)
                        {
                            //RIGHT + RIGHT -> Left rotation
                            RotationLeft(grandParent);
                        }
                        else
                        {
                            //RIGHT + LEFT -> Left rotation + Right rotation
                            RotationLeft(parentNode);
                            RotationRight(grandParent);
                        }
                    }
                    grandParent.Color = TreeNodeColor.Red;
                    (grandParent.Parent as RedBlackTreeNode).Color = TreeNodeColor.Black;
                }
            }
        }
Ejemplo n.º 4
0
 public void Reset()
 {
     this.node = this.startNode;
 }
Ejemplo n.º 5
0
 public RedBlackTreeIterator(RedBlackTreeNode node)
 {
     this.startNode = this.node = node;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Determines whether the specified n is leaf.
 /// </summary>
 /// <param name="n">The n.</param>
 /// <returns>
 ///     <c>true</c> if the specified n is leaf; otherwise, <c>false</c>.
 /// </returns>
 protected bool IsLeaf(RedBlackTreeNode <K, T> n)
 {
     return((n.left == null) && (n.right == null));
 }
        private void AssertStructure(RedBlackTreeNode<int, int> elem)
        {
            if (elem.Left != null)
            {
                Assert.IsTrue(elem.Left.Value < elem.Value);
                Assert.AreEqual(elem.Left.Parent, elem);
                AssertStructure(elem.Left);
            }

            if (elem.Right != null)
            {
                Assert.IsTrue(elem.Value < elem.Right.Value);
                Assert.AreEqual(elem.Right.Parent, elem);
                AssertStructure(elem.Right);
            }
        }
 private void AssertRoot(RedBlackTreeNode<int, int> root)
 {
     Assert.AreEqual(root.Parent, null);
 }
 public void UpdateAfterRotateRight(RedBlackTreeNode <RBNode> node)
 {
     UpdateAfterChildrenChange(node);
     UpdateAfterChildrenChange(node.parent);
 }
Ejemplo n.º 10
0
 private void Insert_2_SKIP(RedBlackTreeNode node)
 {
     return; // Tree is Valiid
 }
Ejemplo n.º 11
0
 public RedBlackTreeNode(TKey value, int height, RedBlackTreeNode <TKey> parent, RedBlackTreeNode <TKey> left, RedBlackTreeNode <TKey> right)
 {
     base.Value = value;
     Color      = RedBlackTreeColors.Red;
     Parent     = parent;
     LeftChild  = left;
     RightChild = right;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Determines whether the specified node is red.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <returns>
 ///     <c>true</c> if the specified node is red; otherwise, <c>false</c>.
 /// </returns>
 internal static bool IsRed(RedBlackTreeNode <TKey, TValue> node)
 {
     return((node != null) && (node.Color == NodeColor.Red));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Determines whether the specified node is black.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <returns>
 ///     <c>true</c> if the specified node is black; otherwise, <c>false</c>.
 /// </returns>
 internal static bool IsBlack(RedBlackTreeNode <TKey, TValue> node)
 {
     return((node == null) || (node.Color == NodeColor.Black));
 }
 public RedBlackTreeNode(T newData, RedBlackTreeNode newParent)
 {
     this.color = RED;
     data       = newData;
     parent     = newParent;
 }
Ejemplo n.º 15
0
 private void AssertParent(RedBlackTreeNode <int, int> child, RedBlackTreeNode <int, int> parent)
 {
     Assert.AreEqual(child.Parent, parent);
 }
        }//end-test-case


        /// <summary>
        /// Helper function to calculate the Maximum Height
        /// </summary>
        private static int GetMaxHeight(RedBlackTreeNode<int> tree)
        {
            if (tree == null)
                return 0;
            else
                return 1 + Math.Max(GetMaxHeight(tree.LeftChild), GetMaxHeight(tree.RightChild));
        }
Ejemplo n.º 17
0
 static bool GetColorSafe(RedBlackTreeNode node)
 {
     return(node != null ? node.color : black);
 }
 private void AssertParent(RedBlackTreeNode<int, int> child, RedBlackTreeNode<int, int> parent)
 {
     Assert.AreEqual(child.Parent, parent);
 }
Ejemplo n.º 19
0
        void DeleteOneChild(RedBlackTreeNode node)
        {
            // case 1
            if (node == null ||  node.parent == null)
            {
                return;
            }

            RedBlackTreeNode parent  = node.parent;
            RedBlackTreeNode sibling = node.Sibling;

            if (sibling == null)
            {
                return;
            }

            // case 2
            if (sibling.color == red)
            {
                parent.color  = red;
                sibling.color = black;
                if (node == parent.left)
                {
                    RotateLeft(parent);
                }
                else
                {
                    RotateRight(parent);
                }
                sibling = node.Sibling;
                if (sibling == null)
                {
                    return;
                }
            }

            // case 3
            if (parent.color == black &&
                sibling.color == black &&
                GetColorSafe(sibling.left) == black &&
                GetColorSafe(sibling.right) == black)
            {
                sibling.color = red;
                DeleteOneChild(parent);
                return;
            }

            // case 4
            if (parent.color == red &&
                sibling.color == black &&
                GetColorSafe(sibling.left) == black &&
                GetColorSafe(sibling.right) == black)
            {
                sibling.color = red;
                parent.color  = black;
                return;
            }

            // case 5
            if (node == parent.left &&
                sibling.color == black &&
                GetColorSafe(sibling.left) == red &&
                GetColorSafe(sibling.right) == black)
            {
                sibling.color = red;
                if (sibling.left != null)
                {
                    sibling.left.color = black;
                }
                RotateRight(sibling);
            }
            else if (node == parent.right &&
                     sibling.color == black &&
                     GetColorSafe(sibling.right) == red &&
                     GetColorSafe(sibling.left) == black)
            {
                sibling.color = red;
                if (sibling.right != null)
                {
                    sibling.right.color = black;
                }
                RotateLeft(sibling);
            }

            // case 6
            sibling = node.Sibling;
            if (sibling == null)
            {
                return;
            }
            sibling.color = parent.color;
            parent.color  = black;
            if (node == parent.left)
            {
                if (sibling.right != null)
                {
                    sibling.right.color = black;
                }
                RotateLeft(parent);
            }
            else
            {
                if (sibling.left != null)
                {
                    sibling.left.color = black;
                }
                RotateRight(parent);
            }
        }
Ejemplo n.º 20
0
 public RedBlackTreeNodeEventArgs(RedBlackTreeNode node)
 {
     this.Node = node;
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Clears this instance.
 /// </summary>
 public void Clear()
 {
     root = null;
     size = 0;
 }