Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary search tree encapsulated #2131

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
96 changes: 37 additions & 59 deletions exercises/practice/binary-search-tree/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,57 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class BinarySearchTree : IEnumerable<int>
public class BinarySearchTree<T> where T : IComparable
{
public BinarySearchTree(int value)
class Node
{
Value = value;
public T Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
}

public BinarySearchTree(IEnumerable<int> values)
{
var array = values.ToArray();

if (array.Length == 0)
{
throw new ArgumentException("Cannot create tree from empty list");
}
Node head;

public int Count { get; private set; }
public int Depth { get; private set; }

Value = array[0];

foreach (var value in array.Skip(1))
{
Add(value);
}
}

public int Value { get; }

public BinarySearchTree Left { get; private set; }

public BinarySearchTree Right { get; private set; }

public BinarySearchTree Add(int value)
public void Add(T value)
{
if (value <= Value)
{
Left = Add(value, Left);
Count++;

var depth = 1;

if (head == null) {
head = new Node { Value = value };
Depth = 1;
return;
}
else
{
Right = Add(value, Right);
}

return this;
}

private static BinarySearchTree Add(int value, BinarySearchTree tree)
{
if (tree == null)
var node = head;
while(node.Value.CompareTo(value) != 0)
{
return new BinarySearchTree(value);
depth++;
if (node.Value.CompareTo(value) < 0)
{
node.Left ??= new Node { Value = value };
node = node.Left;
} else {
node.Right ??= new Node { Value = value };
node = node.Right;
}
}

return tree.Add(value);
Depth = depth;
}

public IEnumerator<int> GetEnumerator()
public bool Contains(T value)
{
foreach (var left in Left?.AsEnumerable() ?? Enumerable.Empty<int>())
var node = head;
while(node != null)
{
yield return left;
}

yield return Value;
if (node.Value.CompareTo(value) == 0) { return true; }

foreach (var right in Right?.AsEnumerable() ?? Enumerable.Empty<int>())
{
yield return right;
if (node.Value.CompareTo(value) < 0) { node = node.Left; }
else { node = node.Right; }
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
return false;
}
}
3 changes: 2 additions & 1 deletion exercises/practice/binary-search-tree/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"j2jensen",
"robkeim",
"ShamilS",
"wolf99"
"wolf99",
"michalporeba"
],
"files": {
"solution": [
Expand Down
53 changes: 5 additions & 48 deletions exercises/practice/binary-search-tree/BinarySearchTree.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;

public class BinarySearchTree : IEnumerable<int>
public class BinarySearchTree<T> where T : IComparable
{
public BinarySearchTree(int value)
{
}
public int Count => throw new NotImplementedException("You need to implement this function.");
public int Depth => throw new NotImplementedException("You need to implement this function.");
michalporeba marked this conversation as resolved.
Show resolved Hide resolved

public BinarySearchTree(IEnumerable<int> values)
{
}

public int Value
{
get
{
throw new NotImplementedException("You need to implement this function.");
}
}

public BinarySearchTree Left
{
get
{
throw new NotImplementedException("You need to implement this function.");
}
}

public BinarySearchTree Right
{
get
{
throw new NotImplementedException("You need to implement this function.");
}
}

public BinarySearchTree Add(int value)
{
throw new NotImplementedException("You need to implement this function.");
}

public IEnumerator<int> GetEnumerator()
{
throw new NotImplementedException("You need to implement this function.");
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException("You need to implement this function.");
}
public void Add(T value) => throw new NotImplementedException("You need to implement this function.");
public bool Contains(T value) => throw new NotImplementedException("You need to implement this function.");
michalporeba marked this conversation as resolved.
Show resolved Hide resolved
}
104 changes: 43 additions & 61 deletions exercises/practice/binary-search-tree/BinarySearchTreeTests.cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests file has understandably changed lots. We have to decide whether we want to diverge from the original prob-specs data: https://github.com/exercism/problem-specifications/blob/main/exercises/binary-search-tree/canonical-data.json

Original file line number Diff line number Diff line change
@@ -1,84 +1,66 @@
using System.Linq;
using Xunit;

public class BinarySearchTreeTests
{
[Fact]
public void Data_is_retained()
public void New_bst_is_empty()
{
var tree = new BinarySearchTree(4);
Assert.Equal(4, tree.Value);
var sut = new BinarySearchTree<int>();
Assert.Equal(0, sut.Count);
}

[Fact(Skip = "Remove this Skip property to run this test")]
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(Skip = "Remove this Skip property to run this test")]
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(Skip = "Remove this Skip property to run this test")]
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(Skip = "Remove this Skip property to run this test")]
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(Skip = "Remove this Skip property to run this test")]
public void Can_sort_single_number()
[Fact]
public void New_bst_has_no_depth()
{
var tree = new BinarySearchTree(2);
Assert.Equal(new[] { 2 }, tree.AsEnumerable());
var sut = new BinarySearchTree<int>();
Assert.Equal(0, sut.Depth);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_smaller_than_first()
[Fact]
public void Single_value_results_in_depth_one()
{
var tree = new BinarySearchTree(new[] { 2, 1 });
Assert.Equal(new[] { 1, 2 }, tree.AsEnumerable());
var sut = new BinarySearchTree<int>();
sut.Add(1);
Assert.Equal(1, sut.Depth);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_same_as_first()
[Fact]
public void Single_value_can_be_found()
{
var tree = new BinarySearchTree(new[] { 2, 2 });
Assert.Equal(new[] { 2, 2 }, tree.AsEnumerable());
var sut = new BinarySearchTree<int>();
sut.Add(2);
Assert.True(sut.Contains(2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_greater_than_first()
[Fact]
public void Balanced_three_is_well_behaved()
{
var tree = new BinarySearchTree(new[] { 2, 3 });
Assert.Equal(new[] { 2, 3 }, tree.AsEnumerable());
var sut = new BinarySearchTree<int>();
sut.Add(5);
sut.Add(3);
sut.Add(7);
Assert.Equal(3, sut.Count);
Assert.Equal(2, sut.Depth);
Assert.True(sut.Contains(3));
Assert.True(sut.Contains(5));
Assert.True(sut.Contains(7));
Assert.False(sut.Contains(4));
Assert.False(sut.Contains(6));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_complex_tree()
[Fact]
public void Left_heavy_five_is_well_behaved()
{
var tree = new BinarySearchTree(new[] { 2, 1, 3, 6, 7, 5 });
Assert.Equal(new[] { 1, 2, 3, 5, 6, 7 }, tree.AsEnumerable());
var sut = new BinarySearchTree<int>();
sut.Add(5);
sut.Add(3);
sut.Add(2);
sut.Add(5);
sut.Add(1);
Assert.Equal(5, sut.Count);
Assert.Equal(4, sut.Depth);
Assert.True(sut.Contains(1));
Assert.True(sut.Contains(2));
Assert.False(sut.Contains(8));
}
}