Fixed delete method - Or maybe not
This commit is contained in:
parent
615b157123
commit
32fbf6f4ec
@ -7,16 +7,18 @@ namespace BinaryTree
|
||||
static void Main(string[] args)
|
||||
{
|
||||
tree tree = new tree();
|
||||
tree.Add(5);
|
||||
tree.Add(7);
|
||||
tree.Add(6);
|
||||
tree.Add(8);
|
||||
tree.Add(3);
|
||||
tree.Add(1);
|
||||
tree.Add(4);
|
||||
tree.Add(0.5);
|
||||
|
||||
tree.Delete(1);
|
||||
Random random = new Random();
|
||||
|
||||
for (var i = 0; i < 1000; i++)
|
||||
{
|
||||
tree.Add(random.Next(0, 10000));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
tree.Delete(tree.Root.Data);
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
|
@ -82,8 +82,9 @@ namespace BinaryTree
|
||||
if (Current.GetLeft() != null && Current.GetRight() != null)
|
||||
{
|
||||
//If it has 2 children
|
||||
node temp = new node();
|
||||
temp = Current.GetLeft();
|
||||
node temp = Current.GetLeft();
|
||||
|
||||
//Traverse to node that will replace Current
|
||||
while(true){
|
||||
if (temp.GetRight() != null)
|
||||
{
|
||||
@ -95,13 +96,21 @@ namespace BinaryTree
|
||||
|
||||
Current.Data = temp.Data;
|
||||
|
||||
if (temp.GetLeft() != null)
|
||||
{
|
||||
temp.GetParent().SetRight(temp.GetLeft());
|
||||
}else{
|
||||
temp.GetParent().SetRight(null);
|
||||
}
|
||||
//Find if the temp node is a left or a right now before deleting.
|
||||
//Adopt subtree Nodes// -- Subtrees are lost (If temp node is only 1 level down, data is lost)
|
||||
// if (temp.GetLeft() != null)
|
||||
// {
|
||||
// temp.GetParent().SetRight(temp.GetLeft());
|
||||
// }else{
|
||||
// temp.GetParent().SetRight(null);
|
||||
// }
|
||||
|
||||
|
||||
if(temp.GetParent() == Current){
|
||||
Current.SetLeft(null);
|
||||
}else{
|
||||
Current.SetLeft(temp.GetLeft());
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
@ -136,6 +145,5 @@ namespace BinaryTree
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user