Changes BST to Binary Tree

This commit is contained in:
Luke Else 2022-04-14 17:26:06 +01:00
parent 31888a1529
commit a2c7326e4b
3 changed files with 20 additions and 11 deletions

View File

@ -1,7 +1,7 @@
using C_.Datastructures.BinarySearchTree; using C_.Datastructures.BinaryTree;
using System; using System;
namespace C_.Datastructures.BinarySearchTree namespace C_.Datastructures.BinaryTree
{ {
internal class Tree<T> where T:IComparable internal class Tree<T> where T:IComparable
{ {
@ -47,10 +47,10 @@ namespace C_.Datastructures.BinarySearchTree
return; return;
} }
public bool Find(T value) public TreeNode<T>? Find(T value)
{//Return true if the item can be found within the tree {//Return true if the item can be found within the tree
if (Root == default || Root.Value!.Equals(default)) if (Root == default || Root.Value!.Equals(default))
return false; return default;
TreeNode<T>? current = Root; TreeNode<T>? current = Root;
@ -58,11 +58,11 @@ namespace C_.Datastructures.BinarySearchTree
{ {
//Compare value at node to see if we are looking for the root item //Compare value at node to see if we are looking for the root item
if (current.Value!.Equals(value)) if (current.Value!.Equals(value))
return true; return current;
current = Traverse(value, current); current = Traverse(value, current);
} }
return false; return default;
} }
private TreeNode<T>? Traverse(T value, TreeNode<T> node) private TreeNode<T>? Traverse(T value, TreeNode<T> node)

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using C_.Datastructures.Generic; using C_.Datastructures.Generic;
namespace C_.Datastructures.BinarySearchTree namespace C_.Datastructures.BinaryTree
{ {
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>> internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
{ {

View File

@ -1,5 +1,5 @@
using System; using System;
using C_.Datastructures.BinarySearchTree; using C_.Datastructures.BinaryTree;
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
@ -99,6 +99,17 @@ Console.WriteLine("Hello, World!");
//queue.Pop(); //queue.Pop();
//queue.Pop(); //queue.Pop();
Tree<int> tree = new Tree<int>(); Tree<int> tree = new Tree<int>();
tree.Add(4); tree.Add(4);
@ -111,9 +122,7 @@ tree.Add(1);
tree.Add(4); tree.Add(4);
tree.Add(7); tree.Add(7);
tree.Find(2); var x = tree.Find(2);
Console.ReadLine(); Console.ReadLine();