Null objects?(can't think of better name)

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

kind regards,

adam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>
 
using namespace std;
 
struct node{
 
   int key_value;
   node *right;
   node *left;
 
 
};
 
node* insert(node* tree,int key){
 
 
  if(tree == NULL){
 
    node* newTree = new node;
    newTree->right = NULL;
    newTree->left = NULL;
    newTree->key_value = key;
    return newTree;
 
 
  }
 
  if(key < tree->key_value){
 
     tree->left = insert(tree->left,key);
 
  }
  else{
 
     tree->right = insert(tree->right,key);
 
  }
 
  return tree;
 
 
}
 
 
int main()
{
 
 
 
}
if(tree == NULL)
I suggest you use nullptr from now on.
anybody?
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);
Last edited on
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?
Here is a fairly exhaustive book list for C++:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

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.

Topic archived. No new replies allowed.