public Trie(List<String> targets_in ) { this.root = new TrieNode ('r'); // r means root this.currentNode = root; //build trie with target lines foreach(String line in targets_in) { foreach (char ch in line) { //if current node doesnt have the character, add it and traverse if (!currentNode.hasChildByID (ch)) { currentNode.addChild (ch); currentNode = currentNode.getChildByID (ch); } // else traverse the matching char branch else { currentNode = currentNode.getChildByID (ch); } } currentNode = root; } }
public void getMatches(String source_in) { String contents; //String contents = File.ReadAllText (source_in); using (StreamReader streamReader = new StreamReader(source_in, Encoding.UTF8)) { contents = streamReader.ReadToEnd(); } for(int i =0; i < contents.Length; i ++) { currentNode = root; for (int j = i; j < contents.Length; j++) { char ch = contents [j]; //.ToString ().ToLower ()[0]; if (currentNode.hasChildByID (ch)) { currentNode = currentNode.getChildByID (ch); if (!currentNode.hasAnyChildren ()) { Console.WriteLine ("\t"+ i + "\t"+ contents.Substring(i,j-i+1)); } } else { break; } } } }
public void setCurrentNode(TrieNode current_in) { this.currentNode = current_in; }