コード例 #1
0
        private bool IsLeftChild(RBNode <T> node)
        {
            if (node.Parent != null)
            {
                return(node.Parent.Left == node);
            }

            throw new NullReferenceException("Cannot specify node direction becouse parent node is null");
        }
コード例 #2
0
        private void RepairRedUncle(RBNode <T> origin)
        {
            RBNode <T> uncle = GetUncle(origin);

            origin.Parent.SetColor(Color.Black);
            uncle.SetColor(Color.Black);
            origin.Parent.Parent.SetColor(Color.Red);

            RepairTree(origin.Parent.Parent);
        }
コード例 #3
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);
        }
コード例 #4
0
        public RBTree(Comparison <T> comparisonDifference)
        {
            if (comparisonDifference == null)
            {
                throw new ArgumentNullException("comparisonDifference paramenter cannot be null value");
            }

            valueComparison = comparisonDifference;
            //valueComparisonEquality = GetValueComparator();

            root       = null;
            nodesCount = 0;
        }
コード例 #5
0
        private RBNode <T> GetUncle(RBNode <T> origin)
        {
            RBNode <T> grand = origin.Parent.Parent;

            if (grand.Left == origin)
            {
                return(grand.Right);
            }
            else
            {
                return(grand.Left);
            }
        }
コード例 #6
0
        public void Insert(T value)
        {
            if (!Contains(value))
            {
                RBNode <T> insertedNode = InsertNode(new RBNode <T>(value));
                RepairTree(insertedNode);
            }
            else
            {
                throw new InvalidOperationException("Cannot insert value which actually exist in the tree");
            }

            nodesCount++;
        }
コード例 #7
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);
        }
コード例 #8
0
 private void RepairRootNode(RBNode <T> origin)
 {
     origin.SetColor(Color.Black);
 }