コード例 #1
0
ファイル: TrieNodeBase.cs プロジェクト: k-sae/trienet
        /// <summary>
        /// Remove the value belongs to a key, if key exists
        /// </summary>
        /// <param name="key"></param>
        /// <param name="position"></param>
        /// <returns>boolean indicaties if the function shall continue with deletion process</returns>
        public bool Remove(string key, int position)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (EndOfString(position, key))
            {
                // check if the key is part of longer key
                if (BelongsToLongerKey())
                {
                    // if true remove only the value
                    RemoveValue();
                    // prevent the backward removal of the key
                    return(false);
                }
                // the key is either a unique key or contains a shorter key
                // return true so the function can continue deleting
                return(true);
            }

            // had to use stack approach since no backward approach

            // replace this with get or null child and check if null return false
            TrieNodeBase <TValue> child = GetChildOrNull(key, position);

            if (child is null)
            {
                return(false);
            }
            bool removeRecursively = child.Remove(key, position + 1);

            // check if a delete signal from the previous call
            if (removeRecursively)
            {
                RemoveChild(key, position);
                // discontinue the delete process if a subkey has a value or if it contains another key
                return(!HasValue() && !BelongsToLongerKey());
            }
            return(false);
        }