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
@ -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
|
//Create a new Tree with Head
|
||||||
return new Tree<T>{
|
return new Tree<T>{
|
||||||
Root = TreeNode<T>.Create(value)
|
Root = TreeNode<T>.Create(value)
|
||||||
@ -85,24 +85,12 @@ namespace C_.Datastructures.BinarySearchTree
|
|||||||
|
|
||||||
TreeNode<T>? node;
|
TreeNode<T>? node;
|
||||||
|
|
||||||
if (value.CompareTo(current.Value) < 0)
|
node = Descend(value, Traverse(value, current));
|
||||||
{//Descend left
|
|
||||||
node = Descend(value, current.Left);
|
|
||||||
if (node == null)
|
if (node == null)
|
||||||
{
|
{
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{//Descend right
|
|
||||||
node = Descend(value, current.Right);
|
|
||||||
if (node == null)
|
|
||||||
{
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,14 +9,14 @@ namespace C_.Datastructures.BinarySearchTree
|
|||||||
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
|
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
|
||||||
{
|
{
|
||||||
//All properties inherited from base class
|
//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
|
//Create a new node without any children
|
||||||
return new TreeNode<T>{
|
return new TreeNode<T>{
|
||||||
Value = value
|
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
|
//Create a new node with the option of having children
|
||||||
return new TreeNode<T>{
|
return new TreeNode<T>{
|
||||||
Value = value,
|
Value = value,
|
||||||
|
Loading…
Reference in New Issue
Block a user