temp = new_item;
doesn't do what you think it does. All it does is to reassign the local variable temp
to something else. It doesn't change the pointer you copied it from.
Think of Node
as a person and each hand of that person (which can point to another person) being the left
and right
pointers respectively. Now your tree is a whole network of people pointing to each other. Then temp
is someone else' hand, repeatedly pointing to different people, until he/she ends up pointing at nothing. Then you tell him/her to point to someone else (new_item
), which obviously won't change your network of people pointing at each other, since temp
isn't part of this network.
The simplest fix I can think of is to make temp
a Node**
:
Node** temp = &root;
// your code to traverse the tree, except that you should
// replace `temp = ...` with `temp = &...`
// and every other occurrence of `temp` with `*temp`
*temp = new_item;
To extend the metaphor, temp
would then be pointing to a hand rather than a person, and changing *temp
would involve making that hand point to another person. Since that hand is part of the network, changing it would change the network.