From 31888a152943d27ad38ef5cd8197532b9c9784c2 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 13 Apr 2022 21:17:33 +0100 Subject: [PATCH] Updated Tree to ensure no null values are added (Incomparable) and updated descend function to use traverse --- C#/Datastructures/BinarySearchTree/Tree.cs | 28 ++++++------------- .../BinarySearchTree/TreeNode.cs | 4 +-- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/C#/Datastructures/BinarySearchTree/Tree.cs b/C#/Datastructures/BinarySearchTree/Tree.cs index 8e2d012..d1c4fc2 100644 --- a/C#/Datastructures/BinarySearchTree/Tree.cs +++ b/C#/Datastructures/BinarySearchTree/Tree.cs @@ -15,8 +15,8 @@ namespace C_.Datastructures.BinarySearchTree Count = 0 }; } - - public static Tree Create(T? value){ + + public static Tree Create(T value){ //Create a new Tree with Head return new Tree{ Root = TreeNode.Create(value) @@ -85,24 +85,12 @@ namespace C_.Datastructures.BinarySearchTree TreeNode? 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; + } } } \ No newline at end of file diff --git a/C#/Datastructures/BinarySearchTree/TreeNode.cs b/C#/Datastructures/BinarySearchTree/TreeNode.cs index 60cee3c..87c0c59 100644 --- a/C#/Datastructures/BinarySearchTree/TreeNode.cs +++ b/C#/Datastructures/BinarySearchTree/TreeNode.cs @@ -9,14 +9,14 @@ namespace C_.Datastructures.BinarySearchTree internal class TreeNode : DirectedNode> { //All properties inherited from base class - public static TreeNode Create(T? value){ + public static TreeNode Create(T value){ //Create a new node without any children return new TreeNode{ Value = value }; } - public static TreeNode Create(T? value, TreeNode? left, TreeNode? right){ + public static TreeNode Create(T value, TreeNode? left, TreeNode? right){ //Create a new node with the option of having children return new TreeNode{ Value = value,