Deletion in Binary Search Tree

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Deletion in Binary Search Tree

Karthik77
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Deletion in Binary Search Tree

Bert Gunter-2
"I am unable to complete the R code for deleting nodes in a binary search
tree."

What does this mean? What errors did you receive or what did or did not
happen with your current code? With what code was the tree built?

Also, this smells like homework. We do not do homework (or try not to) on
this list.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Tue, Oct 3, 2017 at 7:28 AM, Karthiko S <[hidden email]> wrote:

> 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]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.