Skip to content

Commit

Permalink
Updated nuget packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Jun 16, 2024
1 parent 5f8d109 commit 67db007
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand Down
108 changes: 55 additions & 53 deletions DsaDotnet/Trees/BinaryTree.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
namespace DsaDotnet.Trees
namespace DsaDotnet.Trees;

public class BinaryTree<T> where T : IComparable<T>
{
public class BinaryTree<T> where T : IComparable<T>
private readonly IComparer<T> _comparer;

public BinaryTree(IComparer<T>? comparer = null)
{
private TreeNode<T>? _root { get; set; }
if (comparer != null)
{
_comparer = comparer;
}
else
{
_comparer ??= Comparer<T>.Default;
}
}

private readonly IComparer<T> _comparer;
private TreeNode<T>? _root { get; set; }

public BinaryTree(IComparer<T>? comparer = null)
public void Insert(params T[] keys)
{
for (var i = 0; i < keys.Length; i++)
{
if (comparer != null)
{
_comparer = comparer;
}
else
{
_comparer ??= Comparer<T>.Default;
}
_root = InsertRec(_root, keys[i]);
}
}

public void Insert(params T[] keys)
private TreeNode<T> InsertRec(TreeNode<T>? node, T key)
{
if (node == null)
{
for (var i = 0; i < keys.Length; i++)
{
_root = InsertRec(_root, keys[i]);
}
node = new TreeNode<T> { Key = key };
return node;
}

private TreeNode<T> InsertRec(TreeNode<T>? node, T key)
var comparison = _comparer.Compare(key, node.Key);
switch (comparison)
{
case < 0:
node.Left = InsertRec(node.Left, key);
break;
case > 0:
node.Right = InsertRec(node.Right, key);
break;
}

return node;
}

public TreeNode<T>? Search(T key)
{
return SearchRec(_root, key);
}

private TreeNode<T>? SearchRec(TreeNode<T>? node, T key)
{
while (true)
{
if (node == null)
{
node = new TreeNode<T>() { Key = key };
return node;
}

var comparison = _comparer.Compare(key, node.Key);

switch (comparison)
{
case < 0:
node.Left = InsertRec(node.Left, key);
break;
node = node.Left;
continue;
case > 0:
node.Right = InsertRec(node.Right, key);
break;
}

return node;
}

public TreeNode<T>? Search(T key)
{
return SearchRec(_root, key);
}

private TreeNode<T>? SearchRec(TreeNode<T>? node, T key)
{
while (true)
{
if (node == null) return node;

var comparison = _comparer.Compare(key, node.Key);

switch (comparison)
{
case < 0:
node = node.Left;
continue;
case > 0:
node = node.Right;
continue;
default:
return node;
}
node = node.Right;
continue;
default:
return node;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/Graphs/GraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class GraphTests
{
[Theory]
[InlineData(new int[] { })]
[InlineData(new int[] { 1, 2 })]
[InlineData(new int[] { 1, 1 })]
[InlineData(new[] { 1, 2 })]
[InlineData(new[] { 1, 1 })]
public void WeightedGraph_AddNodes(int[] nodeKeys)
{
// Arrange
var graph = new WeightedGraph<int>();
graph.AddNodes(nodeKeys.Select(n => new WeightedGraphNode<int>() { Key = n }).ToArray());
graph.AddNodes(nodeKeys.Select(n => new WeightedGraphNode<int> { Key = n }).ToArray());

// Act
var nodes = graph.AllNodes();
Expand Down Expand Up @@ -58,13 +58,13 @@ public void WeightedGraph_AddWeightedEdges((int, int)[] edges)

[Theory]
[InlineData(new int[] { })]
[InlineData(new int[] { 1, 2 })]
[InlineData(new int[] { 1, 1 })]
[InlineData(new[] { 1, 2 })]
[InlineData(new[] { 1, 1 })]
public void UnWeightedGraph_AddNodes(int[] nodeKeys)
{
// Arrange
var graph = new UnWeightedGraph<int>();
graph.AddNodes(nodeKeys.Select(n => new GraphNode<int>() { Key = n }).ToArray());
graph.AddNodes(nodeKeys.Select(n => new GraphNode<int> { Key = n }).ToArray());

// Act
var nodes = graph.AllNodes();
Expand Down Expand Up @@ -92,14 +92,14 @@ public void UnWeightedGraph_AddEdges((int, int)[] edges)

[Theory]
[InlineData(new int[] { })]
[InlineData(new int[] { 0 })]
[InlineData(new[] { 0 })]
[InlineData(new[] { 1, 2, 3 })]
[InlineData(new[] { 1, 1 })]
public void GraphNode_AddNeighbor(int[] neighborKeys)
{
// Arrange
var node = new GraphNode<int>() { Key = 0 };
var neighborNodes = neighborKeys.Select(k => new GraphNode<int>() { Key = k });
var node = new GraphNode<int> { Key = 0 };
var neighborNodes = neighborKeys.Select(k => new GraphNode<int> { Key = k });

// Act
foreach (var neighborNode in neighborNodes)
Expand Down
8 changes: 4 additions & 4 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
3 changes: 0 additions & 3 deletions Tests/Trees/BinaryTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class BinaryTreeTests
[InlineData(new[] { 0 }, 0)]
[InlineData(new[] { 0, 1, 2 }, 1)]
[InlineData(new[] { 2, 1, 0, 3, 4 }, 3)]

public void Binary_Tree_Search_Returns_Correct_Node(int[] elements, int searchKey)
{
// Arrange
Expand All @@ -22,6 +21,4 @@ public void Binary_Tree_Search_Returns_Correct_Node(int[] elements, int searchKe
// Assert
node!.Key.Should().Be(searchKey);
}


}

0 comments on commit 67db007

Please sign in to comment.