[Глава 5] Поиск в ширину

This commit is contained in:
Пытков Роман
2025-07-27 22:07:09 +03:00
parent 455ef1ba54
commit 08b37fe0e1
5 changed files with 96 additions and 23 deletions

View File

@@ -0,0 +1,23 @@
using GrokAlgorithms.Chapter5_BreadthFirstSearch;
static class Bfs
{
public static TreeNode<T>? Search<T>(TreeNode<T> root, Func<T, bool> condition)
{
Queue<TreeNode<T>> queue = new();
queue.Enqueue(root);
HashSet<TreeNode<T>> visited = [];
while (queue.Count > 0)
{
var curr = queue.Dequeue();
if (visited.Contains(curr))
continue;
if (condition(curr.Value))
return curr;
foreach (var child in curr.Children)
queue.Enqueue(child);
visited.Add(curr);
}
return null;
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View 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);

View File

@@ -0,0 +1,18 @@
namespace GrokAlgorithms.Chapter5_BreadthFirstSearch;
public class TreeNode<T>
{
public T Value { get; init; }
public string Comment { get; init; }
public List<TreeNode<T>> Children { get; init; }
public bool Selected { get; set; }
public TreeNode(T value)
{
Value = value;
Comment = "";
Children = new List<TreeNode<T>>();
Selected = false;
}
}

View File

@@ -1,30 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter4_QuickSort", "Chapter4_QuickSort\Chapter4_QuickSort.csproj", "{4088F633-80E4-44DF-9DE4-996A4B3E3088}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chapter5_BreadthFirstSearch", "Chapter5_BreadthFirstSearch\Chapter5_BreadthFirstSearch.csproj", "{F54FC00B-FB3D-4A8B-ABA8-33A43FC74DE4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F895D96-6E79-40BC-A778-89E76BC981F6}.Release|Any CPU.Build.0 = Release|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4C54C0A-59E1-4C40-B2C2-D8C800603AF6}.Release|Any CPU.Build.0 = Release|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4088F633-80E4-44DF-9DE4-996A4B3E3088}.Release|Any CPU.Build.0 = Release|Any CPU
{F54FC00B-FB3D-4A8B-ABA8-33A43FC74DE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F54FC00B-FB3D-4A8B-ABA8-33A43FC74DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F54FC00B-FB3D-4A8B-ABA8-33A43FC74DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F54FC00B-FB3D-4A8B-ABA8-33A43FC74DE4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal