Пример #1
0
        private TreeDictionary <TKey, TValue, TComparer> AddRecursive(TKey key, TValue value, 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(value));
            }
            if (comparison < 0)
            {
                var newLeft = _left.AddRecursive(key, value, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(newLeft, _right)
                           : Balanced(newLeft, _right));
            }
            else
            {
                var newRight = _right.AddRecursive(key, value, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(_left, newRight)
                           : Balanced(_left, newRight));
            }
        }
Пример #2
0
 private TreeDictionary <TKey, TValue, TComparer> InsertMax(TKey key, TValue value)
 {
     if (Count == 0)
     {
         return(TreeDictionary.Single <TKey, TValue, TComparer>(key, value));
     }
     return(NearlyBalanced(_left, _right.InsertMax(key, value)));
 }