Results 1 to 1 of 1
Thread: Implementing binary search tree
- 08-12-2009, 05:14 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
Implementing binary search tree
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;
}
Similar Threads
-
Binary Search Tree
By Goo in forum New To JavaReplies: 0Last Post: 03-06-2009, 04:46 PM -
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12-04-2008, 02:03 AM -
Binary Search Tree Traversal
By dch414 in forum New To JavaReplies: 2Last Post: 11-07-2008, 12:01 AM -
Can anybody help with cuncurrent binary search tree guys)
By danylo in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:22 PM -
Implementing a red-black tree in java
By baltazar in forum New To JavaReplies: 1Last Post: 07-13-2007, 08:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks