Updated Tree to ensure no null values are added (Incomparable) and updated descend function to use traverse

This commit is contained in:
Luke Else 2022-04-13 21:17:33 +01:00
parent 9fab683f85
commit 31888a1529
2 changed files with 10 additions and 22 deletions

View File

@ -16,7 +16,7 @@ namespace C_.Datastructures.BinarySearchTree
};
}
public static Tree<T> Create(T? value){
public static Tree<T> Create(T value){
//Create a new Tree with Head
return new Tree<T>{
Root = TreeNode<T>.Create(value)
@ -85,24 +85,12 @@ namespace C_.Datastructures.BinarySearchTree
TreeNode<T>? node;
if (value.CompareTo(current.Value) < 0)
{//Descend left
node = Descend(value, current.Left);
if (node == null)
{
return current;
}
return node;
}
else
{//Descend right
node = Descend(value, current.Right);
if (node == null)
{
return current;
}
return node;
node = Descend(value, Traverse(value, current));
if (node == null)
{
return current;
}
return node;
}
}
}

View File

@ -9,14 +9,14 @@ namespace C_.Datastructures.BinarySearchTree
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
{
//All properties inherited from base class
public static TreeNode<T> Create(T? value){
public static TreeNode<T> Create(T value){
//Create a new node without any children
return new TreeNode<T>{
Value = value
};
}
public static TreeNode<T> Create(T? value, TreeNode<T>? left, TreeNode<T>? right){
public static TreeNode<T> Create(T value, TreeNode<T>? left, TreeNode<T>? right){
//Create a new node with the option of having children
return new TreeNode<T>{
Value = value,