Updated Tree to ensure no null values are added (Incomparable) and updated descend function to use traverse
This commit is contained in:
parent
9fab683f85
commit
31888a1529
@ -15,8 +15,8 @@ namespace C_.Datastructures.BinarySearchTree
|
||||
Count = 0
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
node = Descend(value, Traverse(value, current));
|
||||
if (node == null)
|
||||
{
|
||||
return current;
|
||||
}
|
||||
else
|
||||
{//Descend right
|
||||
node = Descend(value, current.Right);
|
||||
if (node == null)
|
||||
{
|
||||
return current;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user