2022-04-14 20:16:41 +00:00
|
|
|
using C_.Datastructures.Stack;
|
2022-04-14 21:21:40 +00:00
|
|
|
using C_.Datastructures.LinkedList;
|
2022-04-04 20:36:04 +00:00
|
|
|
using System;
|
|
|
|
|
2022-04-14 16:26:06 +00:00
|
|
|
namespace C_.Datastructures.BinaryTree
|
2022-04-04 20:36:04 +00:00
|
|
|
{
|
|
|
|
internal class Tree<T> where T:IComparable
|
|
|
|
{
|
2022-04-05 21:35:33 +00:00
|
|
|
public TreeNode<T>? Root { get; set; }
|
2022-04-04 20:36:04 +00:00
|
|
|
public int Count { get; set; }
|
|
|
|
|
2022-04-13 20:08:58 +00:00
|
|
|
public static Tree<T> Create(){
|
2022-04-04 20:36:04 +00:00
|
|
|
//Create a new Tree with no Head
|
|
|
|
return new Tree<T>{
|
2022-04-05 21:35:33 +00:00
|
|
|
Root = null,
|
2022-04-04 20:36:04 +00:00
|
|
|
Count = 0
|
|
|
|
};
|
|
|
|
}
|
2022-04-13 20:17:33 +00:00
|
|
|
|
|
|
|
public static Tree<T> Create(T value){
|
2022-04-04 20:36:04 +00:00
|
|
|
//Create a new Tree with Head
|
|
|
|
return new Tree<T>{
|
2022-04-05 21:35:33 +00:00
|
|
|
Root = TreeNode<T>.Create(value)
|
2022-04-04 20:36:04 +00:00
|
|
|
};
|
|
|
|
}
|
2022-04-05 21:35:33 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:55:59 +00:00
|
|
|
public bool Delete(T value)
|
|
|
|
{
|
|
|
|
//Check if root of tree is null
|
|
|
|
if (Count == 0)
|
|
|
|
return false;
|
|
|
|
|
2022-04-14 21:21:40 +00:00
|
|
|
//Check if the only value is the root
|
2022-04-14 20:55:59 +00:00
|
|
|
if (Count == 1)
|
|
|
|
{
|
|
|
|
if (Root!.Value!.Equals(value))
|
|
|
|
{//If the only item is the one we are trying to delete
|
|
|
|
Count = 0;
|
|
|
|
Root = default;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Stack to store the items leading up to and including the one we are trying to delete
|
|
|
|
Stack<TreeNode<T>>? deletionStack;
|
|
|
|
|
|
|
|
//Search for item being deleted + Parents
|
|
|
|
deletionStack = Find(value);
|
|
|
|
|
|
|
|
if (deletionStack == default)
|
|
|
|
return false; //Item was not found
|
|
|
|
|
|
|
|
//Delete Item (replace) and replace pointers to retain integrity of tree
|
|
|
|
|
|
|
|
TreeNode<T>? node = deletionStack.Pop();
|
|
|
|
|
|
|
|
//stack to store the items leading up to the value that we will use to replace the node
|
|
|
|
Stack<TreeNode<T>>? replacementStack = Min(node!.Right);
|
2022-04-17 22:14:18 +00:00
|
|
|
|
|
|
|
if (replacementStack == default)
|
|
|
|
{//Nothing to the right of the value we are deleting
|
|
|
|
if (deletionStack.Peek() != default)
|
|
|
|
{//Parent adopts left hand side of node if present
|
|
|
|
deletionStack.Pop()!.Left = node.Left;
|
|
|
|
}
|
2022-04-14 20:55:59 +00:00
|
|
|
|
2022-04-17 22:14:18 +00:00
|
|
|
if (node.Left != default)
|
|
|
|
{//Node adopts left value if no lower value to the right
|
|
|
|
node.Value = node.Left!.Value;
|
|
|
|
node.Left = node.Left.Left;
|
2022-04-14 20:55:59 +00:00
|
|
|
}
|
2022-04-17 22:14:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{//Replace the value + reorder nodes
|
|
|
|
node.Value = replacementStack.Peek()!.Value;
|
|
|
|
TreeNode<T>? replacementNode = replacementStack.Pop();
|
|
|
|
|
|
|
|
switch (replacementStack.GetCount())
|
|
|
|
{//Determine what to do based on number of items in replacement stack
|
|
|
|
case 1:
|
|
|
|
node.Right = replacementNode!.Right;
|
|
|
|
break;
|
|
|
|
case >=2:
|
|
|
|
replacementStack.Peek()!.Left = replacementNode!.Right;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-04-14 20:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Count--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-14 21:21:40 +00:00
|
|
|
public LinkedList<T>? Inorder(TreeNode<T>? node)
|
|
|
|
{
|
|
|
|
LinkedList<T> list = LinkedList<T>.Create();
|
|
|
|
|
|
|
|
if (Count == 0)
|
|
|
|
return default;
|
|
|
|
|
|
|
|
if (node == null)
|
|
|
|
node = Root;
|
|
|
|
|
|
|
|
Inorder(list, node!);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
private void Inorder(LinkedList<T> list, TreeNode<T> node)
|
|
|
|
{
|
|
|
|
if (node.Left != default)
|
|
|
|
Inorder(list, node.Left);
|
|
|
|
|
|
|
|
list.Append(node.Value!);
|
|
|
|
|
|
|
|
if (node.Right != default)
|
|
|
|
Inorder(list, node.Right);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-04-14 20:55:59 +00:00
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
private Stack<TreeNode<T>>? Find(T value)
|
2022-04-13 20:08:58 +00:00
|
|
|
{//Return true if the item can be found within the tree
|
|
|
|
if (Root == default || Root.Value!.Equals(default))
|
2022-04-14 16:26:06 +00:00
|
|
|
return default;
|
2022-04-13 20:08:58 +00:00
|
|
|
|
|
|
|
TreeNode<T>? current = Root;
|
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
Stack<TreeNode<T>>? stack = Stack<TreeNode<T>>.Create(Root);
|
|
|
|
|
|
|
|
while (stack.Peek() != default)
|
2022-04-10 21:27:56 +00:00
|
|
|
{
|
2022-04-13 20:08:58 +00:00
|
|
|
//Compare value at node to see if we are looking for the root item
|
2022-04-14 20:16:41 +00:00
|
|
|
if (stack.Peek()!.Value!.Equals(value))
|
|
|
|
return stack;
|
2022-04-10 21:27:56 +00:00
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
stack.Push(Traverse(value, stack.Peek()));
|
2022-04-10 21:27:56 +00:00
|
|
|
}
|
2022-04-14 16:26:06 +00:00
|
|
|
return default;
|
2022-04-08 21:35:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
private Stack<TreeNode<T>>? Min(TreeNode<T>? node)
|
|
|
|
{//Returns a Stack with the value on top being the minimum of the subtree
|
|
|
|
|
2022-04-14 19:13:35 +00:00
|
|
|
if(node == default)
|
|
|
|
return default;
|
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
//Stack to store and be able to get the parent values
|
|
|
|
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
|
|
|
|
|
2022-04-14 19:13:35 +00:00
|
|
|
while(true){
|
2022-04-14 20:16:41 +00:00
|
|
|
if (stack.Peek()!.Left == default)
|
|
|
|
return stack;
|
2022-04-14 19:13:35 +00:00
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
stack.Push(stack.Peek()!.Left);
|
2022-04-14 19:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
private Stack<TreeNode<T>>? Max(TreeNode<T>? node)
|
|
|
|
{///Returns a Stack with the value on top being the maximum of the subtree
|
2022-04-14 19:13:35 +00:00
|
|
|
if(node == default)
|
|
|
|
return default;
|
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
//Stack to store and be able to get the parent values
|
|
|
|
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
|
|
|
|
|
2022-04-14 19:13:35 +00:00
|
|
|
while(true){
|
2022-04-14 20:16:41 +00:00
|
|
|
if (stack.Peek()!.Right == default)
|
|
|
|
return stack;
|
2022-04-14 19:13:35 +00:00
|
|
|
|
2022-04-14 20:16:41 +00:00
|
|
|
stack.Push(stack.Peek()!.Right);
|
2022-04-14 19:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 20:16:41 +00:00
|
|
|
|
2022-04-05 21:35:33 +00:00
|
|
|
private TreeNode<T>? Descend(T value, TreeNode<T>? current)
|
2022-04-08 21:35:55 +00:00
|
|
|
{//Keep trying to determine whether to go left or right until null node is found that can be appended to
|
2022-04-05 21:35:33 +00:00
|
|
|
if (current == default)
|
|
|
|
return default;
|
|
|
|
|
|
|
|
TreeNode<T>? node;
|
|
|
|
|
2022-04-13 20:17:33 +00:00
|
|
|
node = Descend(value, Traverse(value, current));
|
|
|
|
if (node == null)
|
|
|
|
{
|
|
|
|
return current;
|
2022-04-05 21:35:33 +00:00
|
|
|
}
|
2022-04-13 20:17:33 +00:00
|
|
|
return node;
|
2022-04-16 21:36:03 +00:00
|
|
|
}
|
2022-04-04 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|