Example #1
0
        /// <summary>
        /// Deletes element in i-th position.
        /// </summary>
        /// <param name="data">
        /// Element to be deleted.
        /// </param>
        /// <param name="index">
        /// Position index.
        /// </param>
        public BPlusTreeNode <T> Delete(T data, int index)
        {
            if (this.IsLeaf)
            {
                if (!this.IsHalf)
                {
                    this.Values.RemoveAt(index);
                    return(this);
                }

                if (this._parent == null)
                {
                    this.Values.RemoveAt(index);
                    return(this);
                }

                this.Values.Remove(data);
                return(this.Merge());
            }

            // Find successor.
            BPlusTreeNode <T> successorNode = null;

            for (int i = 0; i < this.Values.Count; i++)
            {
                int result = this._comparer.Compare(this.Values[i], data);
                if (result <= 0)
                {
                    successorNode = (BPlusTreeNode <T>) this.Neighbors[i + 1];
                }
            }

            while (successorNode.Neighbors != null)
            {
                successorNode = (BPlusTreeNode <T>)successorNode.Neighbors[0];
            }

            this.Values[index] = successorNode.Values[0];
            return(successorNode.Delete(this.Values[index], 0));
        }
Example #2
0
        /// <summary>
        /// Deletes data from given node.
        /// </summary>
        /// <param name="node">
        /// Node from which element will be removed.
        /// </param>
        /// <param name="data">
        /// Element to be removed.
        /// </param>
        /// <returns>
        /// </returns>
        private BPlusTreeNode <T> Delete(BPlusTreeNode <T> node, T data)
        {
            while (node != null)
            {
                for (int i = 0; i < node.Values.Count; i++)
                {
                    int result = this._comparer.Compare(node.Values[i], data);
                    if (result == 0)
                    {
                        return(node.Delete(data, i));
                    }

                    if (result > 0)
                    {
                        if (node.Neighbors == null)
                        {
                            return(null);
                        }

                        return(this.Delete((BPlusTreeNode <T>)node.Neighbors[i], data));
                    }

                    if (i + 1 == node.Values.Count)
                    {
                        if (node.Neighbors == null)
                        {
                            return(null);
                        }

                        return(this.Delete((BPlusTreeNode <T>)node.Neighbors[i + 1], data));
                    }
                }
            }

            return(null);
        }