I get the algorithm and how binary trees should work but this I don't get :

look at the basic binary tree insert function :

``````node* insert_in_tree(int number,node* p_tree) {

if(p_tree == 0) {

node* newNode = new node;
node->num = number;
node->p_left = 0;
node->p_right = 0;
return newNode;

}

if(number < p_tree->num) {

p_tree->p_left = insert_in_tree(number,p_tree->p_left);

}

else {

p_tree->p_right = insert_in_tree(number,p_tree->p_right);

}

return p_tree;

so from main :

int main()

int main()
{
node* tree_node = 0;
cin >> num;
insert_in_tree(num,tree_node);

}
``````

I get the first call to the function, tree is equal to 0, it creates a new node and so on, but how does tree changes it value so its not 0 anymore. I just can't see it in code?

1
Contributor
1
13
Views
4 Years
Discussion Span
Last Post by nhrnjic6

Think I got it, just a Q :

is this how I call it in main :

``````  while(num != 0) {cin >> num; tree_node = insert(num,tree_node);}
``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.