简体   繁体   中英

c++ Struct pointer unable to read memory when initialised as NULL

I'm creating a program that incorporates Binary search tree algorithm but I've run into a problem that I'm not sure how to solve. Heres the relevant bits of my code.

struct node {
    string data;
    node *left = NULL;
    node *right = NULL;
};

Thats my struct for the node.

void Insert_Rec(string word, node* ptr) {

if (ptr->data == "") {

    ptr->data = word;
    ptr->left = NULL;
    ptr->right = NULL;
    cout << "overwitten!" << endl;
}
else if (word < ptr->data) {
    if (ptr->left != NULL) {
        cout << "Recursing left!";
        Insert_Rec(word, ptr->left);
    }
    else {
        ptr->data = word;
        ptr->left = NULL;
        ptr->right = NULL;
        cout << "Inserted!";
    }
}

And the problem lies there, the program never enters if(ptr->left != NULL) statement. Looking at my visual studio debugger, the ptr->left shows "" instead of NULL. How can i solve this!? I've tried some of the other solutions here but they are either not relevant or just dont work!!

program never enters if(ptr->left != NULL) statement

well ptr->left starts out NULL, and you never assign anything else to it, so it will stay NULL forever.

if (ptr->left) {
    cout << "Recursing left!";
    Insert_Rec(word, ptr->left);
}
else {
    /* this just overwrites the existing node in-place
       but you should be creating a new node for the left child
    ptr->data = word;
    ptr->left = NULL;
    ptr->right = NULL;
    */
    ptr->left = new node{word, nullptr, nullptr};
    cout << "Inserted!";
}

There are a number of other issues with your code (Vlad-from-Moscow's answer shows a better design for this function, but you really need to fix it at the container class level), but this is the immediate blocker.

Your function implementation does not make sense in whole.

The function can be defined the following way

void Insert_Rec( node **head, const std::string &word ) 
{
    if ( *head == nullptr )
    {
        *head = new node { word, nullptr, nullptr };
    }
    else if ( word < ( *head )->data )
    {
        Insert_Rec( &( *head )->left, word );
    }
    else
    {
        Insert_Rec( &( *head )->right, word );
    }
}  

It is the function that should allocate a new node if it is required.

If you want that the BST would not contain duplicates then change the last else statement to else if statement like

else if ( ( *head )->data < word )

The function can be called the following way

node *head = nullptr;
//...
Insert_Rec( &head, "Hello" );

Also instead of the "double" pointer you can use a referenced type as the type of the first function parameter.

For example

void Insert_Rec( node * &head, const std::string &word ) 
{
    if ( head == nullptr )
    {
        head = new node { word, nullptr, nullptr };
    }
    else if ( word < head->data )
    {
        Insert_Rec( head->left, word );
    }
    else
    {
        Insert_Rec( head->right, word );
    }
} 

You've set node *left=NULL and node *right=NULL in struct. Remove that NULL. Leave only two pointers. Otherwise, it's just null. Anyway, keep in mind that very rarely you want to initialise the pointers in a struct.

struct node {
    // simple is better (-:
    string data;
    struct node *left;
    struct node *right;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM