private WordTreeNode InsertAfter(WordTreeNode currentNode, char[] word, bool isWord) { Debug.Assert(currentNode != null); if (word == null || word.Length <= 0) { return(null); } WordTreeNode targetNode = null; // see if the currentNode has a next pointer for next letter char charToFind = word[0]; foreach (var node in currentNode.Next) { if (charToFind.Equals(node.Character)) { targetNode = node; break; } } // if null create a new node if (targetNode == null) { targetNode = new WordTreeNode(charToFind, currentNode, word.Length == 1); currentNode.Next.Add(targetNode); } // corner case when AddWord("care") is called before AddWord("car") if (word.Length == 1) { targetNode.IsWord = true; } // remove the first char and then continue return(InsertAfter(targetNode, RemoveFirstChar(word), word.Length == 1)); }
public WordTreeNode(char character, WordTreeNode previous, bool isWord) { Character = character; Previous = previous; IsWord = isWord; }