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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:42:45+00:00 2026-05-29T11:42:45+00:00

Below is the code example for the insert code of a node into a

  • 0

Below is the code example for the insert code of a node into a tree.
The example is taken from http://cslibrary.stanford.edu/110/BinaryTrees.html
The problem is this: I have a basic understanding of pointers and memory, and understand the whole thing but the insert node.
here’s the node struct:

struct node { 
  int data; 
  struct node* left; 
  struct node* right; 
}  

Now, in the page I provided, it says that this method of insertion is done to avoid the pass by reference. so instead of calling insert(struct node** nodeptr, int some data); it is called this way: nodeptr = insert(data int).
so my question is. I understand the part of pointer assignment, that the pointer returned by the insert function is placed into nodeptr. supposing that nodeptr is the root of the tree, how can it affect some node which will point to the new node.

struct node* insert(struct node* node, int data) 
{ 
  // 1. If the tree is empty, return a new, single node 
  if (node == NULL) 
  { 
     return(newNode(data)); 
  } 
  else 
  { 
     // 2. Otherwise, recur down the tree 
     if (data <= node->data) 
     {
        node->left = insert(node->left, data); 
     }
     else 
     {
        node->right = insert(node->right, data);
     }

     return(node); // return the (unchanged) node 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-05-29T11:42:46+00:00Added an answer on May 29, 2026 at 11:42 am

    It might help to walk through the code for a few inserts. So, assuming we’re calling insert from function foo, our call tree would look something like this:

     foo: root = NULL;
     foo: root = insert(root, 5);
    
         insert: if (node == NULL)
         insert: return newnode(data); // newnode = 0xff864000
    
     foo: root = 0xff864000
    

    After this first call to data, our tree looks something like this:

    Address       data        left            right
    -------       ----        ----            -----
    0xff864000       5        0x00000000      0x00000000
    

    Now we make a second call to insert a new value:

    foo: root = insert(root, 3);
    
        insert: if (node == NULL) // node == 0xff864000
        insert: if (data <= node->data)
        insert: node->left = insert(node->left, data);
    

    Now we call insert again; this time node is the left child of the current node (which should be NULL):

            insert(2): if (node == NULL) // node == 0x00000000
            insert(2): return newnode(data); // newnode == 0xff86400c
    
        insert: node->left = 0xff86400c;
        insert: return node; 
    
    foo: root = 0xff864000
    

    So the result of this second call to insert is assigned to the left child of the current node, and our tree now looks something like this:

    Address       data        left            right
    -------       ----        ----            -----
    0xff864000       5        0xff86400c      0x00000000
    0xff86400c       3        0x00000000      0x00000000
    

    Add another element:

    foo: root = insert(root, 2);
    
        insert: if (node == NULL)  // node = 0xff864000
        insert: if (data <= node->data)
        insert: node->left = insert(node->left, data);
    
            insert(2): if (node == NULL)  // node = 0xff86400c
            insert(2): if (data <= node->data)
            insert(2): node->left = insert(node->left, data);
    
                insert(3): if (node == NULL)
                insert(3): return newnode(data); // newnode = 0xff864018
    
            insert(2): node->left = 0xff864018
            insert(2): return node;
    
       insert: node->left = 0xff86400c
       insert: return node;
    
    foo: root = 0xff8640000
    

    And now our tree looks like

    Address       data        left            right
    -------       ----        ----            -----
    0xff864000       5        0xff86400c      0x00000000
    0xff86400c       3        0xff864018      0x00000000
    0xff864018       2        0x00000000      0x00000000
    

    And finally:

    foo: root = insert(root, 7);
    
        insert: if (node == NULL)  // node = 0xff864000
        insert: if (data <= node->data)
        insert: node->right = insert(node->right, data);
    
            insert(2): if (node == NULL)  // node = 0x0x00000000
            insert(2): return newnode(data); // newnode = 0xff864024
    
        insert: node->right = 0xff864024
        insert: return node;
    
    foo: root = 0xff8640000
    
    Address       data        left            right
    -------       ----        ----            -----
    0xff864000       5        0xff86400c      0xff864024
    0xff86400c       3        0xff864018      0x00000000
    0xff864018       2        0x00000000      0x00000000
    0xff864024       7        0x00000000      0x00000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the code below to insert content into a page. I need to
The example code below works as as a server process. But when I add
I know how to do this... I'll give example code below. But I can't
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
Given the example source code below, is it possible for someone to see the
I have a basic PHP question, take the code below for example, let's say
I constantly find myself writing similar code like the example below: if (object[Object Name]
Below is an example class hierarchy and code. What I'm looking for is a
For the example below: if a == 100: # Five lines of code elif

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.