public TrieNode(byte value, TrieNode parent, Boolean isWord) { this.children = new TrieNode[NumLetters]; this.Value = value; this.Parent = parent; this.IsWord = isWord; this.HasChildren = false; }
private TrieNode loadFromFile(String path) { TrieNode node = new TrieNode(Byte.MaxValue, null, false); using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read)) { using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { String line = reader.ReadLine().Trim().ToLower(); line = Processor(line); if (Matcher(line)) node.AddWord(line); } } } return node; }
private void find(TrieNode current, int x, int y) { if (current.IsWord && current.LastFound != this.solveNumber) { found++; current.LastFound = this.solveNumber; } if (!current.HasChildren) return; used[x][y] = true; byte imax = (byte)(x + 2 > len ? len : x + 2); byte jmax = (byte)(y + 2 > len ? len : y + 2); byte jmin = (byte)(y - 1 < 0 ? 0 : y - 1); for (byte i = (byte)(x - 1 < 0 ? 0 : x - 1); i < imax; i++) for (byte j = jmin; j < jmax; j++) if ((current[board[i, j]]) != null && !used[i][j]) find(current[board[i, j]], i, j); used[x][y] = false; }
/// <summary> /// Load the dictionary from the given file path. If a cached version exists, it will be loaded. /// </summary> /// <param name="dictionaryPath"></param> public void LoadDictionary(String dictionaryPath) { try { this.trie = loadFromCache(dictionaryPath); } catch (Exception) { try { this.trie = loadFromFile(dictionaryPath); } catch (Exception x) { throw new DictionaryNotLoadedException(x); } } }
/// <summary> /// Recurses over the trie and applies an Action to TrieNodes which match a Predicate. /// </summary> /// <param name="node">TrieNode to perform Action on.</param> /// <param name="predicate">Predicate to match desired TrieNodes.</param> /// <param name="action">Action to perform on TrieNode.</param> public static void Iterate(TrieNode node, Predicate<TrieNode> predicate, Action<TrieNode> action) { if (node == null) throw new ArgumentNullException("node"); if (predicate(node)) action(node); foreach (TrieNode n in node.children) if (n != null) Iterate(n, predicate, action); }
/// <summary> /// Add a new child node to this TrieNode. /// </summary> /// <param name="c">Char identifier for new child.</param> /// <param name="isWord">Is the child node a word terminator?</param> /// <returns>Returns the child TrieNode added.</returns> public TrieNode Put(Char c, Boolean isWord) { this.HasChildren = true; if (this[c] == null) this[c] = new TrieNode((byte)(c - FirstChar), this, isWord); if (isWord) this[c].IsWord = true; return this[c]; }