Exemple #1
0
        private AlphabetTrieNode DeleteInternal(AlphabetTrieNode node, string key)
        {
            if (node == null || string.IsNullOrEmpty(key))
            {
                return(null);
            }

            var index = GetIndex(key, 0);

            if (key.Length == 1 && node.Children?[index] != null)
            {
                if (node.Children?[index].IsCompleteWord == true)
                {
                    if (node.Children?[index].Children?.Any(arg => arg != null) == true)
                    {
                        node.IsCompleteWord = false;
                        return(node);
                    }

                    node.Children[index] = null;
                    return(IsNonEmptyNode(node) ? node : null);
                }

                return(node);
            }

            node.Children[index] = DeleteInternal(node.Children[index], key.Substring(1));

            return(IsNonEmptyNode(node) ? node : null);
        }
Exemple #2
0
 public void Delete(string key)
 {
     root = DeleteInternal(root, key);
 }
Exemple #3
0
 private static bool IsNonEmptyNode(AlphabetTrieNode node)
 {
     return(node.Children.Any(arg => arg != null) || node.IsCompleteWord);
 }