-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
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 all 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 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,113 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
public class BinarySearchTree : IEnumerable<int> | ||
public class BinarySearchTree<T> where T : IComparable | ||
{ | ||
public BinarySearchTree(int value) | ||
class Node | ||
{ | ||
Value = value; | ||
} | ||
|
||
public BinarySearchTree(IEnumerable<int> values) | ||
{ | ||
var array = values.ToArray(); | ||
public T Value { get; set; } | ||
public Node Left { get; set; } | ||
public Node Right { get; set; } | ||
|
||
if (array.Length == 0) | ||
public static object ToData(Node node) | ||
{ | ||
throw new ArgumentException("Cannot create tree from empty list"); | ||
if (node == null) return null; | ||
|
||
return new | ||
{ | ||
data = node.Value, | ||
left = Node.ToData(node.Left), | ||
right = Node.ToData(node.Right) | ||
}; | ||
} | ||
|
||
Value = array[0]; | ||
|
||
foreach (var value in array.Skip(1)) | ||
public IEnumerable<T> GetOrderedValues() | ||
{ | ||
Add(value); | ||
if (Left != null) | ||
{ | ||
foreach(var value in Left.GetOrderedValues()) | ||
{ | ||
yield return value; | ||
} | ||
} | ||
yield return Value; | ||
if (Right != null) | ||
{ | ||
foreach(var value in Right.GetOrderedValues()) | ||
{ | ||
yield return value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int Value { get; } | ||
Node head; | ||
|
||
public BinarySearchTree Left { get; private set; } | ||
public int Count { 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++; | ||
|
||
if (head == null) { | ||
head = new Node { Value = value }; | ||
return; | ||
} | ||
else | ||
|
||
var node = head; | ||
|
||
while(true) | ||
{ | ||
Right = Add(value, Right); | ||
if (node.Value.CompareTo(value) >= 0) | ||
{ | ||
if (node.Left == null) | ||
{ | ||
node.Left = new Node { Value = value }; | ||
break; | ||
} | ||
node = node.Left; | ||
} else { | ||
if (node.Right == null) | ||
{ | ||
node.Right = new Node { Value = value }; | ||
break; | ||
} | ||
node = node.Right; | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private static BinarySearchTree Add(int value, BinarySearchTree tree) | ||
public bool Contains(T value) | ||
{ | ||
if (tree == null) | ||
var node = head; | ||
while(node != null) | ||
{ | ||
return new BinarySearchTree(value); | ||
} | ||
if (node.Value.CompareTo(value) == 0) { return true; } | ||
|
||
return tree.Add(value); | ||
if (node.Value.CompareTo(value) < 0) { node = node.Left; } | ||
else { node = node.Right; } | ||
} | ||
return false; | ||
} | ||
|
||
public IEnumerator<int> GetEnumerator() | ||
public string ToJson() | ||
{ | ||
foreach (var left in Left?.AsEnumerable() ?? Enumerable.Empty<int>()) | ||
if (head == null) return null; | ||
|
||
var settings = new JsonSerializerSettings | ||
{ | ||
yield return left; | ||
} | ||
Formatting = Formatting.Indented, | ||
}; | ||
|
||
yield return Value; | ||
var data = Node.ToData(head); | ||
|
||
foreach (var right in Right?.AsEnumerable() ?? Enumerable.Empty<int>()) | ||
{ | ||
yield return right; | ||
} | ||
return JsonConvert.SerializeObject(data, settings); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
public IEnumerable<T> GetOrderedValues() | ||
{ | ||
return GetEnumerator(); | ||
if (head == null) return new T[0]; | ||
return head.GetOrderedValues(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
"j2jensen", | ||
"robkeim", | ||
"ShamilS", | ||
"wolf99" | ||
"wolf99", | ||
"michalporeba" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
56 changes: 12 additions & 44 deletions
56
exercises/practice/binary-search-tree/BinarySearchTree.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,21 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
public class BinarySearchTree : IEnumerable<int> | ||
public class BinarySearchTree<T> where T : IComparable | ||
{ | ||
public BinarySearchTree(int value) | ||
{ | ||
} | ||
public int Count | ||
=> throw new NotImplementedException("Please implement the Count property of the BinarySearchTree<T> class."); | ||
|
||
public BinarySearchTree(IEnumerable<int> values) | ||
{ | ||
} | ||
public void Add(T value) | ||
=> throw new NotImplementedException("Please implement the Add(T) method of the BinarySearchTree<T> class."); | ||
|
||
public int Value | ||
{ | ||
get | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
} | ||
public bool Contains(T value) | ||
=> throw new NotImplementedException("Please implement the Contains(T) method of the BinarySearchTree<T> class."); | ||
|
||
public BinarySearchTree Left | ||
{ | ||
get | ||
{ | ||
throw new NotImplementedException("You need to implement this function."); | ||
} | ||
} | ||
public string ToJson() | ||
=> throw new NotImplementedException("Please implement the ToJson() method of the BinarySearchTree<T> class."); | ||
|
||
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 IEnumerable<T> GetOrderedValues() | ||
=> throw new NotImplementedException("Please implement the GetOrderedValues() method of the BinarySearchTree<T> class."); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This style is more commonly used in the code base: