private List <string> GetWords(AutocompleteNode root)
        {
            List <string>    list        = new List <string>();
            AutocompleteNode currentNode = root;

            foreach (AutocompleteNode node in currentNode.Children.Values)
            {
                if (node.IsLeaf)
                {
                    list.Add(node.Word);
                }
                else
                {
                    list.AddRange(GetWords(node));
                }
            }
            return(list);
        }
        public void Add(string word)
        {
            AutocompleteNode currentNode = _root;

            foreach (char c in word)
            {
                if (currentNode.Children.ContainsKey(c))
                {
                    currentNode = currentNode.Children[c];
                }
                else
                {
                    currentNode.Children.Add(c, new AutocompleteNode(c, currentNode.Word + c));
                    currentNode = currentNode.Children[c];
                }
            }
            if (!currentNode.Children.ContainsKey('\0'))
            {
                currentNode.Children.Add('\0', new AutocompleteNode('\0', currentNode.Word));
            }
        }
        public List <string> FindWordByPrefix(string prefix)
        {
            List <string>    list          = new List <string>();
            AutocompleteNode currentNode   = _root;
            bool             prefixFounded = true;

            foreach (char c in prefix)
            {
                if (currentNode.Children.ContainsKey(c))
                {
                    currentNode = currentNode.Children[c];
                }
                else
                {
                    prefixFounded = false;
                };
            }
            if (prefixFounded)
            {
                list.AddRange(GetWords(currentNode));
            }
            return(list);
        }
 public AutocompleteSystem()
 {
     _root = new AutocompleteNode('\0', "");
 }