public IEnumerable <string> AutoComplete(string beginning) { LetterNode currentNode = _root; for (int i = 0; i < beginning.Length; ++i) { char currentCharacter = beginning[i]; if (currentNode.Children.ContainsKey(currentCharacter)) { currentNode = currentNode.Children[currentCharacter]; } else { currentNode = null; break; } } List <string> words = new List <string>(); if (currentNode != null) // null if there are no matches { GetWords(currentNode, beginning, words); } return(words); }
private void Swap(int idx1, int idx2) { LetterNode node1 = heap[idx1]; LetterNode node2 = heap[idx2]; heap[idx1] = node2; heap[idx2] = node1; }
public void LongestWordReturnsLongestPlayNodeIfComputerIsToLoseTest() { ComputerPlayer computer = new ComputerPlayer(); LetterNode node = computer.LongestWord(new LetterNode("game")); Assert.IsFalse(node.Child('m').LeafNode); }
public void LongestWordReturnsNextBestPlayNodeIfComputerIsToWinTest() { ComputerPlayer computer = new ComputerPlayer(); LetterNode node = computer.LongestWord(new LetterNode("aye")); Assert.IsTrue(node.Child('e').LeafNode); }
public void ForcedWinReturnsNextBestPlayNodeIfComputerIsToWinTest() { ComputerPlayer computer = new ComputerPlayer(); LetterNode node = computer.ForcedWin(new LetterNode("gamma")); Assert.AreEqual('a', node.Letter()); }
public void ForcedWinReturnsNullNodeIfComputerIsToLoseTest() { ComputerPlayer computer = new ComputerPlayer(); LetterNode node = computer.ForcedWin(new LetterNode("game")); Assert.IsNull(node); }
// depth first search to get all the words private void GetWords(LetterNode node, string currentWord, List <string> words) { foreach (char character in node.Children.Keys) { GetWords(node.Children[character], currentWord + character, words); } if (node.IsTerminal) { words.Add(currentWord); return; } }
public void Add(string word) { LetterNode currentNode = _root; for (int i = 0; i < word.Length; ++i) { char currentCharacter = word[i]; if (!currentNode.Children.ContainsKey(currentCharacter)) { currentNode.Children[currentCharacter] = new LetterNode(); } currentNode = currentNode.Children[currentCharacter]; } currentNode.IsTerminal = true; }
public void Clear() { _root = new LetterNode(); }
public void Add(LetterNode node) { // Add node at end of heap and sift it up this.heap.Add(node); SiftUp(this.heap.Count - 1); }