Exemplo n.º 1
0
        /** Returns if the word is in the trie. */
        public bool Search(string word) // should only be true if the found node is a leaf
        {
            Node curr = _root;

            for (int i = 0; i < word.Length; i++)
            {
                curr = curr.FindNode(word[i]);
                if (curr == null)
                {
                    return(false);
                }
            }

            return(curr.IsLeaf);
        }
Exemplo n.º 2
0
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public bool StartsWith(string prefix)
        {
            Node curr = _root;

            foreach (char c in prefix)
            {
                curr = curr.FindNode(c);
                if (curr == null)
                {
                    return(false);
                }
            }

            return(true);
            //return _root.GetChild(prefix) != null; // return true if any node found
        }
Exemplo n.º 3
0
        /** Inserts a word into the trie. */
        public void Insert(string word)
        {
            Node   curr       = _root;
            string restOfWord = word;

            for (int i = 0; i < word.Length; i++)
            {
                Node prev = curr;
                curr = curr.FindNode(restOfWord[0]);
                if (curr == null)
                {
                    curr = prev;
                    break;
                }
                restOfWord = restOfWord.Substring(1);
            }

            curr.AddToChildren(restOfWord);
        }