0
votes

I have a binary tree that looks like this

struct Node 
{
  int key;
  double data;
  Node* right;
  Node* left;
};

and I have this "insert" function for inserting new nodes and building the tree

void insert(Node*& p, int key, double to_be_inserted) 
{
  if (p == nullptr) 
  {
    p = new Node;
    p->key = key;
    p->data = to_be_inserted;
    p->left = nullptr;
    p->right = nullptr;
  }
  else 
  {
    if (p->key == key) 
    {
      p->data = to_be_inserted;
    }
    else 
    {
      Node*& child = (p->key > key) ? p->left : p->right;
      insert(child, key, to_be_inserted);
    }
  }
}

and a main function that looks like this

int main(int argc, char ** argv) 
{
  Node* root = nullptr;
  insert(root, 11, 11);
  insert(root, 6, 6);
  insert(root, 4, 4);
  insert(root, 5, 5);
  insert(root, 8, 8);
  insert(root, 10, 10);
  insert(root, 19, 19);
  insert(root, 17, 17);
  insert(root, 43, 43);
  insert(root, 31, 31);
  insert(root, 49, 49);

  printTree(root, 0);
  return 0;
}

The final "printed-out" tree looks like this

enter image description here

(This "print-out" is meant to be read from left to right instead of top to bottom)

My questions are:

  1. How does the insert function know what the tree currently looks like when it's being called a second (or more) time? root in main is just a single node (with two children) that is not changing(according to my debugger) and you're using the same root as argument multiple times. How does it know where it left off when insert is called again? What is the p = new Node line actually doing (in insert). Seems to me that it is just overwriting root, over and over again? Basically, where does it store the memory of what the (current) full tree looks like?

    1. Would the insert function behave differently if it was declared as Node * insert(Node * p, int key, double value);?

    2. Is there a specific reason why p is a pointer reference instead of a normal pointer? What's the difference?

Sorry, for the long (and possibly stupid) questions. I am new to C++. I understand the basics of pointers and references but apparently I can't really seem to figure out what is actually happening in the insert function (and in the main function, when insert is being called multiple times with the SAME root parameter).

Thanks!

1

1 Answers

3
votes

How does the insert function know what the tree currently looks like when it's being called a second (or more) time?

Because the root node retains memory of its two children, and its children retain memory of each of their children, and...

The insert function starts at a node and in this block

    else 
    {
      Node*& child = (p->key > key) ? p->left : p->right;
      insert(child, key, to_be_inserted);
    }

Determines which branch should be "drilled down into" to find an empty node to insert into. Each insert of a value at a node traverses the tree until it find the proper insert location.

Would the insert function behave differently if it was declared as Node * insert(Node * p, int key, double value);?

Yes, if it was declared as above in your question, the value of p which is of type Node * would have been copied from main into the function insert as a local variable. Any changes to p in insert will be local and won't affect the value of root in main. When you pass by reference, any changes made to p in insert will affect root in main. If you don't pass by reference, root will always be a nullptr, and insert will only execute this branch:

  if (p == nullptr) 
  {
    p = new Node;
    p->key = key;
    p->data = to_be_inserted;
    p->left = nullptr;
    p->right = nullptr;
  }

which will leak memory.

Is there a specific reason why p is a pointer reference instead of a normal pointer? What's the difference?

See above