Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:06:56+00:00 2026-05-14T05:06:56+00:00

I’ve been trying to implement a delete function for a Binary Search Tree but

  • 0

I’ve been trying to implement a delete function for a Binary Search Tree but haven’t been able to get it to work in all cases.

This is my latest attempt:

Node* RBT::BST_remove(int c)
{
    Node* t = get_node(c);
    Node* temp = t;

    if(t->get_left() == empty)
        *t = *t->get_left();
    else if(t->get_right() == empty)
        *t = *t->get_right();
    else if((t->get_left() != empty) && (t->get_right() != empty))
    {
        Node* node = new Node(t->get_data(), t->get_parent(), t->get_colour(), t->get_left(), t->get_right());
        *t = *node;
    }

    return temp;
}

Node* RBT::get_node(int c)
{
    Node* pos = root;

    while(pos != empty)
    {
        if(c < pos->get_data())
            pos = pos->get_left();
        else if(c == pos->get_data())
            return pos;
        else
            pos = pos->get_right();
    }

    return NULL;
}

t is a node and empty is just a node with nothing in it.

I’m just trying to swap the values but I’m getting a runtime error. Any ideas?

edit: I’m returning temp to delete it afterwards.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:06:56+00:00Added an answer on May 14, 2026 at 5:06 am

    First, your last else if conditional clause is redundant. Swap it with an else clause.

    Secondly, I think it would make things easier for you if you’d take as parameter a pointer to the node to remove. You can write a find() function which would find a node given its key. I’m assuming of course that you can change the function signature. If you can take as parameter the node to remove you can focus on removing the node rather than add logic for finding the node. Otherwise, still write that find() function and use that for getting the pointer to the relevant node.

    When you remove a node in a binary search tree you must maintain the ordering so the tree doesn’t lose its integrity. Recall that there is a specific ordering in the tree that supports the fast retrieval of elements. So, enumerate the possible cases:

    1. The node to delete has no children. That’s easy: just release its resources and you’re done.
    2. The node has a single child node. That’s fairly simple too. Release the node and replace it with its child, so the child holds the removed node’s place in the tree.
    3. The node has two children. Let’s call the node D. Find the right-most child of D‘s left subtree. Let’s call this node R. Assign the value of R to D, and delete R (as described in this algorithm). Notice that R can have zero or one children.

    The third scenario, illustrated:

             .
            .
           .
          /
         D
        / \
       /\  .
      /  \
     /    \
    +------+
            \
             R
            /
           ?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.