示例#1
0
        // adds an item (no validation)
        void _Add(TKey key, TValue value)
        {
            // tree is empty?
            if (null == root)
            {
                root          = new _Node(_comparer, _minimumDegree, true);
                root.Items[0] = new KeyValuePair <TKey, TValue>(key, value);                // Insert key
                root.KeyCount = 1;
                return;
            }

            // grow tree if root is full
            if (root.KeyCount == 2 * _minimumDegree - 1)
            {
                _Node newRoot = new _Node(_comparer, _minimumDegree, false);

                newRoot.Children[0] = root;
                newRoot.Split(0, root);
                // figure out which child gets the key (sort)
                int i = 0;
                if (0 > _comparer.Compare(newRoot.Items[0].Key, key))
                {
                    ++i;
                }
                newRoot.Children[i].Insert(key, value);

                root = newRoot;
            }
            else              // just insert
            {
                root.Insert(key, value);
            }
        }