public void Add(string str) { JTrieNode cur = _root; JTrieNode tmp = null; foreach (char ch in str) { if (cur.Children == null) cur.Children = new Dictionary<int, JTrieNode>(); if (!cur.Children.Keys.Contains(ch)) { tmp = new JTrieNode() { NodeKey = ch }; cur.Children.Add(ch, tmp); } cur = cur.Children[ch]; cur.NoOfPrefix += 1; } cur.IsWord = true; }
/// Init root node /// public JTrie() { _root = new JTrieNode() { NodeKey = ' ' }; }
/// max search count can be specified via the constructure /// else, default value will be used public JTrie(int maxSearchCount) { _root = new JTrieNode() { NodeKey = ' ' }; _maxSearchCount = maxSearchCount; }
/// recursive method iterates through all the trie and return full word with /// specified prefix /// private void GetMoreWords(JTrieNode cur, List<string> result, string prefix, int top) { if (cur.Children == null) return; foreach (JTrieNode node in cur.Children.Values) { string tmp = prefix + node.NodeKey; if (node.IsWord) { if (result.Count >= top) break; else result.Add(tmp); } GetMoreWords(node, result, tmp, top); } }