/// <summary> /// Get node for given char array /// </summary> public TrieNode GetChild(char[] letters) { TrieNode currentNode = this; for (int i = 0; i < letters.Length && currentNode != null; i++) { currentNode = currentNode.GetChild(letters[i]); if (currentNode == null) { return(null); } } return(currentNode); }
/// <summary> /// Append word to the prefix tree /// </summary> public void AddWord(String word) { char[] argChars = word.ToCharArray(); TrieNode currentNode = root; for (int i = 0; i < argChars.Length; i++) { if (!currentNode.ContainsChildValue(argChars[i])) { currentNode.AddChild(argChars[i], new TrieNode(currentNode.Key + argChars[i])); } currentNode = currentNode.GetChild(argChars[i]); } currentNode.IsWord = true; }
/// <summary> /// Get node for given word or prefix /// </summary> public TrieNode GetNode(String argString) { return(root.GetChild(argString)); }