Binary Tree
public void delete (int d){
TNode Temp = Root;
TNode Parent = Root;
while (Temp!=null) {
if (d<Temp.data && Temp.data!=d){
Today's DSA Code:
Parent = Temp;
Temp=Temp.L;
}
else
{
Temp= Temp.R;
}
}
if (Temp==null){
System.out.println ("Not Found");
}
else {
if (Temp.R == null && Temp.L == null){ //no child
if (Temp.data < Parent.data){
Parent.L = null;
}
else
{
Parent.R = null;
}
}
}
else
if ((Temp.R == null && Temp.L != null) || (Temp.R!=null && Temp.L == null)){
if(Temp.R == null) {
if(d<Parent.data)
Parent.L=Temp.L;
}
else {
Parent.R = Temp.L;
}
if (Temp.L == null){
}
}
No comments:
Post a Comment