Esempio n. 1
0
        /// <summary>
        /// Inserts a new entry to this node, assuming it's a leaf node.
        /// </summary>
        public void InsertAsLeaf(K key, V value, int index)
        {
            Debug.Assert(IsLeaf, "This node is not a leaf node.");

            entries.Insert(index, new Tuple <K, V>(key, value));
            nodeManager.MarkAsChanged(this);
        }
Esempio n. 2
0
        //
        // Public Methods
        //

        /// <summary>
        /// Remove an entry from this instance.
        /// </summary>
        public void Remove(int removeAt)
        {
            // Validate argument
            if (false == (removeAt >= 0) && (removeAt < this.entries.Count))
            {
                throw new ArgumentOutOfRangeException();
            }

            // http://webdocs.cs.ualberta.ca/~holte/T26/del-b-tree.html
            // https://en.wikipedia.org/wiki/B-tree#Deletion
            // If this is a node leave, flagged entry will be removed
            if (IsLeaf)
            {
                // Step 1: Remove X from the current node.
                // Being a leaf node there are no subtrees to worry about.
                entries.RemoveAt(removeAt);
                nodeManager.MarkAsChanged(this);

                // If the removal does not cause underflow then we are done here
                if ((EntriesCount >= nodeManager.MinEntriesPerNode) || (parentId == 0))
                {
                    return;
                }
                // Otherwise, rebalance this node
                else
                {
                    Rebalance();
                }
            }
            // If the value to be deleted does not occur in a leaf,
            // we replace it with the largest value in its left subtree
            // and then proceed to delete that value from the node that
            // originally contained it
            else
            {
                // Grab the largest entry on the left subtree
                var             leftSubTree = nodeManager.Find(this.childrenIds[removeAt]);
                TreeNode <K, V> largestNode; int largestIndex;
                leftSubTree.FindLargest(out largestNode, out largestIndex);
                var replacementEntry = largestNode.GetEntry(largestIndex);

                // REplace it
                this.entries[removeAt] = replacementEntry;
                nodeManager.MarkAsChanged(this);

                // Remove it from the node we took it from
                largestNode.Remove(largestIndex);
            }
        }