private void BFS(IGraph graph, Int32 sourceVertice) { AST.Queue <Int32> queue = new AST.Queue <Int32>(); // We start at source, so we're the shortest path to source. // Shortest path found. this._shortestPathToVerticeMap[sourceVertice] = true; queue.Enqueue(sourceVertice); while (!queue.IsEmpty) { Int32 vertice = queue.Dequeue(); // Remove the next vertice from the queue. foreach (Int32 adjacentVertice in graph.GetAdjacentVertices(vertice)) { // For every unmarked adjacent vertice: // - Save the last vertice that connected to it. // - Mark the shortest path for the adjacent vertice as found. // - Add it to the queue to be visied later. // When no more adjacent vertices are found, start dequeuing vertices // so we can visit its adjacent vertices. if (!this._shortestPathToVerticeMap[adjacentVertice]) { this._edgeTo[adjacentVertice] = vertice; this._shortestPathToVerticeMap[adjacentVertice] = true; queue.Enqueue(adjacentVertice); } } } }
private void GetKeysThatMatch(Node node, StringBuilder prefix, Int32 index, String pattern, AST.Queue <String> queue) { if (node == null) { return; } Char c = pattern[index]; if (c == '.' || c < node.Char) { this.GetKeysThatMatch(node.Left, prefix, index, pattern, queue); } if (c == '.' || c == node.Char) { if (index == pattern.Length - 1 && node.Value != null) { queue.Enqueue(prefix.ToString() + node.Char); } if (index < pattern.Length - 1) { this.GetKeysThatMatch(node.Middle, prefix.Append(node.Char), index + 1, pattern, queue); prefix.Remove(prefix.Length - 1, 1); } } if (c == '.' || c > node.Char) { this.GetKeysThatMatch(node.Right, prefix, index, pattern, queue); } }
public IEnumerable <String> GetKeysWithPrefix(String prefix) { if (prefix == null) { throw new ArgumentNullException(nameof(prefix)); } AST.Queue <String> queue = new AST.Queue <String>(); Node node = this.Get(this._root, prefix, 0); if (node == null) { return(queue); } if (node.Value != null) { queue.Enqueue(prefix); } this.GetKeysWithPrefix(node.Middle, new StringBuilder(prefix), queue); return(queue); }
private void GetKeysWithPrefix(Node node, StringBuilder prefix, AST.Queue <String> queue) { if (node == null) { return; } this.GetKeysWithPrefix(node.Left, prefix, queue); if (node.Value != null) { queue.Enqueue(prefix.ToString() + node.Char); } this.GetKeysWithPrefix(node.Middle, prefix.Append(node.Char), queue); prefix.Remove(prefix.Length - 1, 1); this.GetKeysWithPrefix(node.Right, prefix, queue); }
private void GetKeysThatMatch(Node node, StringBuilder prefix, String pattern, AST.Queue <String> results) { if (node == null) { return; } Int32 index = prefix.Length; if (index == pattern.Length && node.Value != null) { results.Enqueue(prefix.ToString()); } if (index == pattern.Length) { return; } Char c = pattern[index]; if (c == '.') { for (char ch = this.Alphabet.MinChar; ch <= this.Alphabet.MaxChar; ch++) { prefix.Append(ch); this.GetKeysThatMatch(node.Next[ch], prefix, pattern, results); prefix.Remove(prefix.Length - 1, 1); } } else { prefix.Append(c); this.GetKeysThatMatch(node.Next[c], prefix, pattern, results); prefix.Remove(prefix.Length - 1, 1); } }
private void GetKeysWithPrefix(Node node, StringBuilder prefix, AST.Queue <String> results) { if (node == null) { return; } if (node.Value != null) { results.Enqueue(prefix.ToString()); } for (char c = this.Alphabet.MinChar; c <= this.Alphabet.MaxChar; c++) { prefix.Append(c); this.GetKeysWithPrefix(node.Next[c], prefix, results); prefix.Remove(prefix.Length - 1, 1); } }