From a2c7326e4bdd3cc95a9c9a867c2cad8e519f6d43 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 14 Apr 2022 17:26:06 +0100 Subject: [PATCH] Changes BST to Binary Tree --- .../{BinarySearchTree => BinaryTree}/Tree.cs | 12 ++++++------ .../TreeNode.cs | 2 +- C#/Program.cs | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) rename C#/Datastructures/{BinarySearchTree => BinaryTree}/Tree.cs (92%) rename C#/Datastructures/{BinarySearchTree => BinaryTree}/TreeNode.cs (94%) diff --git a/C#/Datastructures/BinarySearchTree/Tree.cs b/C#/Datastructures/BinaryTree/Tree.cs similarity index 92% rename from C#/Datastructures/BinarySearchTree/Tree.cs rename to C#/Datastructures/BinaryTree/Tree.cs index d1c4fc2..fbe7f3f 100644 --- a/C#/Datastructures/BinarySearchTree/Tree.cs +++ b/C#/Datastructures/BinaryTree/Tree.cs @@ -1,7 +1,7 @@ -using C_.Datastructures.BinarySearchTree; +using C_.Datastructures.BinaryTree; using System; -namespace C_.Datastructures.BinarySearchTree +namespace C_.Datastructures.BinaryTree { internal class Tree where T:IComparable { @@ -47,10 +47,10 @@ namespace C_.Datastructures.BinarySearchTree return; } - public bool Find(T value) + public TreeNode? Find(T value) {//Return true if the item can be found within the tree if (Root == default || Root.Value!.Equals(default)) - return false; + return default; TreeNode? current = Root; @@ -58,11 +58,11 @@ namespace C_.Datastructures.BinarySearchTree { //Compare value at node to see if we are looking for the root item if (current.Value!.Equals(value)) - return true; + return current; current = Traverse(value, current); } - return false; + return default; } private TreeNode? Traverse(T value, TreeNode node) diff --git a/C#/Datastructures/BinarySearchTree/TreeNode.cs b/C#/Datastructures/BinaryTree/TreeNode.cs similarity index 94% rename from C#/Datastructures/BinarySearchTree/TreeNode.cs rename to C#/Datastructures/BinaryTree/TreeNode.cs index 87c0c59..acccd2c 100644 --- a/C#/Datastructures/BinarySearchTree/TreeNode.cs +++ b/C#/Datastructures/BinaryTree/TreeNode.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Threading.Tasks; using C_.Datastructures.Generic; -namespace C_.Datastructures.BinarySearchTree +namespace C_.Datastructures.BinaryTree { internal class TreeNode : DirectedNode> { diff --git a/C#/Program.cs b/C#/Program.cs index d4b046f..96bcf3b 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -1,5 +1,5 @@ using System; -using C_.Datastructures.BinarySearchTree; +using C_.Datastructures.BinaryTree; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); @@ -99,6 +99,17 @@ Console.WriteLine("Hello, World!"); //queue.Pop(); //queue.Pop(); + + + + + + + + + + + Tree tree = new Tree(); tree.Add(4); @@ -111,9 +122,7 @@ tree.Add(1); tree.Add(4); tree.Add(7); -tree.Find(2); - - +var x = tree.Find(2); Console.ReadLine();