Пример #1
0
        private bool Remove(string word, Node node)
        {
            if (word == "")
            {
                if (node.isWord)
                {
                    node.isWord = false;
                }
                return(true);
            }

            bool removeFlag = false;
            char nextChar   = word[0];
            Node nextNode   = null;

            if (node.HasChild(nextChar))
            {
                if (word.Length > 0)
                {
                    nextNode   = node.GetChild(nextChar);
                    removeFlag = Remove(word.Substring(1), node.GetChild(nextChar));
                }
            }

            if (removeFlag && !nextNode.HasChildren() && !nextNode.isWord)
            {
                node.RemoveChild(nextChar);
                return(true);
            }
            return(false);
        }
Пример #2
0
 private Node GetOrCreateNextNode(Node current, char c)
 {
     if (!current.HasChild(c))
     {
         current.AddChild(c);
     }
     return(current.GetChild(c));
 }
Пример #3
0
        public List <string> AutoComplete(string word)
        {
            Node startNode = root;
            var  results   = new List <string>();

            foreach (var c in word)
            {
                if (startNode.HasChild(c))
                {
                    startNode = startNode.GetChild(c);
                }
                else
                {
                    return(results);
                }
            }
            return(AutoComplete(word, startNode, results));
        }