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
122 changes: 78 additions & 44 deletions exercises/practice/binary-search-tree/.meta/Example.cs
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();
}
}
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
56 changes: 12 additions & 44 deletions exercises/practice/binary-search-tree/BinarySearchTree.cs
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.");
Comment on lines +7 to +8
Copy link
Member

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:

Suggested change
public int Count
=> throw new NotImplementedException("Please implement the Count property of the BinarySearchTree<T> class.");
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.");
}
Loading
Loading