public bool ContainsWord(string word) { DigitalTreeNode currNode = RootNode; foreach (char ch in word) { if (!currNode.NextNodes.ContainsKey(ch)) { return(false); } currNode = currNode.NextNodes[ch]; } return((currNode == null) ? false : currNode.IsCompleteWord); }
public List <List <WordPathNode> > FindWords(int currRow, int currCol, List <List <WordPathNode> > results, DigitalTreeNode currNode, List <WordPathNode> currPath, HashSet <int> visitorTracker, HashSet <DigitalTreeNode> foundWords) { if (currNode.IsCompleteWord && !foundWords.Contains(currNode)) { if (currPath.Count >= MinimumWordLength) { AddPathToList(results, currPath); } // Keep track of found words so we don't add duplicates foundWords.Add(currNode); } foreach (int nextIndex in GetNeighbors(currRow, currCol)) { if (visitorTracker.Contains(nextIndex)) { continue; // We've already visited this node } int nextRow = GetRow(nextIndex); int nextCol = GetColumn(nextIndex); char nextChar = BoggleBoard.GetCharacter(nextRow, nextCol); DigitalTreeNode nextNode = null; if (!currNode.NextNodes.ContainsKey(nextChar)) { continue; } nextNode = currNode.NextNodes[nextChar]; // TODO: Account for the "U" that follows "Q" on the "QU" dices currPath.Add(new WordPathNode(nextRow, nextCol, nextChar)); visitorTracker.Add(nextIndex); // Recurse into neighbors with viable prefixes FindWords(GetRow(nextIndex), GetColumn(nextIndex), results, nextNode, currPath, visitorTracker, foundWords); visitorTracker.Remove(nextIndex); currPath.RemoveAt(currPath.Count - 1); } return(results); }
private IEnumerable <string> InnerEnumerator(DigitalTreeNode currNode, StringBuilder prefix) { if (currNode.IsCompleteWord) { yield return(prefix.ToString()); } foreach (char ch in currNode.NextNodes.Keys) { prefix.Append(ch); foreach (string word in InnerEnumerator(currNode.NextNodes[ch], prefix)) { yield return(word); } prefix.Remove(prefix.Length - 1, 1); } }
private void LoadWord(string word) { DigitalTreeNode currNode, newNode; var upperWord = word.ToUpper(); currNode = RootNode; foreach (char ch in upperWord) { if (currNode.NextNodes.ContainsKey(ch)) { currNode = currNode.NextNodes[ch]; } else { newNode = new DigitalTreeNode(); currNode.NextNodes.Add(ch, newNode); currNode = newNode; } } currNode.IsCompleteWord = true; }
public DigitalTree() { RootNode = new DigitalTreeNode(); }