Reorganised Repo

This commit is contained in:
luke-else 2021-11-18 17:40:29 +00:00
parent 409a5fd884
commit c942ce57ce
19 changed files with 371 additions and 371 deletions

View File

@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -1,31 +1,31 @@
using System; using System;
namespace BinaryTree namespace BinaryTree
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
tree tree = new tree(); tree tree = new tree();
Random random = new Random(); Random random = new Random();
for (var i = 0; i < 35; i++) for (var i = 0; i < 35; i++)
{ {
tree.Add(random.Next(0, 100)); tree.Add(random.Next(0, 100));
} }
while (true) while (true)
{ {
Console.Clear(); Console.Clear();
tree.Print(tree.Root); tree.Print(tree.Root);
Console.WriteLine("-------------------------------------"); Console.WriteLine("-------------------------------------");
Console.WriteLine("Delete node: "); Console.WriteLine("Delete node: ");
tree.Delete(Convert.ToInt32(Console.ReadLine())); tree.Delete(Convert.ToInt32(Console.ReadLine()));
} }
} }
} }
} }

View File

@ -1,52 +1,52 @@
namespace BinaryTree namespace BinaryTree
{ {
public class node public class node
{ {
public dynamic Data { get; set; } public dynamic Data { get; set; }
private node Parent { get; set; } private node Parent { get; set; }
private node Left { get; set; } private node Left { get; set; }
private node Right { get; set; } private node Right { get; set; }
//Print Method Data //Print Method Data
public int StartPos; public int StartPos;
public int Size { get { return Data.Length; } } public int Size { get { return Data.Length; } }
public int EndPos { get { return StartPos + Size; } set { StartPos = value - Size; } } public int EndPos { get { return StartPos + Size; } set { StartPos = value - Size; } }
public node(dynamic _Data = null, public node(dynamic _Data = null,
node _Parent = null, node _Parent = null,
node _Left = null, node _Left = null,
node _Right = null){ node _Right = null){
//New node for a Binary Tree. Values can be set to null if they are not present //New node for a Binary Tree. Values can be set to null if they are not present
Data = _Data; Data = _Data;
Parent = _Parent; Parent = _Parent;
Left = _Left; Left = _Left;
Right = _Right; Right = _Right;
} }
public node GetParent(){ public node GetParent(){
return Parent; return Parent;
} }
public void SetParent(node _Parent){ public void SetParent(node _Parent){
Parent = _Parent; Parent = _Parent;
} }
public node GetLeft(){ public node GetLeft(){
return Left; return Left;
} }
public void SetLeft(node _Left){ public void SetLeft(node _Left){
Left = _Left; Left = _Left;
} }
public node GetRight(){ public node GetRight(){
return Right; return Right;
} }
public void SetRight(node _Right){ public void SetRight(node _Right){
Right = _Right; Right = _Right;
} }
} }
} }

View File

@ -1,280 +1,280 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace BinaryTree namespace BinaryTree
{ {
public class tree public class tree
{ {
public node Root { get; set; } public node Root { get; set; }
private node Current { get; set; } private node Current { get; set; }
public tree(dynamic _Data = null){ public tree(dynamic _Data = null){
//New tree - Sets the Root of the tree to a new node //New tree - Sets the Root of the tree to a new node
Root = new node(_Data); Root = new node(_Data);
} }
public void Add(dynamic _Data){ public void Add(dynamic _Data){
//If the Root of the List is not set. //If the Root of the List is not set.
if (Root.Data == null) if (Root.Data == null)
{ {
Root.Data = _Data; Root.Data = _Data;
return; return;
}else{ }else{
Current = Root; Current = Root;
} }
//traverse through the tree to the relevant point at which the item should be added //traverse through the tree to the relevant point at which the item should be added
while(true){ while(true){
var temp = Traverse(_Data, Current); var temp = Traverse(_Data, Current);
//If it is null, we are at a leaf node and we can stop traversing the tree //If it is null, we are at a leaf node and we can stop traversing the tree
if (temp == null) if (temp == null)
{ {
break; break;
} }
Current = temp; Current = temp;
} }
//Check whether we need to add the node to the left or right of //Check whether we need to add the node to the left or right of
if (_Data < Current.Data) if (_Data < Current.Data)
{ {
Current.SetLeft(new node(_Data, Current)); Current.SetLeft(new node(_Data, Current));
}else{ }else{
Current.SetRight(new node(_Data, Current)); Current.SetRight(new node(_Data, Current));
} }
} }
private node Traverse(dynamic _Data, node _Current){ private node Traverse(dynamic _Data, node _Current){
//Check whether to branch to the right or the left in the tree. //Check whether to branch to the right or the left in the tree.
if (_Data < _Current.Data) if (_Data < _Current.Data)
{ {
return _Current.GetLeft(); return _Current.GetLeft();
} }
return _Current.GetRight(); return _Current.GetRight();
} }
public bool Delete(dynamic _Data){ public bool Delete(dynamic _Data){
//Traverse to the node we are trying to delete //Traverse to the node we are trying to delete
Current = Root; Current = Root;
//try statement will run unless the node does not exitst, in which case it will throw an exception and return false. //try statement will run unless the node does not exitst, in which case it will throw an exception and return false.
try try
{ {
while(true){ while(true){
if (_Data != Current.Data) if (_Data != Current.Data)
{ {
Current = Traverse(_Data, Current); Current = Traverse(_Data, Current);
}else{ }else{
break; break;
} }
} }
} }
catch catch
{ {
//If it can't be found, return false //If it can't be found, return false
return false; return false;
} }
if (Current.GetLeft() != null && Current.GetRight() != null) if (Current.GetLeft() != null && Current.GetRight() != null)
{ {
//If it has 2 children //If it has 2 children
node temp = Current.GetLeft(); node temp = Current.GetLeft();
//Traverse to node that will replace Current //Traverse to node that will replace Current
while(true){ while(true){
if (temp.GetRight() != null) if (temp.GetRight() != null)
{ {
temp = temp.GetRight(); temp = temp.GetRight();
}else{ }else{
break; break;
} }
} }
Current.Data = temp.Data; Current.Data = temp.Data;
//Find if the temp node is a left or a right node before deleting. //Find if the temp node is a left or a right node before deleting.
if(temp.GetParent() == Current){ if(temp.GetParent() == Current){
//If the current node is parent to our replacing node. (Only 1 level down) //If the current node is parent to our replacing node. (Only 1 level down)
Current.SetLeft(temp.GetLeft()); Current.SetLeft(temp.GetLeft());
if (temp.GetLeft() != null) if (temp.GetLeft() != null)
{ {
temp.GetLeft().SetParent(temp.GetParent()); temp.GetLeft().SetParent(temp.GetParent());
} }
}else{ }else{
//If we could traverse down the left hand side //If we could traverse down the left hand side
if (temp.GetLeft() != null) if (temp.GetLeft() != null)
{ {
temp.GetParent().SetRight(temp.GetLeft()); temp.GetParent().SetRight(temp.GetLeft());
temp.GetLeft().SetParent(temp.GetParent()); temp.GetLeft().SetParent(temp.GetParent());
}else{ }else{
temp.GetParent().SetRight(null); temp.GetParent().SetRight(null);
} }
} }
return true; return true;
}else if(Current.GetLeft() != null || Current.GetRight() != null) }else if(Current.GetLeft() != null || Current.GetRight() != null)
{ {
bool LeftFlag = false; bool LeftFlag = false;
if (Current == Root) if (Current == Root)
{ {
if (Current.GetLeft() != null) if (Current.GetLeft() != null)
{ {
Root = Current.GetLeft(); Root = Current.GetLeft();
}else{ }else{
Root = Current.GetRight(); Root = Current.GetRight();
} }
return true; return true;
} }
if (Current.Data < Current.GetParent().Data) if (Current.Data < Current.GetParent().Data)
{ {
LeftFlag = true; LeftFlag = true;
} }
if (Current.GetLeft() != null) if (Current.GetLeft() != null)
{//If it has a left child {//If it has a left child
if (LeftFlag == true) if (LeftFlag == true)
{ {
Current.GetParent().SetLeft(Current.GetLeft()); Current.GetParent().SetLeft(Current.GetLeft());
}else{ }else{
Current.GetParent().SetRight(Current.GetLeft()); Current.GetParent().SetRight(Current.GetLeft());
} }
Current.GetLeft().SetParent(Current.GetParent()); Current.GetLeft().SetParent(Current.GetParent());
}else{//If it has a right child }else{//If it has a right child
if (LeftFlag == true) if (LeftFlag == true)
{ {
Current.GetParent().SetLeft(Current.GetRight()); Current.GetParent().SetLeft(Current.GetRight());
}else{ }else{
Current.GetParent().SetRight(Current.GetRight()); Current.GetParent().SetRight(Current.GetRight());
} }
Current.GetRight().SetParent(Current.GetParent()); Current.GetRight().SetParent(Current.GetParent());
} }
return true; return true;
}else{ }else{
//If it has no children //If it has no children
if (Current == Root) if (Current == Root)
{ {
Root = null; Root = null;
return true; return true;
}else{ }else{
//Set the parent's relevent pointer to null //Set the parent's relevent pointer to null
if (Current.Data <= Current.GetParent().Data) if (Current.Data <= Current.GetParent().Data)
{ {
Current.GetParent().SetLeft(null); Current.GetParent().SetLeft(null);
return true; return true;
}else{ }else{
Current.GetParent().SetRight(null); Current.GetParent().SetRight(null);
return true; return true;
} }
} }
} }
} }
//print Method functions //print Method functions
public void Print(node root, int topMargin = 2, int leftMargin = 2) public void Print(node root, int topMargin = 2, int leftMargin = 2)
{ {
if (root == null) return; if (root == null) return;
int rootTop = Console.CursorTop + topMargin; int rootTop = Console.CursorTop + topMargin;
var last = new List<node>(); var last = new List<node>();
var next = root; var next = root;
for (int level = 0; next != null; level++) for (int level = 0; next != null; level++)
{ {
var item = new node { Data = next.Data.ToString(" 0 ") }; var item = new node { Data = next.Data.ToString(" 0 ") };
item.SetLeft(next.GetLeft()); item.SetLeft(next.GetLeft());
item.SetRight(next.GetRight()); item.SetRight(next.GetRight());
if (level < last.Count) if (level < last.Count)
{ {
item.StartPos = last[level].EndPos + 1; item.StartPos = last[level].EndPos + 1;
last[level] = item; last[level] = item;
} }
else else
{ {
item.StartPos = leftMargin; item.StartPos = leftMargin;
last.Add(item); last.Add(item);
} }
if (level > 0) if (level > 0)
{ {
item.SetParent(last[level - 1]); item.SetParent(last[level - 1]);
if (next == item.GetParent().GetLeft()) if (next == item.GetParent().GetLeft())
{ {
item.GetParent().SetLeft(item); item.GetParent().SetLeft(item);
item.EndPos = Math.Max(item.EndPos, item.GetParent().StartPos); item.EndPos = Math.Max(item.EndPos, item.GetParent().StartPos);
} }
else else
{ {
item.GetParent().SetRight(item); item.GetParent().SetRight(item);
item.StartPos = Math.Max(item.StartPos, item.GetParent().EndPos); item.StartPos = Math.Max(item.StartPos, item.GetParent().EndPos);
} }
} }
next = next.GetLeft() ?? next.GetRight(); next = next.GetLeft() ?? next.GetRight();
for (; next == null; item = item.GetParent()) for (; next == null; item = item.GetParent())
{ {
Print(item, rootTop + 2 * level); Print(item, rootTop + 2 * level);
if (--level < 0) break; if (--level < 0) break;
if (item == item.GetParent().GetLeft()) if (item == item.GetParent().GetLeft())
{ {
item.GetParent().StartPos = item.EndPos; item.GetParent().StartPos = item.EndPos;
next = item.GetParent().GetRight(); next = item.GetParent().GetRight();
} }
else else
{ {
if (item.GetParent().GetLeft() == null) if (item.GetParent().GetLeft() == null)
item.GetParent().EndPos = item.StartPos; item.GetParent().EndPos = item.StartPos;
else else
item.GetParent().StartPos += (item.StartPos - item.GetParent().EndPos) / 2; item.GetParent().StartPos += (item.StartPos - item.GetParent().EndPos) / 2;
} }
} }
} }
Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1); Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
} }
private void Print(node item, int top) private void Print(node item, int top)
{ {
SwapColors(); SwapColors();
Print(item.Data, top, item.StartPos); Print(item.Data, top, item.StartPos);
SwapColors(); SwapColors();
if (item.GetLeft() != null) if (item.GetLeft() != null)
PrintLink(top + 1, "┌", "┘", item.GetLeft().StartPos + item.GetLeft().Size / 2, item.StartPos); PrintLink(top + 1, "┌", "┘", item.GetLeft().StartPos + item.GetLeft().Size / 2, item.StartPos);
if (item.GetRight() != null) if (item.GetRight() != null)
PrintLink(top + 1, "└", "┐", item.EndPos - 1, item.GetRight().StartPos + item.GetRight().Size / 2); PrintLink(top + 1, "└", "┐", item.EndPos - 1, item.GetRight().StartPos + item.GetRight().Size / 2);
} }
private void PrintLink(int top, string start, string end, int startPos, int endPos) private void PrintLink(int top, string start, string end, int startPos, int endPos)
{ {
Print(start, top, startPos); Print(start, top, startPos);
Print("─", top, startPos + 1, endPos); Print("─", top, startPos + 1, endPos);
Print(end, top, endPos); Print(end, top, endPos);
} }
private void Print(string s, int top, int left, int right = -1) private void Print(string s, int top, int left, int right = -1)
{ {
Console.SetCursorPosition(left, top); Console.SetCursorPosition(left, top);
if (right < 0) right = left + s.Length; if (right < 0) right = left + s.Length;
while (Console.CursorLeft < right) Console.Write(s); while (Console.CursorLeft < right) Console.Write(s);
} }
private void SwapColors() private void SwapColors()
{ {
var color = Console.ForegroundColor; var color = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor; Console.ForegroundColor = Console.BackgroundColor;
Console.BackgroundColor = color; Console.BackgroundColor = color;
} }
} }
} }

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.31229.75 VisualStudioVersion = 16.0.31229.75
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoublyLinkedList", "DoublyLinkedList\DoublyLinkedList.csproj", "{D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoublyLinkedList", "DoublyLinkedList.csproj", "{D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27 VisualStudioVersion = 16.0.30320.27
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prime Numbers (Efficient)", "Prime Numbers (Efficient)\Prime Numbers (Efficient).csproj", "{F438C480-1EEE-4010-A79D-BDFC1712EFBC}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prime Numbers (Efficient)", "Prime Numbers (Efficient).csproj", "{F438C480-1EEE-4010-A79D-BDFC1712EFBC}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution