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 8211801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:33:15+00:00 2026-06-07T10:33:15+00:00

inline void insert(node *root, int value) { if(!root) { root = new node(); root->value

  • 0
inline void insert(node *root, int value)
{
    if(!root)
    {
        root = new node();
        root->value = value;
    }
    else
    {
        node *itr = root;
        while(1)
        {
            if(itr->value > value)
                itr = itr->left;
            else
                itr = itr->right;
            if(!itr)
            {
                itr = new node();
                itr->value = value; 

                break;
            }

        }
    }
}

//call insert function like this

node *head = 0;
insert(head, 5);
insert(head, 10);
insert(head, 3);
insert(head, 1);
insert(head, 4);

I know this code wouldn’t work because ‘itr’ in insert function is a local variable, thus, it wouldn’t reflect the tree outside of the method.
However, I don’t clearly understand why it wouldn’t work.
Although ‘itr’ is a local variable, ‘itr’ points to the same location where ‘root’ points to. Also, I am dereferencing it to move ‘left’ or ‘right’ so I think it should work.

I think this is a basic problem of passing a pointer by value vs pointer but I couldn’t find a clear explanation of why I can’t change the tree using a local variable of a pointer.

  • 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-07T10:33:17+00:00Added an answer on June 7, 2026 at 10:33 am

    Suppose you had

    int x = 0;
    int y = x;
    y = 3478;
    

    Would you expect that x also contain 3478?
    I knew you wouldn’t, and the same is true for your root and itr.

    This is a classic pencil-and-paper problem (most pointer problems are) and it’s definitely worth pulling out some dead tree when you run into problems like this.

    Here’s an ASCII version for one of your cases, where you want to insert to the right, and right is NULL.
    The arrows show where the respective variables are pointing.

    Start of function:

               ____
    root ---> |    |
              ------
               / \
              /   \
           left   NULL
    

    itr = root;    
               ____
    root ---> |    | <--- itr
              ------
               / \
              /   \
           left   NULL
    

    itr = itr->right;
               ____
    root ---> |    | 
              ------
               / \
              /   \
           left   NULL <--- itr
    

    if (!itr)
        itr = new node();
    
               ____
    root ---> |    | 
              ------
               / \
              /   \                   ____
           left   NULL      itr ---> |    |
                                      ----
    

    As you can see, the input tree hasn’t been modified at all, you just allocated a new node outside of it and left it there.

    This would work:

               ____
    root ---> |    | <--- itr
              ------
               / \
              /   \
           left   NULL
    

       if (!itr->right)
       {
           itr->right = new node()
       }
    
    
               ____
    root ---> |    | <--- itr
              ------
               / \
              /   \
           left   ____
                 |    |
                  ----
    

    Pencil and paper is the best way to figure out pointers.

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

Sidebar

Related Questions

public void insert(final String key, final String value) throws Exception { execute(new Command(){ public
//binary_tree.h file typedef struct node node; struct node { node():left(0), right(0), value(-1){}; ~node(){if(left) delete
static inline void *__memset(void *s, char c, size_t n) { int d0, d1; asm
if I do: void foo() { if( .. ) { inline int baa(..) {
public: inline int GetValue() const { return m_nValue; } inline void SetValue(int nNewValue) {
template<int size> inline void* byteswap(void* __x); template<> inline void* byteswap<2>(void* __x) { return (*(uint16*)__x
I have a fnc: template<class T, T constraint> inline void CheckSize(const T& value) {
Minimal code: // --------inline.h-------- struct X { static inline void foo (); }; #ifdef
Does this function has the same behavior that memset ? inline void SetZeroArray( void
How do I declare the second parameter as optional? template <typename T> inline void

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.