From 67db00764b7f29c205fe7a62178b9f875c5958d8 Mon Sep 17 00:00:00 2001 From: timmoth Date: Sun, 16 Jun 2024 13:33:56 +0100 Subject: [PATCH] Updated nuget packages --- Benchmarks/Benchmarks.csproj | 2 +- DsaDotnet/Trees/BinaryTree.cs | 108 +++++++++++++++++---------------- Tests/Graphs/GraphTests.cs | 18 +++--- Tests/Tests.csproj | 8 +-- Tests/Trees/BinaryTreeTests.cs | 3 - 5 files changed, 69 insertions(+), 70 deletions(-) diff --git a/Benchmarks/Benchmarks.csproj b/Benchmarks/Benchmarks.csproj index f8340f0..6091da5 100644 --- a/Benchmarks/Benchmarks.csproj +++ b/Benchmarks/Benchmarks.csproj @@ -8,7 +8,7 @@ - + diff --git a/DsaDotnet/Trees/BinaryTree.cs b/DsaDotnet/Trees/BinaryTree.cs index ac9614a..01de053 100644 --- a/DsaDotnet/Trees/BinaryTree.cs +++ b/DsaDotnet/Trees/BinaryTree.cs @@ -1,77 +1,79 @@ -namespace DsaDotnet.Trees +namespace DsaDotnet.Trees; + +public class BinaryTree where T : IComparable { - public class BinaryTree where T : IComparable + private readonly IComparer _comparer; + + public BinaryTree(IComparer? comparer = null) { - private TreeNode? _root { get; set; } + if (comparer != null) + { + _comparer = comparer; + } + else + { + _comparer ??= Comparer.Default; + } + } - private readonly IComparer _comparer; + private TreeNode? _root { get; set; } - public BinaryTree(IComparer? comparer = null) + public void Insert(params T[] keys) + { + for (var i = 0; i < keys.Length; i++) { - if (comparer != null) - { - _comparer = comparer; - } - else - { - _comparer ??= Comparer.Default; - } + _root = InsertRec(_root, keys[i]); } + } - public void Insert(params T[] keys) + private TreeNode InsertRec(TreeNode? node, T key) + { + if (node == null) { - for (var i = 0; i < keys.Length; i++) - { - _root = InsertRec(_root, keys[i]); - } + node = new TreeNode { Key = key }; + return node; } - private TreeNode InsertRec(TreeNode? 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? Search(T key) + { + return SearchRec(_root, key); + } + + private TreeNode? SearchRec(TreeNode? node, T key) + { + while (true) { if (node == null) { - node = new TreeNode() { 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? Search(T key) - { - return SearchRec(_root, key); - } - - private TreeNode? SearchRec(TreeNode? 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; } } } diff --git a/Tests/Graphs/GraphTests.cs b/Tests/Graphs/GraphTests.cs index 8fee060..0eaf077 100644 --- a/Tests/Graphs/GraphTests.cs +++ b/Tests/Graphs/GraphTests.cs @@ -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(); - graph.AddNodes(nodeKeys.Select(n => new WeightedGraphNode() { Key = n }).ToArray()); + graph.AddNodes(nodeKeys.Select(n => new WeightedGraphNode { Key = n }).ToArray()); // Act var nodes = graph.AllNodes(); @@ -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(); - graph.AddNodes(nodeKeys.Select(n => new GraphNode() { Key = n }).ToArray()); + graph.AddNodes(nodeKeys.Select(n => new GraphNode { Key = n }).ToArray()); // Act var nodes = graph.AllNodes(); @@ -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() { Key = 0 }; - var neighborNodes = neighborKeys.Select(k => new GraphNode() { Key = k }); + var node = new GraphNode { Key = 0 }; + var neighborNodes = neighborKeys.Select(k => new GraphNode { Key = k }); // Act foreach (var neighborNode in neighborNodes) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 77ace95..0901a10 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -11,13 +11,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Tests/Trees/BinaryTreeTests.cs b/Tests/Trees/BinaryTreeTests.cs index 72f6a94..b464367 100644 --- a/Tests/Trees/BinaryTreeTests.cs +++ b/Tests/Trees/BinaryTreeTests.cs @@ -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 @@ -22,6 +21,4 @@ public void Binary_Tree_Search_Returns_Correct_Node(int[] elements, int searchKe // Assert node!.Key.Should().Be(searchKey); } - - }