Created
August 25, 2024 08:36
-
-
Save eisterman/bbb264687c48461c85c6869ea45563bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#nullable enable | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class BinarySearchTree : IEnumerable<int> | |
{ | |
public BinarySearchTree(int value) => Value = value; | |
public BinarySearchTree(IEnumerable<int> values) | |
{ | |
var valuesArr = values.ToArray(); | |
Value = valuesArr[0]; | |
foreach (var v in valuesArr.Skip(1)) | |
{ | |
Add(v); | |
} | |
} | |
public int Value | |
{ | |
get; | |
} | |
public BinarySearchTree? Left | |
{ | |
get; | |
private set; | |
} | |
public BinarySearchTree? Right | |
{ | |
get; | |
private set; | |
} | |
private void Add(int value) | |
{ | |
if (value > Value) | |
{ | |
if (Right == null) | |
{ | |
Right = new BinarySearchTree(value); | |
} | |
else | |
{ | |
Right.Add(value); | |
} | |
} | |
else | |
{ | |
if (Left == null) | |
{ | |
Left = new BinarySearchTree(value); | |
} | |
else | |
{ | |
Left.Add(value); | |
} | |
} | |
} | |
public IEnumerator<int> GetEnumerator() | |
{ | |
var res = new List<int>(); | |
if(Left != null) | |
res.AddRange(Left); | |
res.Add(Value); | |
if(Right != null) | |
res.AddRange(Right); | |
return res.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Xunit; | |
public class BinarySearchTreeTests | |
{ | |
[Fact] | |
public void Data_is_retained() | |
{ | |
var tree = new BinarySearchTree(4); | |
Assert.Equal(4, tree.Value); | |
} | |
[Fact] | |
public void Smaller_number_at_left_node() | |
{ | |
var tree = new BinarySearchTree(new[] { 4, 2 }); | |
Assert.Equal(4, tree.Value); | |
Assert.Equal(2, tree.Left.Value); | |
} | |
[Fact] | |
public void Same_number_at_left_node() | |
{ | |
var tree = new BinarySearchTree(new[] { 4, 4 }); | |
Assert.Equal(4, tree.Value); | |
Assert.Equal(4, tree.Left.Value); | |
} | |
[Fact] | |
public void Greater_number_at_right_node() | |
{ | |
var tree = new BinarySearchTree(new[] { 4, 5 }); | |
Assert.Equal(4, tree.Value); | |
Assert.Equal(5, tree.Right.Value); | |
} | |
[Fact] | |
public void Can_create_complex_tree() | |
{ | |
var tree = new BinarySearchTree(new[] { 4, 2, 6, 1, 3, 5, 7 }); | |
Assert.Equal(4, tree.Value); | |
Assert.Equal(2, tree.Left.Value); | |
Assert.Equal(1, tree.Left.Left.Value); | |
Assert.Equal(3, tree.Left.Right.Value); | |
Assert.Equal(6, tree.Right.Value); | |
Assert.Equal(5, tree.Right.Left.Value); | |
Assert.Equal(7, tree.Right.Right.Value); | |
} | |
[Fact] | |
public void Can_sort_single_number() | |
{ | |
var tree = new BinarySearchTree(2); | |
Assert.Equal(new[] { 2 }, tree.AsEnumerable()); | |
} | |
[Fact] | |
public void Can_sort_if_second_number_is_smaller_than_first() | |
{ | |
var tree = new BinarySearchTree(new[] { 2, 1 }); | |
Assert.Equal(new[] { 1, 2 }, tree.AsEnumerable()); | |
} | |
[Fact] | |
public void Can_sort_if_second_number_is_same_as_first() | |
{ | |
var tree = new BinarySearchTree(new[] { 2, 2 }); | |
Assert.Equal(new[] { 2, 2 }, tree.AsEnumerable()); | |
} | |
[Fact] | |
public void Can_sort_if_second_number_is_greater_than_first() | |
{ | |
var tree = new BinarySearchTree(new[] { 2, 3 }); | |
Assert.Equal(new[] { 2, 3 }, tree.AsEnumerable()); | |
} | |
[Fact] | |
public void Can_sort_complex_tree() | |
{ | |
var tree = new BinarySearchTree(new[] { 2, 1, 3, 6, 7, 5 }); | |
Assert.Equal(new[] { 1, 2, 3, 5, 6, 7 }, tree.AsEnumerable()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment