Ejemplo n.º 1
0
        /*************************************************************
        * Function: search(string input_prefix_string, List<string> words)
        * Date Created: April 23, 2017
        * Date Last Modified: April 23, 2017
        * Description: search the input word.
        * Return: NONE
        *************************************************************/
        public void search(string input_prefix_string, List <string> words)
        {
            if ((string.IsNullOrEmpty(input_prefix_string)))
            {
                return;
            }

            char[] input_prefix_char = input_prefix_string.ToArray();

            Trie_Node cur   = Trie.m_root;
            Trie_Node child = null;

            foreach (char c in input_prefix_char)
            {
                if (cur.children.Exists(x => x.c == c))
                {
                    child = cur.AddOrGetChild(c);
                    cur   = child;
                }
                else
                {
                    words.Add("can't find the word");
                    return;
                }
            }
            //words.Add("success");

            insertString_rec(cur, input_prefix_string.Substring(0, input_prefix_string.Length - 1), words);
        }
Ejemplo n.º 2
0
        public void Addstring(string s)
        {
            Trie_Node n = m_root;

            foreach (char c in s)
            {
                n = n.AddOrGetChild(c);
            }
            n = n.AddOrGetChild('\0');
        }
Ejemplo n.º 3
0
        public Trie_Node AddOrGetChild(char cc)
        {
            foreach (Trie_Node n in children)
            {
                if (n.c == cc)
                {
                    return(n);
                }
            }

            Trie_Node node = new Trie_Node(cc);

            children.Add(node);
            return(node);
        }
Ejemplo n.º 4
0
        /*************************************************************
        * Function: insertString_rec(Trie_Node node, string sub_string, List<string> words)
        * Date Created: April 23, 2017
        * Date Last Modified: April 23, 2017
        * Description: using rec to process the children node.
        * Return: NONE
        *************************************************************/
        private void insertString_rec(Trie_Node node, string sub_string, List <string> words)
        {
            if (node == null)
            {
                return;
            }

            sub_string = sub_string + node.c;

            if (node.c == '\0')
            {
                words.Add(sub_string);
            }

            foreach (Trie_Node n in node.children)
            {
                insertString_rec(n, sub_string, words);
            }
        }