[R] Deletion in Binary Search Tree
Karthiko S
karthik.100 at gmail.com
Tue Oct 3 16:28:22 CEST 2017
Dear All,
I am unable to complete the R code for deleting nodes in a binary search
tree.
Have provided the code snippet below for an usecase where the node to be
deleted would have a leftnode and no rightnode. I am stuck up at how to
release the node pertaining to the key value given by the function
call and then
assigning the leftnode in place of the deleted note.
Any help would be appreciated .
deletenode<-function(node,key)
{
if(identical(node,NULL)==TRUE)
{
return(node)
}
else
{
if(key<node$key)
{
node$leftnode<-deletenode(node$leftnode,key)
}
if(key>node$key)
{
node$leftnode<- deletenode(node$rightnode,key)
}
if(key==node$key)
{
if((identical(node$leftnode,NULL)==TRUE)&&
(identical(node$rightnode,NULL)==FALSE))
{
temp<-node
node<-node$rightnode
remove(temp)
return(node)
}
}
deletenode(root,key)
Thanks and Regards
karthik
[[alternative HTML version deleted]]
More information about the R-help
mailing list