Пример #1
0
        private TreeDictionary <TKey, TValue, TComparer> AddRecursive(TKey key, TValue value, Func <TValue, TValue> combiner, ref bool needRebalance)
        {
            if (_count == 0)
            {
                needRebalance = true;
                return(TreeDictionary.Single <TKey, TValue, TComparer>(key, value));
            }
            int comparison = _Comparer.Compare(key, _key);

            if (comparison == 0)
            {
                needRebalance = false;
                return(ReplaceValue(combiner(_value)));
            }
            if (comparison < 0)
            {
                var dict = _left.AddRecursive(key, value, combiner, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(dict, _right)
                           : Balanced(dict, _right));
            }
            else
            {
                var dict = _right.AddRecursive(key, value, combiner, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(_left, dict)
                           : Balanced(_left, dict));
            }
        }