TNode tempDelete = this.GetNode(data);
if (tempDelete != null)
{
if ((tempDelete.Left == null ) &&(tempDelete.Right == null)) //Its a Leaf node
{
tempParent = tempDelete.Parent;
if(tempDelete == tempParent.Left) //Justremove by making it null
tempParent.Left = null;
else
tempParent.Right = null;
}
else if ((tempDelete.Left == null ) ||(tempDelete.Right == null)) //It has either Left orRight child
{
tempChild = tempDelete.Left == null? tempDelete.Right : tempDelete.Left; //Get the child
tempParent = tempDelete.Parent; //Getthe parent
if(tempDelete == tempParent.Left) //Makeparent points to it's child so it will automatically deleted like Linked list
tempParent.Left = tempChild;
else
tempParent.Right = tempChild;
}
else if ((tempDelete.Left != null) ||(tempDelete.Right != null)) //It has both Left andRight child
{
TNodepredNode = this.GetNode(this.TreePredecessor_Ite(data)); //Findit's predecessor
if(predNode.Left != null) // Predecessor node canhave no or left child. Do below two steps only if it has left child
{
tempChild = predNode.Left;
predNode.Parent.Right = tempChild; //Assignleft child of predecessor to it's Parent's right.
}
tempDelete.Data = predNode.Data; //Replace the value of predecessor nodeto the value of to be deleted node
//predNode = null; //Remove predecessornode as it's no longer required.
}
return data + " Deleted";
}
else
return "Please enter the valid tree element!";