0
votes

here is my code. after reading T the program reads the first input data and stops working. message "Unhandled exception at 0x00844D30 in tree.exe: 0xC0000005: Access violation reading location 0x00000000." is shown. I included iostream and stddef.h libraries.

class Node{
public:
    int data;
    Node *left, *right;
    Node(int d){
        data = d;
        left = right = NULL;
    }
};

class Solution{
public:
    Node* insert(Node* root, int data){
        if (root = NULL){
            return new Node(data);
        }
        else{
            Node* cur;
            if (data <= root->data){
                cur = insert(root->left, data);
                root->left = cur;
            }
            else{
                cur = insert(root->right, data);
                root->right = cur;
            }
            return root;
        }
    }

};

int main(){
    Solution myTree;
    Node* root = NULL;
    int T, data;
    cin >> T;
    while (T-->0){
        cin >> data;
        root = myTree.insert(root, data);
    }

    return 0;
}
2
can you reformat your code, it is unreadable.recycler
And what is the problem with the code? Please include the problem description in the question body (please read about how to ask good questions). Also, have you tried to debug the program? What does the debugger say? Where does it stop (if it does)?Some programmer dude
Node(int d) : data(a), left(nullptr), right(nullptr) {} would be betterEd Heal
Have you thought about using smart pointers?Ed Heal
if (root = NULL){ should be if (root == NULL){Ed Heal

2 Answers

1
votes

This if will always go into the else part. Because you assign NULL to the variable root and then check the value. It should be root == NULL

        if (root = NULL){
            return new Node(data);
        }
        else{
            Node* cur;
            if (data <= root->data){
                cur = insert(root->left, data);
                root->left = cur;
            }
            else{
                cur = insert(root->right, data);
                root->right = cur;
            }
            return root;
        }
1
votes

The condition should be

if (root==NULL)

    #include <iostream>
#include <stddef.h>
using namespace std;
class Node{
public:
    int data;
    Node *left, *right;
    Node(int d){
        data = d;
        left = right = NULL;
    }
};

class Solution{
public:
    Node* insert(Node* root, int data){
        if(root == NULL){
            return new Node(data);
        }
        else{
            Node* cur;
            if (data <= root->data){
                cur = insert(root->left, data);
                root->left = cur;
            }
            else{
                cur = insert(root->right, data);
                root->right = cur;
            }
            cur->left=cur->right=NULL;
            return root;
        }
    }

};

int main(){
    Solution myTree;
    Node* root = NULL;
    int T, data;
    cin>>T;
    while (T-->0){
        cin>>data;
        root = myTree.insert(root, data);
    }

    return 0;
}