[Глава 5] Поиск в ширину
This commit is contained in:
28
Chapter5_BreadthFirstSearch/Program.cs
Normal file
28
Chapter5_BreadthFirstSearch/Program.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using GrokAlgorithms.Chapter5_BreadthFirstSearch;
|
||||
|
||||
int count = 0;
|
||||
|
||||
// Команда для построения дерева
|
||||
TreeNode<int> BuildTreeCommand(int value, int depth, int branchingFactor)
|
||||
{
|
||||
var node = new TreeNode<int>(value) { Comment = $"{count++}" };
|
||||
if (depth == 0) return node;
|
||||
|
||||
for (int i = 1; i <= branchingFactor; i++)
|
||||
node.Children.Add(BuildTreeCommand(value + i * 10, depth - 1, branchingFactor));
|
||||
return node;
|
||||
}
|
||||
|
||||
// Команда для печати дерева
|
||||
void PrintTreeCommand(TreeNode<int> node, string indent = "")
|
||||
{
|
||||
Console.WriteLine($"{indent}{node.Value} [{node.Comment}] {(node.Selected ? " Selected" : "")}");
|
||||
foreach (var child in node.Children)
|
||||
PrintTreeCommand(child, indent + " ");
|
||||
}
|
||||
|
||||
var root = BuildTreeCommand(1, 3, 2);
|
||||
var found = Bfs.Search(root, (num) => num > 25);
|
||||
if (found != null)
|
||||
found.Selected = true;
|
||||
PrintTreeCommand(root);
|
||||
Reference in New Issue
Block a user