Delete method finally finished, parent

and root nodes are finally fully traced!
This commit is contained in:
lukejelse04 2021-07-08 15:17:25 +01:00
parent 579b3dc0de
commit c9f16de23b
2 changed files with 24 additions and 27 deletions

View File

@ -10,7 +10,7 @@ namespace BinaryTree
Random random = new Random(); Random random = new Random();
for (var i = 0; i < 10; i++) for (var i = 0; i < 50; i++)
{ {
tree.Add(random.Next(0, 100)); tree.Add(random.Next(0, 100));
} }
@ -23,9 +23,6 @@ namespace BinaryTree
Console.WriteLine("Delete node: "); Console.WriteLine("Delete node: ");
tree.Delete(Convert.ToInt32(Console.ReadLine())); tree.Delete(Convert.ToInt32(Console.ReadLine()));
tree.Print(tree.Root);
Console.ReadLine();
} }

View File

@ -42,8 +42,7 @@ namespace BinaryTree
}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){
@ -65,18 +64,14 @@ namespace BinaryTree
if (_Data != Current.Data) if (_Data != Current.Data)
{ {
Current = Traverse(_Data, Current); Current = Traverse(_Data, Current);
}else{ }else{
break; break;
} }
} }
} }
catch (System.Exception) catch
{ {
//If it can't be found, return false //If it can't be found, return false
return false; return false;
@ -99,9 +94,7 @@ namespace BinaryTree
Current.Data = temp.Data; Current.Data = temp.Data;
//Find if the temp node is a left or a right now 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());
@ -110,16 +103,11 @@ namespace BinaryTree
if (temp.GetLeft() != null) if (temp.GetLeft() != null)
{ {
temp.GetParent().SetRight(temp.GetLeft()); temp.GetParent().SetRight(temp.GetLeft());
temp.GetLeft().SetParent(temp.GetParent());//---
}else{ }else{
temp.GetParent().SetRight(null); temp.GetParent().SetRight(null);
} }
} }
//*Code not currently working - Doesn't confidently replace the root or parent nodes of each item that is deleted
// temp.SetParent(Current.GetParent());
// if (Current.GetParent() == null)
// {
// Root = Current;
// }
return true; return true;
@ -136,9 +124,10 @@ namespace BinaryTree
}else{ }else{
Root = Current.GetRight(); Root = Current.GetRight();
} }
return true;
} }
if (Current == Root || Current.Data < Current.GetParent().Data) if (Current.Data < Current.GetParent().Data)
{ {
LeftFlag = true; LeftFlag = true;
} }
@ -151,13 +140,15 @@ namespace BinaryTree
}else{ }else{
Current.GetParent().SetRight(Current.GetLeft()); Current.GetParent().SetRight(Current.GetLeft());
} }
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());
} }
return true; return true;
@ -166,15 +157,24 @@ namespace BinaryTree
}else{ }else{
//If it has no children //If it has no children
if (Current.Data <= Current.GetParent().Data) if (Current == Root)
{ {
Current.GetParent().SetLeft(null); Root = null;
return true; return true;
}else{ }else{
Current.GetParent().SetRight(null);
return true;
}
//Set the parent's relevent pointer to null
if (Current.Data <= Current.GetParent().Data)
{
Current.GetParent().SetLeft(null);
return true;
}else{
Current.GetParent().SetRight(null);
return true;
}
}
} }
} }