-
-
Notifications
You must be signed in to change notification settings - Fork 366
Binary search tree encapsulated #2131
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
Draft
michalporeba
wants to merge
8
commits into
exercism:main
Choose a base branch
from
michalporeba:binary-search-tree-encapsulated
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcf36e5
BST encapsulated version
michalporeba 4fb7212
adding myself to the contributor's list
michalporeba 4a0c7a3
Update exercises/practice/binary-search-tree/BinarySearchTree.cs
michalporeba d16a2b8
Update exercises/practice/binary-search-tree/BinarySearchTree.cs
michalporeba 9fafef1
Merge branch 'exercism:main' into binary-search-tree-encapsulated
michalporeba 4f81127
standard tests through serialisation
michalporeba 1df3bd4
skip all but the first tests
michalporeba a8fd858
the new standard not implemented messages
michalporeba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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; | ||
} | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
"j2jensen", | ||
"robkeim", | ||
"ShamilS", | ||
"wolf99" | ||
"wolf99", | ||
"michalporeba" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
This file contains hidden or 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
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."); | ||
|
||
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
104
exercises/practice/binary-search-tree/BinarySearchTreeTests.cs
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains hidden or 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
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.