Beispiel #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 _23TreeNode <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.
            _23TreeNode <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 = (_23TreeNode <T>) this.Neighbors[i + 1];
                }
            }

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

            this.Values[index] = successorNode.Values[0];
            return(successorNode.Delete(this.Values[index], 0));
        }
Beispiel #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 _23TreeNode <T> Delete(_23TreeNode <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((_23TreeNode <T>)node.Neighbors[i], data));
                    }

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

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

            return(null);
        }