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

  • SEARCH
  • Home
  • 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 8561745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:34:18+00:00 2026-06-11T16:34:18+00:00

In the chapter on binary search tree’s in CLRS and I encountered the transplant

  • 0

In the chapter on binary search tree’s in CLRS and I encountered the transplant function which replaces a node u with node v with appropriate changes in the parent element.
Here is the code I wrote for transplant function:

void transplant(Node* root, Node* u, Node* v)
{
    if(u->parent == NULL)
        root = v;
    else if(u == u->parent->left)
        u->parent->left = v;
    else
        u->parent->right = v;
    if(v != NULL)
        v->parent = u->parent;
}

It’s not that I don’t understand how this works but that why this works. When I make a function call I’m basically sending a copy of pointers root, u, v to the function right ? so the changes made in the function shouldn’t actually reflect on the root unless I return it or use pointer to pointer type but its actually changing the original root. I defined root as global variable, does that change anything ?

  • 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-06-11T16:34:20+00:00Added an answer on June 11, 2026 at 4:34 pm

    The function will not work if u->parent is NULL, in that case, only the local variable root is set to v, nothing accessible from outside the function changes.

    If u->parent != NULL, then u->parent->left and u->parent->right are the members of the Node pointed to by u->parent, and those are overwritten, so the changes are visible in main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading the Cormen algorithms book (binary search tree chapter) and it says that
In chapter 6 of Learn You a Haskell , the following function is introduced:
If Book aggregates Chapter which in turn aggregates Page, then what should be the
Toward the end of Chapter 16 of the C++ Primer I encountered the following
I'm just revising chapter 4 of C# in Depth which deals with nullable types,
I'm reading through chapter 4 of the magento developers guide which explains how to
url: /contents/view/chapter:models/section:associations class ContentsController extends AppController { function view() { $this->params['named']; } } For
This is concerning chapter 3.5 from SICP, in which streams are being discussed. The
I've read chapter 7 in the 'Linux Device Drivers' (which can be found here)
Another chapter from the arguments between myself and the other senior developer series :P

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.