updated tree deletion method

Still not complete but will occasionally delete correctly, seems to have more issues with items near the top of the tree
This commit is contained in:
Luke Else 2022-04-16 22:36:03 +01:00
parent 4b45f5d561
commit e5b0d1659b
2 changed files with 17 additions and 9 deletions

View File

@ -100,10 +100,11 @@ namespace C_.Datastructures.BinaryTree
if (node.Value!.CompareTo(parent!.Value) < 0)
{//Item being deleted is the left child
parent.Left = default;
}
}else{
//Item being deleted is the right value
parent.Right = default;
}
}
Count--;
return true;
}
@ -215,5 +216,13 @@ namespace C_.Datastructures.BinaryTree
}
return node;
}
private void ReplaceChild(TreeNode<T> parent, TreeNode<T>? replacement){
if (parent.Value!.CompareTo(replacement!.Value) < 0)
{//Append to left side
parent.Left = replacement;
}//Append to right side
parent.Right = replacement;
}
}
}

View File

@ -112,17 +112,16 @@ Console.WriteLine("Hello, World!");
Tree<int> tree = new Tree<int>();
tree.Add(4);
tree.Add(5);
tree.Add(7);
tree.Add(6);
tree.Add(2);
tree.Add(4);
tree.Add(3);
tree.Add(2);
tree.Add(1);
tree.Add(8);
tree.Add(7);
tree.Add(0);
var x = tree.Delete(1);
var x = tree.Delete(3);
//tree.Delete(5);
var l = tree.Inorder(null);