Fixed Delete method

Need to ensure that both root node and parent nodes are
still being traced (Not currently true)
This commit is contained in:
lukejelse04 2021-07-07 23:21:46 +01:00
parent 32fbf6f4ec
commit 3b5b114952
4 changed files with 171 additions and 22 deletions

View File

@ -14,7 +14,7 @@
"args": [], "args": [],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole", "console": "externalTerminal",
"stopAtEntry": false "stopAtEntry": false
}, },
{ {

View File

@ -10,17 +10,22 @@ namespace BinaryTree
Random random = new Random(); Random random = new Random();
for (var i = 0; i < 1000; i++) for (var i = 0; i < 10; i++)
{ {
tree.Add(random.Next(0, 10000)); tree.Add(random.Next(0, 100));
} }
tree.Delete(tree.Root.Data); tree.Print(tree.Root);
Console.ReadLine(); while (true)
{
Console.WriteLine("-------------------------------------");
Console.WriteLine("Delete node: ");
tree.Delete(Convert.ToInt32(Console.ReadLine()));
tree.Print(tree.Root);
}
} }
} }

View File

@ -7,6 +7,13 @@ namespace BinaryTree
private node Left { get; set; } private node Left { get; set; }
private node Right { get; set; } private node Right { get; set; }
//Print Method Data
public int StartPos;
public int Size { get { return Data.Length; } }
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,

View File

@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
namespace BinaryTree namespace BinaryTree
{ {
public class tree public class tree
@ -107,32 +110,64 @@ namespace BinaryTree
if(temp.GetParent() == Current){ if(temp.GetParent() == Current){
Current.SetLeft(null); //If the current node is parent to our replacing node. (Only 1 level down)
}else{
Current.SetLeft(temp.GetLeft()); Current.SetLeft(temp.GetLeft());
}else{
//If we could traverse down the left hand side
if (temp.GetLeft() != null)
{
temp.GetParent().SetRight(temp.GetLeft());
}else{
temp.GetParent().SetRight(null);
}
} }
return true; return true;
}else if(Current.GetLeft() != null) }else if(Current.GetLeft() != null || Current.GetRight() != null)
{ {
//If it has a left child
Current.Data = Current.GetLeft().Data; bool LeftFlag = false;
Current.SetLeft(null);
if (Current == Root)
{
if (Current.GetLeft() != null)
{
Root = Current.GetLeft();
}else{
Root = Current.GetRight();
}
}
if (Current.Data < Current.GetParent().Data)
{
LeftFlag = true;
}
if (Current.GetLeft() != null)
{//If it has a left child
if (LeftFlag == true)
{
Current.GetParent().SetLeft(Current.GetLeft());
}else{
Current.GetParent().SetRight(Current.GetLeft());
}
}else{//If it has a right child
if (LeftFlag == true)
{
Current.GetParent().SetLeft(Current.GetRight());
}else{
Current.GetParent().SetRight(Current.GetRight());
}
}
return true; return true;
}else if(Current.GetRight() != null){
//If it has a right child
Current.Data = Current.GetRight().Data;
Current.SetRight(null);
return true;
}else{ }else{
//If it has no children //If it has no children
if (Current.Data < Current.GetParent().Data) if (Current.Data <= Current.GetParent().Data)
{ {
Current.GetParent().SetLeft(null); Current.GetParent().SetLeft(null);
return true; return true;
@ -144,6 +179,108 @@ namespace BinaryTree
} }
} }
//print Method functions
public void Print(node root, int topMargin = 2, int leftMargin = 2)
{
if (root == null) return;
int rootTop = Console.CursorTop + topMargin;
var last = new List<node>();
var next = root;
for (int level = 0; next != null; level++)
{
var item = new node { Data = next.Data.ToString(" 0 ") };
item.SetLeft(next.GetLeft());
item.SetRight(next.GetRight());
if (level < last.Count)
{
item.StartPos = last[level].EndPos + 1;
last[level] = item;
}
else
{
item.StartPos = leftMargin;
last.Add(item);
}
if (level > 0)
{
item.SetParent(last[level - 1]);
if (next == item.GetParent().GetLeft())
{
item.GetParent().SetLeft(item);
item.EndPos = Math.Max(item.EndPos, item.GetParent().StartPos);
}
else
{
item.GetParent().SetRight(item);
item.StartPos = Math.Max(item.StartPos, item.GetParent().EndPos);
}
}
next = next.GetLeft() ?? next.GetRight();
for (; next == null; item = item.GetParent())
{
Print(item, rootTop + 2 * level);
if (--level < 0) break;
if (item == item.GetParent().GetLeft())
{
item.GetParent().StartPos = item.EndPos;
next = item.GetParent().GetRight();
}
else
{
if (item.GetParent().GetLeft() == null)
item.GetParent().EndPos = item.StartPos;
else
item.GetParent().StartPos += (item.StartPos - item.GetParent().EndPos) / 2;
}
}
}
Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
}
private void Print(node item, int top)
{
SwapColors();
Print(item.Data, top, item.StartPos);
SwapColors();
if (item.GetLeft() != null)
PrintLink(top + 1, "┌", "┘", item.GetLeft().StartPos + item.GetLeft().Size / 2, item.StartPos);
if (item.GetRight() != null)
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)
{
Print(start, top, startPos);
Print("─", top, startPos + 1, endPos);
Print(end, top, endPos);
}
private void Print(string s, int top, int left, int right = -1)
{
Console.SetCursorPosition(left, top);
if (right < 0) right = left + s.Length;
while (Console.CursorLeft < right) Console.Write(s);
}
private void SwapColors()
{
var color = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor;
Console.BackgroundColor = color;
}
} }
} }