so here is a binary tree example from alex allains book I'm still not too sure really how it works but anyway the main question I wanted to ask is with the first if block which tests to see if the node pointer == NULL and if it does it will do something,
but first of all how would the nodepointer which we are passing in first of all even = NULL? would we need to set it to NULL before we passed it in?
also could anybody help me out and show me how a main function(with explanations to this code would look as it was never given in the book and it's confusing me =( thanks guys I know this is probably a harder question to answer but I really appreciate you guys taking the time
also could anybody help me out and show me how a main function(with explanations to this code would look as it was never given in the book and it's confusing me
but first of all how would the nodepointer which we are passing in first of all even = NULL? would we need to set it to NULL before we passed it in?
Yes, you would need to set the pointer to nullptr, otherwise it could contain any value because it would be uninitialized.
also could anybody help me out and show me how a main function(with explanations to this code would look as it was never given in the book and it's confusing me
Have you considered finding a better book? The code shown looks more like C code than good C++ code.
I don't really know how showing a main() for the posted code would help. The code posted is very incomplete.
You are supposed to pass a pointer to the root node of the tree together with the value that you want to insert. The function then returns a pointer to the new root of the tree so that you can use to update your pointer.
When the tree is empty there is no root node so in that case you just pass a null pointer.
1 2 3 4 5 6 7
// Pointer to the root node of the tree.
node* tree = nullptr;
// Insert the values 45, 10 and 27 into the tree.
tree = insert(tree, 45);
tree = insert(tree, 10);
tree = insert(tree, 27);
Thanks peter and yeah it's Allex Allains jumping into c++ It's not great in my opinion he does not explain things in a lot of details do you reccomend any books with more advanced topics like this for beginners or books just for beginners?
one follow up question on the second time around how would the tree not = NULL or nullptr? I thought we are just changing the key value to a value not the actual node pointer tree so wouldn't tree always = nullptr?
No, when you insert an element you are returning a pointer which you would assign to "tree", see Peter87's post above. The only time tree will be a nullptr would be when the tree is empty.