-1

I can't add nodes like in a binary search tree. It seems the add function is the problem because it don't actually change the root node or any other nodes. What can I do?

#include <iostream>
#include <string.h>
using namespace std;

typedef struct Node{
    int data;
    Node* left=NULL;
    Node* right=NULL;
}Node;

Node* root=NULL;

void add(Node* r, Node* node){
    if(r==NULL){
        r=node;
        return;
    }
    if(r->data > node->data){
        add(r->left, node);
        return;
    }
    add(r-> right, node);


}

int main() { 
    Node* r=new Node;
    r->data=5;
    add(root,r);

    cout<<root->data;

    return 0;
 }
  • 1
    A few unrelated notes, `NULL` is a C-compatible macro for null pointers, use `nullptr` (or plain `0`); Then `struct` is the same as `class` (basically) and both introduce the name of the structure (or class) as a type. You don't need to `typedef` aliases for structures. And the `` header file is the C compatible file for null-terminated arrays of characters. For the C++ `std::string` class (which you don't use) include ``. And [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) is a bad habit. – Some programmer dude May 16 '22 at 10:58
  • I also recommend you take some time to read more about classes and objects and how to use them. For a tree I would recommend to create a `Tree` class to handle all tree-related operations. Inside the `Tree` class you can create a private `Node` class to make up the nodes of the tree. And you don't need `new` to create objects in C++. – Some programmer dude May 16 '22 at 11:00
  • 1
    As for your problem: Arguments to functions are by default passed *by value*. That means that the value used in the call is *copied* into the functions local argument variable. Whatever changes you make to the variable, like assigning to it, will be done on the local variable only, which contains the copy of the original value. Therefore your assignment `r=node` in the `add` function is pointless, as the change will be lost once the function returns. Please open your text-book and read about *references* and how to pass arguments by reference. – Some programmer dude May 16 '22 at 11:02
  • You have some misunderstanding about pointers. In your `add` function it seem you expect that `r=node;` would assign object to your `root` variable, but, in fact, you are simply changing the value (address) of the `r` parameter. This as no effect. – 440 May 16 '22 at 11:03
  • If you want a function to modify a thing, you need to pass it a pointer (or reference) *to that thing*. There is nothing special about pointers. (Analogy: if I hand you a note with my home address on it, then no matter what you write on that note, my home is still in the same place.) – molbdnilo May 16 '22 at 11:11
  • 1
    Or have the function return the thing (a pointer to the root of the tree in your case). This might be easier to understand than `Node **` for a beginner. You would then write `root = add(root, r);` to add that node. – Goswin von Brederlow May 16 '22 at 11:17
  • @Goswin von Brederlow it works, thanks. – Stănescu Cristian May 16 '22 at 19:01

0 Answers0