Beispiel #1
0
        public void InOrderPredecessor_RootLeft()
        {
            RedBlackNode<int> rootLeft = new RedBlackNode<int>(50)
            {
                Left = new RedBlackNode<int>(25)
            };

            Assert.AreEqual<int>(25, rootLeft.InOrderPredecessor.Value);
        }
Beispiel #2
0
        public void InOrderPredecessor_RootRight()
        {
            RedBlackNode<int> rootRight = new RedBlackNode<int>(50)
            {
                Right = new RedBlackNode<int>(75)
            };

            Assert.IsNull(rootRight.InOrderPredecessor);
        }
Beispiel #3
0
 internal static RedBlackNode LeftBalance(object key, object val, RedBlackNode insert, RedBlackNode right)
 {
     if (insert is RedNode && insert.Left is RedNode)
     {
         return(MakeRed(insert.Key, insert.Value, insert.Left.Blacken(), MakeBlack(key, val, insert.Right, right)));
     }
     else if (insert is RedNode && insert.Right is RedNode)
     {
         return(MakeRed(insert.Right.Key, insert.Right.Value,
                        MakeBlack(insert.Key, insert.Value, insert.Left, insert.Right.Left),
                        MakeBlack(key, val, insert.Right.Right, right)));
     }
     else
     {
         return(MakeBlack(key, val, insert, right));
     }
 }
Beispiel #4
0
        internal static BlackNode MakeBlack(object key, object val, RedBlackNode left, RedBlackNode right)
        {
            if (left is null && right is null)
            {
                if (val is null)
                {
                    return(new BlackNode(key));
                }
                return(new BlackValueNode(key, val));
            }
            if (val is null)
            {
                return(new BlackBranchNode(key, left, right));
            }

            return(new BlackBranchValueNode(key, val, left, right));
        }
Beispiel #5
0
        ///<summary>
        /// RotateRight
        /// Rebalance the tree by rotating the nodes to the right
        ///</summary>
        private void RotateRight(RedBlackNode <TKey, TValue> x)
        {
            // pushing node x down and to the Right to balance the tree. x's Left child (y)
            // replaces x (since x < y), and y's Right child becomes x's Left child
            // (since it's < x but > y).

            RedBlackNode <TKey, TValue> y = x.Left;                      // get x's Left node, this becomes y

            // set x's Right link
            x.Left = y.Right;                                   // y's Right child becomes x's Left child

            // modify parents
            if (y.Right != sentinelNode)
            {
                y.Right.Parent = x;                             // sets y's Right Parent to x
            }
            if (y != sentinelNode)
            {
                y.Parent = x.Parent; // set y's Parent to x's Parent
            }
            if (x.Parent != null)    // null=rbTree, could also have used rbTree
            {                        // determine which side of it's Parent x was on
                if (x == x.Parent.Right)
                {
                    x.Parent.Right = y;                 // set Right Parent to y
                }
                else
                {
                    x.Parent.Left = y;                  // set Left Parent to y
                }
            }
            else
            {
                rbTree = y;                                             // at rbTree, set it to y
            }
            // link x and y
            y.Right = x;                                        // put x on y's Right
            if (x != sentinelNode)                              // set y as x's Parent
            {
                x.Parent = y;
            }

            x.Count = x.Left.Count + x.Right.Count + 1;
            y.Count = y.Left.Count + y.Right.Count + 1;
        }
Beispiel #6
0
    private void InsertNode(T value, RedBlackNode current)
    {
        if (value.CompareTo(current.NodeValue) == -1)
        {
            if (current.LeftNode == null)
            {
                var node = new RedBlackNode(value)
                {
                    Color      = NodeColor.Red,
                    ParentNode = current,
                };
                current.LeftNode = node;
                _nodeCount++;
            }
            else
            {
                InsertNode(value, current.LeftNode);
                return;
            }
        }
        else if (value.CompareTo(current.NodeValue) == 1)
        {
            if (current.RightNode == null)
            {
                var node = new RedBlackNode(value)
                {
                    Color      = NodeColor.Red,
                    ParentNode = current,
                };
                current.RightNode = node;
                _nodeCount++;
            }
            else
            {
                InsertNode(value, current.RightNode);
                return;
            }
        }

        // Make sure we didn't violate the rules of a red/black tree.
        CheckNode(current);

        // Automatically make sure the root node is black.
        _rootNode.Color = NodeColor.Black;
    }
Beispiel #7
0
 private void FixChildColors(RedBlackNode current)
 {
     if (current.Color == RedBlackNode.NodeColor.RED)
     {
         if (current.LeftNode != null &&
             current.LeftNode.Color == RedBlackNode.NodeColor.BLACK)
         {
             current.LeftNode.Color = RedBlackNode.NodeColor.RED;
             current.Color          = RedBlackNode.NodeColor.BLACK;
         }
         else if (current.RightNode != null &&
                  current.RightNode.Color == RedBlackNode.NodeColor.BLACK)
         {
             current.RightNode.Color = RedBlackNode.NodeColor.RED;
             current.Color           = RedBlackNode.NodeColor.BLACK;
         }
     }
 }
Beispiel #8
0
        private static RedBlackNode initialInsertRepair(RedBlackNode current)
        {
            // considering the specific of our insertion - if current not black (in this case we have to do nothing)
            // current and current.Brother were red leafs before last insert, so we can make start already from
            // case 3 in https://ru.wikipedia.org/wiki/%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D0%BE-%D1%87%D1%91%D1%80%D0%BD%D0%BE%D0%B5_%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BE

            if (current.NodeColor == RedBlackNode.Color.Red)
            {
                current.NodeColor         = RedBlackNode.Color.Black;
                current.Brother.NodeColor = RedBlackNode.Color.Black;
                current.Parent.NodeColor  = RedBlackNode.Color.Red;
                return(repairAfterInsert(current.Parent));
            }
            else
            {
                return(recountToUp(current));
            }
        }
        private static int Verify <T>(RedBlackNode <T> tree) where T : RedBlackNode <T>
        {
            if (tree == null)
            {
                return(0);
            }

            if (tree is IComparable <T> comp)
            {
                if (tree.Left != null)
                {
                    Assert.IsTrue(comp.CompareTo(tree.Left) > 0, "Order Violation");
                }
                if (tree.Right != null)
                {
                    Assert.IsTrue(comp.CompareTo(tree.Right) < 0, "Order Violation");
                }
            }

            if (tree.Left != null)
            {
                Assert.IsTrue(tree.Left.Parent == tree, "Parent Child ref Mismatch");
            }
            if (tree.Right != null)
            {
                Assert.IsTrue(tree.Right.Parent == tree, "Parent Child ref Mismatch");
            }

            if (tree.Red)
            {
                Assert.IsTrue(IsBlack(tree.Left) && IsBlack(tree.Right), "Red Violation");
            }

            int height;

            Assert.AreEqual(height = Verify(tree.Left), Verify(tree.Right), "Black Violation");

            if (tree.Black)
            {
                height++;
            }

            return(height);
        }
Beispiel #10
0
        ///<summary>
        /// GetMinKey
        /// Returns the minimum key value
        ///<summary>
        public TKey GetMinKey()
        {
            RedBlackNode <TKey, TValue> treeNode = rbTree;

            if (treeNode == null || treeNode == sentinelNode)
            {
                throw (new Exception("RedBlack tree is empty"));
            }

            // traverse to the extreme left to find the smallest key
            while (treeNode.Left != sentinelNode)
            {
                treeNode = treeNode.Left;
            }

            lastNodeFound = treeNode;

            return(treeNode.Key);
        }
Beispiel #11
0
 protected internal override RedBlackNode BalanceRight(RedBlackNode parent)
 {
     if (this._right is RedNode)
     {
         return(SortedMap.MakeRed(this._key, Value,
                                  SortedMap.MakeBlack(parent.Key, parent.Value, parent.Left, this._left),
                                  this._right.Blacken()));
     }
     else if (this._left is RedNode)
     {
         return(SortedMap.MakeRed(this._left.Key, this._left.Value,
                                  SortedMap.MakeBlack(parent.Key, parent.Value, parent.Left, this._left.Left),
                                  SortedMap.MakeBlack(this._key, Value, this._left.Right, this._right)));
     }
     else
     {
         return(base.BalanceRight(parent));
     }
 }
Beispiel #12
0
 /// <summary>
 /// 为了实现LL/RR 最后一句 node = p 预期实现的效果
 /// </summary>
 /// <param name="oldp">老的节点node(就是LL/RR 传递时候的node)</param>
 /// <param name="newp">新的节点node(就是LL/RR 执行完成之后的node)</param>
 /// <param name="path"></param>
 private void ReLink(RedBlackNode oldp, RedBlackNode newp, ref Stack <RedBlackNode> path)
 {
     if (path.Count == 0)
     {
         root = newp;
     }
     else
     {
         RedBlackNode grandfather = path.Peek();
         if (grandfather.left == oldp)
         {
             grandfather.left = newp;
         }
         else
         {
             grandfather.right = newp;
         }
     }
 }
Beispiel #13
0
        internal static RedBlackNode BalanceLeftDelete(object key, object val, RedBlackNode del, RedBlackNode right)
        {
            if (del is RedNode)
            {
                return(MakeRed(key, val, del.Blacken(), right));
            }
            else if (right is BlackNode)
            {
                return(RightBalance(key, val, del, right.Redden()));
            }
            else if (right is RedNode && right.Left is BlackNode)
            {
                return(MakeRed(right.Left.Key, right.Left.Value,
                               MakeBlack(key, val, del, right.Left.Left),
                               RightBalance(right.Key, right.Value, right.Left.Right, right.Right.Redden())));
            }

            throw new InvalidOperationException("Invariant violation");
        }
Beispiel #14
0
 private void FixChildColors(RedBlackNode current)
 {
     // If a node is red, both children must be black,switch if necessary.
     if (current.Color == NodeColor.Red)
     {
         if (current.LeftNode != null &&
             current.LeftNode.Color == NodeColor.Black)
         {
             current.LeftNode.Color = NodeColor.Red;
             current.Color          = NodeColor.Black;
         }
         else if (current.RightNode != null &&
                  current.RightNode.Color == NodeColor.Black)
         {
             current.RightNode.Color = NodeColor.Red;
             current.Color           = NodeColor.Black;
         }
     }
 }
Beispiel #15
0
        /// <summary>
        /// Insert a new object into the RB Tree
        /// </summary>
        /// <param name="item"></param>
        public void Insert(int item)
        {
            RedBlackNode newItem = new RedBlackNode(item);

            if (root == null)
            {
                root        = newItem;
                root.colour = Color.Black;
                return;
            }
            RedBlackNode Y = null;
            RedBlackNode X = root;

            while (X != null)
            {
                Y = X;
                if (newItem.data < X.data)
                {
                    X = X.left;
                }
                else
                {
                    X = X.right;
                }
            }
            newItem.parent = Y;
            if (Y == null)
            {
                root = newItem;
            }
            else if (newItem.data < Y.data)
            {
                Y.left = newItem;
            }
            else
            {
                Y.right = newItem;
            }
            newItem.left   = null;
            newItem.right  = null;
            newItem.colour = Color.Red; //colour the new node red
            InsertFixUp(newItem);       //call method to check for violations and fix
        }
Beispiel #16
0
        /// <summary>
        /// Inserts the specified key/value pair to the tree.
        /// </summary>
        /// <param name="key">The key of the element to insert.</param>
        /// <param name="value">The value of the element to insert. The value can be <c>null</c> for reference types.</param>
        /// <exception cref="System.ArgumentNullException">The key is null.</exception>
        /// <exception cref="System.ArgumentException">An element with the same key already exists in the tree.</exception>
        public override void Insert(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (this.root == null)
            {
                this.root = new RedBlackNode {
                    Key = key, Value = value, Color = NodeColor.Black
                };
                this.nodeCount++;
                this.version++;
                return;
            }

            RedBlackNode node = this.SearchNodeForInsertion(key) as RedBlackNode;

            if (node == null)
            {
                throw new ArgumentException(CollectionMessages.KeyExists, nameof(key));
            }

            if (this.Comparer.Compare(key, node.Key) < 0)
            {
                node.LeftChild = new RedBlackNode {
                    Key = key, Value = value, Parent = node, Color = NodeColor.Red
                };
                this.BalanceInsert(node.LeftChild as RedBlackNode);
            }
            else
            {
                node.RightChild = new RedBlackNode {
                    Key = key, Value = value, Parent = node, Color = NodeColor.Red
                };
                this.BalanceInsert(node.RightChild as RedBlackNode);
            }

            this.nodeCount++;
            this.version++;
        }
        /// <summary>
        /// Inserts the specified key/value pair to the tree.
        /// </summary>
        /// <param name="key">The key of the element to insert.</param>
        /// <param name="value">The value of the element to insert. The value can be <c>null</c> for reference types.</param>
        /// <exception cref="System.ArgumentNullException">The key is null.</exception>
        /// <exception cref="System.ArgumentException">An element with the same key already exists in the tree.</exception>
        public override void Insert(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key", "The key is null.");
            }

            if (_root == null)
            {
                _root = new RedBlackNode {
                    Key = key, Value = value, Color = NodeColor.Black
                };
                _nodeCount++;
                _version++;
                return;
            }

            RedBlackNode node = SearchNodeForInsertion(key) as RedBlackNode;

            if (node == null)
            {
                throw new ArgumentException("An element with the same key already exists in the tree.", "key");
            }

            if (_comparer.Compare(key, node.Key) < 0)
            {
                node.LeftChild = new RedBlackNode {
                    Key = key, Value = value, Parent = node, Color = NodeColor.Red
                }
            }
            ;
            else
            {
                node.RightChild = new RedBlackNode {
                    Key = key, Value = value, Parent = node, Color = NodeColor.Red
                }
            };

            BalanceInsert(node);
            _nodeCount++;
            _version++;
        }
Beispiel #18
0
        private void InsertNode(T value, RedBlackNode current)
        {
            if (value.CompareTo(current.NodeValue) == -1)
            {
                if (current.LeftNode == null)
                {
                    var node = new RedBlackNode(value)
                    {
                        Color      = RedBlackNode.NodeColor.RED,
                        ParentNode = current,
                    };
                    current.LeftNode = node;
                    _nodeCount++;
                }
                else
                {
                    InsertNode(value, current.LeftNode);
                    return;
                }
            }
            else if (value.CompareTo(current.NodeValue) == 1)
            {
                if (current.RightNode == null)
                {
                    var node = new RedBlackNode(value)
                    {
                        Color      = RedBlackNode.NodeColor.RED,
                        ParentNode = current,
                    };
                    current.RightNode = node;
                    _nodeCount++;
                }
                else
                {
                    InsertNode(value, current.RightNode);
                    return;
                }
            }
            CheckNode(current);

            _rootNode.Color = RedBlackNode.NodeColor.BLACK;
        }
Beispiel #19
0
 public void Insert(T value)
 {
     if (_rootNode == null)
     {
         // In this case we are inserting the root node.
         var node = new RedBlackNode(value)
         {
             ParentNode = null,
             Color      = NodeColor.Black
         };
         _rootNode = node;
         _nodeCount++;
     }
     else
     {
         // The root already exists, so traverse the tree to figure out
         // where to put the node.
         InsertNode(value, _rootNode);
     }
 }
        // 1) The root is black
        // 2) All leaves are black
        // 3) Every red node must have two black child nodes
        // 4) Every path from a given node to any of its descendant leaves must contain an equal number of black nodes

        private void CheckNode(RedBlackNode <T> node)
        {
            RedBlackNode <T> left  = (RedBlackNode <T>)node.Left;
            RedBlackNode <T> right = (RedBlackNode <T>)node.Right;

            if (left.BlackHeight != right.BlackHeight)
            {
                // rotate
                // make left node red and checknode
                // make right node red and checknode
            }

            if (node.Color.Equals(NodeColors.Red))
            {
                if (node.Equals(this.Root) || left.Color.Equals(NodeColors.Red) || right.Color.Equals(NodeColors.Red))
                {
                    node.Color = NodeColors.Black;
                }
            }
        }
Beispiel #21
0
        private static IEnumerable <T> InOrderTraversal(RedBlackNode node)
        {
            if (node.LeftNode != null)
            {
                foreach (T nodeVal in InOrderTraversal(node.LeftNode))
                {
                    yield return(nodeVal);
                }
            }

            yield return(node.NodeValue);

            if (node.RightNode != null)
            {
                foreach (T nodeVal in InOrderTraversal(node.RightNode))
                {
                    yield return(nodeVal);
                }
            }
        }
Beispiel #22
0
 internal static RedBlackNode BalanceRightDelete(object key, object val, RedBlackNode left, RedBlackNode del)
 {
     if (del is RedNode)
     {
         return(MakeRed(key, val, left, del.Blacken()));
     }
     else if (left is BlackNode)
     {
         return(LeftBalance(key, val, left.Redden(), del));
     }
     else if (left is RedNode && left.Right is BlackNode)
     {
         return(MakeRed(left.Right.Key, left.Right.Value,
                        LeftBalance(left.Key, left.Value, left.Left.Redden(), left.Right.Left),
                        MakeBlack(key, val, left.Right.Right, del)));
     }
     else
     {
         throw new InvalidOperationException("Invariant violation");
     }
 }
Beispiel #23
0
 static RedBlackNode Append(RedBlackNode left, RedBlackNode right)
 {
     if (left is null)
     {
         return(right);
     }
     if (right is null)
     {
         return(left);
     }
     else if (left is RedNode)
     {
         if (right is RedNode)
         {
             var app = Append(left.Right, right.Left);
             return(app is RedNode
                 ? MakeRed(app.Key, app.Value,
                           MakeRed(left.Key, left.Value, left.Left, app.Left),
                           MakeRed(right.Key, right.Value, app.Right, right.Right))
                 : MakeRed(left.Key, left.Value, left.Left, MakeRed(right.Key, right.Value, app, right.Right)));
         }
         else
         {
             return(MakeRed(left.Key, left.Value, left.Left, Append(left.Right, right)));
         }
     }
     else if (right is RedNode)
     {
         return(MakeRed(right.Key, right.Value, Append(left, right.Left), right.Right));
     }
     else
     {
         var app = Append(left.Right, right.Left);
         return((app is RedNode)
             ? MakeRed(app.Key, app.Value,
                       MakeBlack(left.Key, left.Value, left.Left, app.Left),
                       MakeBlack(right.Key, right.Value, app.Right, right.Right))
                           : BalanceLeftDelete(left.Key, left.Value, left.Left, MakeBlack(right.Key, right.Value, app, right.Right)));
     }
 }
    private void RotateRight(RedBlackNode <TKey, TValue> x)
    {
        RedBlackNode <TKey, TValue> y = x.Left;

        x.Left = y.Right;

        if (y.Right != sentinelNode)
        {
            y.Right.Parent = x;
        }
        if (y != sentinelNode)
        {
            y.Parent = x.Parent;
        }

        if (x.Parent != null)
        {
            if (x == x.Parent.Right)
            {
                x.Parent.Right = y;
            }
            else
            {
                x.Parent.Left = y;
            }
        }
        else
        {
            rbTree = y;
        }

        y.Right = x;
        if (x != sentinelNode)
        {
            x.Parent = y;
        }

        x.Count = x.Left.Count + x.Right.Count + 1;
        y.Count = y.Left.Count + y.Right.Count + 1;
    }
Beispiel #25
0
        public void RotateRight_FullTree()
        {
            RedBlackNode <int> node = new RedBlackNode <int>(100);

            node.Left  = new RedBlackNode <int>(50);
            node.Right = new RedBlackNode <int>(150);

            node.Left.Left   = new RedBlackNode <int>(25);
            node.Left.Right  = new RedBlackNode <int>(75);
            node.Right.Left  = new RedBlackNode <int>(125);
            node.Right.Right = new RedBlackNode <int>(175);

            node.ResetHeight();
            node.Left.ResetHeight();
            node.Right.ResetHeight();
            node.Left.Left.ResetHeight();
            node.Left.Right.ResetHeight();
            node.Right.Left.ResetHeight();
            node.Right.Right.ResetHeight();

            node = node.RotateRight() as RedBlackNode <int>;

            Assert.AreEqual <int>(50, node.Value);
            Assert.AreEqual <int>(25, node.Left.Value);
            Assert.AreEqual <int>(100, node.Right.Value);
            Assert.AreEqual <int>(75, node.Right.Left.Value);
            Assert.AreEqual <int>(150, node.Right.Right.Value);
            Assert.AreEqual <int>(125, node.Right.Right.Left.Value);
            Assert.AreEqual <int>(175, node.Right.Right.Right.Value);


            Assert.AreEqual <int>(3, node.Height);
            Assert.AreEqual <int>(0, node.Left.Height);
            Assert.AreEqual <int>(2, node.Right.Height);
            Assert.AreEqual <int>(1, node.Right.Right.Height);
            Assert.AreEqual <int>(0, node.Right.Left.Height);
            Assert.AreEqual <int>(0, node.Right.Right.Right.Height);
            Assert.AreEqual <int>(0, node.Right.Right.Left.Height);
        }
Beispiel #26
0
        internal static RedBlackNode NodeAt(System.Collections.IComparer comp, RedBlackNode tree, object key)
        {
            var t = tree;

            while (t != null)
            {
                int c = comp.Compare(key, t.Key);
                if (c == 0)
                {
                    return(t);
                }
                else if (c < 0)
                {
                    t = t.Left;
                }
                else
                {
                    t = t.Right;
                }
            }
            return(t);
        }
Beispiel #27
0
        /// <summary>
        /// Adds a node.
        /// </summary>
        /// <param name="key">Key of the node.</param>
        /// <param name="value">Value of the node. </param>
        public void Add(TKey key, TValue value)
        {
            RedBlackNode newNode = new RedBlackNode(key, value);

            if (this.rootNode == null)
            {
                this.rootNode       = newNode;
                this.rootNode.Color = NodeColor.BLACK;
                nodeCount++;
                isModfied = true;
                return;
            }

            RecursiveAdd(newNode, this.rootNode);

            this.RepairTree(newNode);

            this.nodeCount++;
            isModfied           = true;
            this.rootNode.Color = NodeColor.BLACK;
            return;
        }
Beispiel #28
0
        internal static RedBlackNode Add(System.Collections.IComparer comp, RedBlackNode t, object key, object val, Box found)
        {
            if (t is null)
            {
                return(val is null ? new RedNode(key) : new RedValueNode(key, val));
            }
            int c = comp.Compare(key, t.Key);

            if (c == 0)
            {
                found.Value = t;
                return(null);
            }
            var insert = c < 0 ? Add(comp, t.Left, key, val, found) : Add(comp, t.Right, key, val, found);

            if (insert is null)
            {
                return(null);
            }
            return(c < 0
                ? t.AddLeft(insert)
                : t.AddRight(insert));
        }
Beispiel #29
0
            public static void Main()
            {
                var c = new RedBlackNode <int>
                {
                    Key   = "1",
                    Value = 1,
                    Left  = new RedBlackNode <int>
                    {
                        Key   = "11",
                        Value = 11
                    },
                    Right = new RedBlackNode <int>
                    {
                        Key   = "12",
                        Value = 12
                    }
                };

                foreach (var pair in RedBlackNode <int> .GetPairs(c))
                {
                    System.Console.WriteLine(pair.Key + " - " + pair.Value);
                }
            }
Beispiel #30
0
        /// <summary>
        /// Find item in the tree
        /// </summary>
        /// <param name="key"></param>
        public RedBlackNode Find(int key)
        {
            bool         isFound = false;
            RedBlackNode temp    = root;
            RedBlackNode item    = null;

            while (!isFound)
            {
                if (temp == null)
                {
                    break;
                }
                if (key < temp.data)
                {
                    temp = temp.left;
                }
                if (key > temp.data)
                {
                    temp = temp.right;
                }
                if (key == temp.data)
                {
                    isFound = true;
                    item    = temp;
                }
            }
            if (isFound)
            {
                Console.WriteLine("{0} was found", key);
                return(temp);
            }
            else
            {
                Console.WriteLine("{0} not found", key);
                return(null);
            }
        }
Beispiel #31
0
        private static void PrintRedBlackNode(RedBlackNode node, IComparable parentKey)
        {
            Console.Write(
                "Key:{0}\t" + "  Data:{1}\t",
                node.Key,
                node.Value);
            Console.Write(" Color:");
            if (node.Color == Color.BLACK)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Write(node.Color);
                Console.ResetColor();
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(node.Color);
                Console.ResetColor();
            }

            Console.WriteLine("\t Parent Key:{0}", parentKey);
        }
Beispiel #32
0
        public void ToString_RootRight()
        {
            RedBlackNode<int> node = new RedBlackNode<int>(50)
            {
                Right = new RedBlackNode<int>(75),
            };

            Assert.AreEqual<string>("50,Red; Left=null; Right=75", node.ToString());
        }
Beispiel #33
0
        public void RotateRight_FullTree()
        {
            RedBlackNode<int> node = new RedBlackNode<int>(100);

            node.Left = new RedBlackNode<int>(50);
            node.Right = new RedBlackNode<int>(150);

            node.Left.Left = new RedBlackNode<int>(25);
            node.Left.Right = new RedBlackNode<int>(75);
            node.Right.Left = new RedBlackNode<int>(125);
            node.Right.Right = new RedBlackNode<int>(175);

            node.ResetHeight();
            node.Left.ResetHeight();
            node.Right.ResetHeight();
            node.Left.Left.ResetHeight();
            node.Left.Right.ResetHeight();
            node.Right.Left.ResetHeight();
            node.Right.Right.ResetHeight();

            node = node.RotateRight() as RedBlackNode<int>;

            Assert.AreEqual<int>(50, node.Value);
            Assert.AreEqual<int>(25, node.Left.Value);
            Assert.AreEqual<int>(100, node.Right.Value);
            Assert.AreEqual<int>(75, node.Right.Left.Value);
            Assert.AreEqual<int>(150, node.Right.Right.Value);
            Assert.AreEqual<int>(125, node.Right.Right.Left.Value);
            Assert.AreEqual<int>(175, node.Right.Right.Right.Value);

            Assert.AreEqual<int>(3, node.Height);
            Assert.AreEqual<int>(0, node.Left.Height);
            Assert.AreEqual<int>(2, node.Right.Height);
            Assert.AreEqual<int>(1, node.Right.Right.Height);
            Assert.AreEqual<int>(0, node.Right.Left.Height);
            Assert.AreEqual<int>(0, node.Right.Right.Right.Height);
            Assert.AreEqual<int>(0, node.Right.Right.Left.Height);
        }
Beispiel #34
0
        public void RotateRight_CheckColours()
        {
            RedBlackNode<int> node = new RedBlackNode<int>(100);
            node.Left = new RedBlackNode<int>(50);
            node = node.RotateRight() as RedBlackNode<int>;

            Assert.IsFalse(node.IsRed);
            Assert.IsTrue(node.Right.IsRed);
        }
        public void AssertValidTree_InvalidChildren()
        {
            var root = new RedBlackNode<int>(100);
            root.Left = new RedBlackNode<int>(150);

            RedBlackTree<int> bst = new RedBlackTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
Beispiel #36
0
        public void ResetHeight()
        {
            RedBlackNode<int> node = new RedBlackNode<int>(20);

            Assert.AreEqual<int>(0, node.Height);

            node.Left = new RedBlackNode<int>(10);

            Assert.AreEqual<int>(0, node.Height);

            node.ResetHeight();

            Assert.AreEqual<int>(1, node.Height);
        }
Beispiel #37
0
 public void IsLeaf_True()
 {
     RedBlackNode<int> node = new RedBlackNode<int>(50);
     Assert.IsTrue(node.IsLeaf);
 }
        public void AssertValidTree_Invalid_BlackMismatch_Left()
        {
            RedBlackNode<int> root = new RedBlackNode<int>(100, Colour.Black);
            root.Left = new RedBlackNode<int>(50, Colour.Red);
            root.Right = new RedBlackNode<int>(150, Colour.Red);
            root.Left.Left = new RedBlackNode<int>(40, Colour.Red);

            RedBlackTree<int> bst = new RedBlackTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
Beispiel #39
0
        public void InOrderSuccessor_RootLeft()
        {
            RedBlackNode<int> rootLeft = new RedBlackNode<int>(50)
            {
                Left = new RedBlackNode<int>(25)
            };

            Assert.IsNull(rootLeft.InOrderSuccessor);
        }
        public void AssertValidTree_Invalid_DoubleRed_Right()
        {
            RedBlackNode<int> root = new RedBlackNode<int>(100, Colour.Black);
            root.Left = new RedBlackNode<int>(50, Colour.Red);
            root.Right = new RedBlackNode<int>(150, Colour.Red);
            root.Right.Right = new RedBlackNode<int>(160, Colour.Red);

            RedBlackTree<int> bst = new RedBlackTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
Beispiel #41
0
 // Removes the specified name+value from the tree, and returns a new tree.
 // Sets fMissing if name cannot be found.
 public ValueTree RemoveItem(ref bool fMissing, string name)
 {
     Contracts.AssertValue(name);
     return(new ValueTree(RedBlackNode <EquatableObject> .RemoveItem(ref fMissing, _root, name, _hashCodeCache)));
 }
Beispiel #42
0
 public void InOrderSuccessor_Leaf()
 {
     RedBlackNode<int> leaf = new RedBlackNode<int>(50);
     Assert.IsNull(leaf.InOrderSuccessor);
 }
Beispiel #43
0
        public void IsLeaf_False_Left()
        {
            var node = new RedBlackNode<int>(50);
            node.Left = new RedBlackNode<int>(20);

            Assert.IsFalse(node.IsLeaf);
        }
Beispiel #44
0
        public void InOrderSuccessor_RootRight()
        {
            RedBlackNode<int> rootRight = new RedBlackNode<int>(50)
            {
                Right = new RedBlackNode<int>(75)
            };

            Assert.AreEqual<int>(75, rootRight.InOrderSuccessor.Value);
        }
Beispiel #45
0
        public void IsLeaf_False_Right()
        {
            var node = new RedBlackNode<int>(50);
            node.Right = new RedBlackNode<int>(70);

            Assert.IsFalse(node.IsLeaf);
        }