Changes BST to Binary Tree
This commit is contained in:
96
C#/Datastructures/BinaryTree/Tree.cs
Normal file
96
C#/Datastructures/BinaryTree/Tree.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using C_.Datastructures.BinaryTree;
|
||||
using System;
|
||||
|
||||
namespace C_.Datastructures.BinaryTree
|
||||
{
|
||||
internal class Tree<T> where T:IComparable
|
||||
{
|
||||
public TreeNode<T>? Root { get; set; }
|
||||
public int Count { get; set; }
|
||||
|
||||
public static Tree<T> Create(){
|
||||
//Create a new Tree with no Head
|
||||
return new Tree<T>{
|
||||
Root = null,
|
||||
Count = 0
|
||||
};
|
||||
}
|
||||
|
||||
public static Tree<T> Create(T value){
|
||||
//Create a new Tree with Head
|
||||
return new Tree<T>{
|
||||
Root = TreeNode<T>.Create(value)
|
||||
};
|
||||
}
|
||||
|
||||
public void Add(T value)
|
||||
{//Add item to the correct position in the tree (Input cannot be null)
|
||||
Count++;
|
||||
|
||||
if (Root == default)
|
||||
{//If new node should become the root
|
||||
Root = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
|
||||
//Find position to insert
|
||||
TreeNode<T> node = Root;
|
||||
node = Descend(value, node)!;
|
||||
|
||||
if (value.CompareTo(node.Value) < 0)
|
||||
{//Insert to left
|
||||
node.Left = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
//Insert to right
|
||||
node.Right = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
|
||||
public TreeNode<T>? Find(T value)
|
||||
{//Return true if the item can be found within the tree
|
||||
if (Root == default || Root.Value!.Equals(default))
|
||||
return default;
|
||||
|
||||
TreeNode<T>? current = Root;
|
||||
|
||||
while (current != default)
|
||||
{
|
||||
//Compare value at node to see if we are looking for the root item
|
||||
if (current.Value!.Equals(value))
|
||||
return current;
|
||||
|
||||
current = Traverse(value, current);
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
private TreeNode<T>? Traverse(T value, TreeNode<T> node)
|
||||
{//T is comparable so use methods to determine which way to traverse
|
||||
if(node == default)
|
||||
return default;
|
||||
|
||||
if (value.CompareTo(node.Value) < 0)
|
||||
{//Traverse Left
|
||||
return node.Left;
|
||||
}
|
||||
//Traverse Right
|
||||
return node.Right;
|
||||
}
|
||||
|
||||
private TreeNode<T>? Descend(T value, TreeNode<T>? current)
|
||||
{//Keep trying to determine whether to go left or right until null node is found that can be appended to
|
||||
if (current == default)
|
||||
return default;
|
||||
|
||||
TreeNode<T>? node;
|
||||
|
||||
node = Descend(value, Traverse(value, current));
|
||||
if (node == null)
|
||||
{
|
||||
return current;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
28
C#/Datastructures/BinaryTree/TreeNode.cs
Normal file
28
C#/Datastructures/BinaryTree/TreeNode.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.BinaryTree
|
||||
{
|
||||
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
|
||||
{
|
||||
//All properties inherited from base class
|
||||
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){
|
||||
//Create a new node with the option of having children
|
||||
return new TreeNode<T>{
|
||||
Value = value,
|
||||
Left = left,
|
||||
Right = right
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user