Пример #1
0
        /// <summary>
        /// This method collects all the strings that starts with the
        /// given prefix
        /// </summary>
        /// <param name="root"></param>
        /// <param name="que"></param>
        /// <returns></returns>
        private Queue<String> Collect(Node root , Queue<String> que)
        {
            if(root.value != null)
            {
                que.Enqueue(root.value);
            }

            foreach(Node c in root.letter_pointers)
            {
                if(c != null)
                {
                    root = c;
                    Collect(root, que);
                }
            }
            return que;
        }
Пример #2
0
 /// <summary>
 /// 
 /// This default constructor creates the root node 
 /// for the trie
 /// </summary>
 public Trie()
 {
     this.root = new Node();
 }
Пример #3
0
        /// <summary>
        /// 
        /// This method finds the prefix nodes for the
        /// given string
        /// bascially it iterates over the trie to reach a point where
        /// prefix ends and distinct strings start
        /// </summary>
        /// <param name="root"></param>
        /// <param name="prefix"></param>
        /// <param name="que"></param>
        /// <returns></returns>
        private Queue<String> find_prefix(Node root, char [] prefix, Queue<String> que)
        {
            int index_for_first_letter = Convert.ToInt32(letter_indices[char.ToUpper(prefix[0])].ToString());
            if (root.letter_pointers[index_for_first_letter] == null)
            {
                return que;
            }

            //Queue<String> q = new Queue<string>();
            foreach(char c in prefix)
            {
                int index = Convert.ToInt32(letter_indices[char.ToUpper(c)].ToString());

                if(root.letter_pointers[index]!=null)
                {
                    root = root.letter_pointers[index];
                }

             }

            return Collect(root, que);
        }
Пример #4
0
        /// <summary>
        /// This method inserts the word 
        /// into tries data structure
        ///  
        /// Implementation sucessfull
        /// handled all the cases
        /// 
        /// </summary>
        /// 
        private Node insert(Node root, char[] word, int index)
        {
            if(root == null)            // Base condition
            {
                root = new Node();
            }

            if (index == word.Length)       // second base condition
            {
                root.value = new string(word);
                return root;
            }

            char c = word[index];
            index++;
            char x = char.ToUpper(c);
            string temp = letter_indices[x].ToString();
            // creates the new node for that char c i.e letter
            root.letter_pointers[Convert.ToInt32(temp)] = insert(root.letter_pointers[Convert.ToInt32(temp)], word, index);
            return root;
        }
Пример #5
0
        /// <summary>
        /// 
        /// Finds the word in tries
        /// 
        /// tested for two cases
        /// worked fine
        /// </summary>
        /// <param name="root"></param>
        /// <param name="word"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private Node find(Node root, char[] word, int index)
        {
            if(root ==  null)
            {
                return null;
            }

            if(index == word.Length)
            {
                return root;
            }

            char c = word[index];
            index++;
            char x = char.ToUpper(c);
            string temp = letter_indices[x].ToString();
            return find(root.letter_pointers[Convert.ToInt32(temp)], word, index);
        }