Example #1
0
        ///<summary>binary insert, node color is set to RED</summary>
        private RBNode <T> InsertNode(RBNode <T> node)
        {
            if (root == null)
            {
                root = new RBNode <T>(Color.Red);
                return(root);
            }

            RBNode <T> current    = root;
            int        compResult = 0;

            while (true)
            {
                compResult = valueComparison(node.Value, current.Value);
                if (compResult < 0)
                {
                    if (current.Left == null)
                    {
                        break;
                    }
                    else
                    {
                        current = current.Left;
                    }
                }
                else
                {
                    if (current.Right == null)
                    {
                        break;
                    }
                    else
                    {
                        current = current.Right;
                    }
                }
            }

            node.SetParent(current);
            node.SetColor(Color.Red);

            if (compResult < 0)
            {
                current.SetLeft(node);
            }
            else
            {
                current.SetRight(node);
            }

            return(node);
        }
Example #2
0
        private RBNode <T> RotateRight(RBNode <T> parent, RBNode <T> child)
        {
            child.SetParent(parent.Parent);
            if (parent.Parent != null)
            {
                if (IsRightChild(parent))
                {
                    parent.Parent.SetRight(child);
                }
                else
                {
                    parent.Parent.SetLeft(child);
                }
            }

            parent.SetParent(child);
            parent.SetLeft(child.Right);
            child.SetRight(parent);

            return(child);
        }