1

I wrote a function to insert nodes into a binary search tree. However, when trying to build a solution in Visual Studio 2013, I receive this: "Unhandled exception at 0x00FD4CD0 in BST.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC." The following is my code.

void BST::insert(int value) {
    Node* temp = new Node();
    temp->data = value;
    if(root == NULL) {
        root = temp;
        return;
    }
    Node* current;
    current = root;
    Node* parent;
    parent = root;
    current = (temp->data < current->data) ? (current->leftChild) : (current->rightChild);
    while(current != NULL) {
        parent = current;
        current = (temp->data < current->data) ? (current->leftChild) : (current->rightChild);
    }
    if(temp->data < parent->data) {
        parent->leftChild = temp;
    }
    if(temp->data > parent->data) {
        parent->rightChild = temp;
    }
}

Then in my main function I have:

int main() {
    BST bst;
    bst.insert(10);
    system("pause");
}

When I remove bst.insert(10); in my main function, I no longer receive the unhandled exception.

The following is the initialization of my struct

struct Node {
    int data;
    Node* leftChild;
    Node* rightChild;
    Node() : leftChild(NULL), rightChild(NULL) {} 
};
struct BST {
    Node* root;
    void insert(int value);
    BST() : root(NULL) {}
};
Suede
  • 416
  • 2
  • 5
  • 16

1 Answers1

1

In your insertion function you are not setting leftChild and rightChild to NULL.

void BST::insert(int value) {
    Node* temp = new Node();
    temp->data = value;
    temp->leftChild = NULL;
    temp->rightChild = NULL;
    if(root == NULL) {
        root = temp;
        return;
    }

Also, I cannot be sure (as you have not posted the constructor for BST) but you may not have set root to NULL in the BST constructor. Try with these modifications.

Seems like you do not have a constructor in BST from what you have posted:

struct BST {
    Node* root;
    void insert(int value);
    BST(): root(NULL) { } // add a constructor to initialize root to NULL
};
digital_revenant
  • 3,274
  • 1
  • 15
  • 24
  • In my constructor BST (I edited that in the bottom) I set leftChild and rightChild to NULL, but not root. Would I do that in the BST constructor or the Node Constructor? – Suede Oct 23 '13 at 22:41
  • I edited it in my original post and it compiles, so was that the only reason for the 'unhandled exception'? – Suede Oct 23 '13 at 22:51
  • You were comparing root to NULL in `if(root == NULL)` to see if the BST is empty but you never set `root` to `NULL` in the first place. So `root` had a random garbage address and the `if(root == NULL)` check failed and execution went to the part of code below `if`. So, lot of memory access violations there. – digital_revenant Oct 23 '13 at 22:53
  • Ok great, thanks. But what exactly does the error mean? When it tells me there is an unhandled exception, is it giving the address of what root was ? – Suede Oct 23 '13 at 22:59
  • It tells you that the program is trying to access a memory address that is not allowed by the operating system. Read [this](http://en.wikipedia.org/wiki/Segmentation_fault) – digital_revenant Oct 23 '13 at 23:01