Hello every one
I am facing problem to implement remove elements in binary search tree
i have done some code
but it not works properly
can anyone help me to solve this
My code is this
public BSNode findParent(BSNode n)
{
BSNode q=null;
BSNode p=root;
while(p!=n)
{
q=p;
if(n.key>p.key)
{
p=p.right;
}
else
{
p=p.left;
}
return q;
}
return null;
}
private BSNode findSuccessor(BSNode suc)
{
if(suc.right!=null)
{
BSNode p=findLeftMost(suc.right);
return p;
}
else
{
BSNode q=suc;
BSNode p=findParent(q);
while(p!=null&&p.right==q)
{
q=p;
p=findParent(p);
}
return p;
}

}
public void removeNode(int delKey)
{
BSNode bn=findNode(delKey);
BSNode bnparent=findParent(bn);
if(bn.right!=null&&bn.left!=null)//Internal Node
{
BSNode successor=findSuccessor(bn);
bn.key=successor.key;
bn=successor;
}
BSNode newnode=(bn.left!=null)?bn.left:bn.right;

if(newnode!=null)
{
BSNode newnodeparent=findParent(newnode);
newnodeparent=bnparent;
if(bnparent==null)
{
root=newnode;
}
else if(bnparent.left==bn)
{
bnparent.left=newnode;
}
else
{
bnparent.right=newnode;
}
}
else if(bnparent==null)
{
root=null;
}
else
{
if(bnparent!=null)
{
if(bn==bnparent.left)
{
bnparent.left=null;
}
else
{
bnparent.right=null;
}
}
}
bn.left=bn.right=bnparent=null;
}
public BSNode findNode(int k)
{
BSNode n=root;
while(n!=null)
{
if(k==n.key)
{
return n;
}
else if(k<n.key)
{
n=n.left;
}
else
{
n=n.right;
}
}
return null;
}