-1

My program crashes when I try to add a new node to binary search tree.

Crash message: Access violation reading location.

The line in xstring: return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));

The Code:

main:

BSNode* bs = new BSNode("6");

bs->insert("2");
bs->insert("8");

The function where it crashes:

if (value.compare(_data) > 0)
{
    if (this->isLeaf())
    {
        _right = new BSNode(value);
    }
    else
    {
        _right->insert(value);
    }
}
else if (value.compare(_data) < 0)
{
    if (this->isLeaf())
    {
        _left = new BSNode(value);
    }
    else
    {
        _left->insert(value);
    }
}

It crashes at line 1, "if (value.compare(_data) > 0)".

More Code:

BSNode::BSNode(string value)
{
    _data = value;
    _right = NULL;
    _left = NULL;

}

Thanks, Omer.

R Nar
  • 5,465
  • 1
  • 16
  • 32
Omer Paz
  • 21
  • 8
  • 2
    I expect _data is null or not initialized. – drescherjm Nov 23 '15 at 16:06
  • Please provide [MCVE](http://stackoverflow.com/help/mcve). – MikeCAT Nov 23 '15 at 16:06
  • ***Access violation reading location.*** After supplying a MCVE the exact error may help. – drescherjm Nov 23 '15 at 16:07
  • Can you show us BSNode implementation, compare function in particular? – Yuriy Orlov Nov 23 '15 at 16:08
  • I've added the constructor, compare function in particular because i want to know if i have to turn right or left. – Omer Paz Nov 23 '15 at 16:11
  • perhaps your data structure is uninitialized or the function compare is wrong implemented. – Luis Daniel Nov 23 '15 at 16:13
  • The compare function is the original compare function from . – Omer Paz Nov 23 '15 at 16:15
  • 1
    Off topic: Rather than doing `value.compare(_data)` twice, try `int result = value.compare(_data);` and then `if (result > 0)` and `else if (result < 0)` – user4581301 Nov 23 '15 at 16:18
  • 1
    Not seeing any howlers in what is posted. Recommend a step-through with a debugger because `value` is bad, `_data` is bad, or the object into which `value` is being inserted is bad. No way to tell from the above snippets. – user4581301 Nov 23 '15 at 16:23
  • Underscore prefix may be a problem. `_data`, `_right` or `_left` could be colliding with an already defined term. Read more about underscore prefixes: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – user4581301 Nov 23 '15 at 16:28
  • The problem was in function isLeaf(), thank's for your help. – Omer Paz Nov 23 '15 at 16:46
  • @OmerPaz I had implemented this as an example, if you want I can send you my code – Luis Daniel Nov 23 '15 at 16:50

1 Answers1

0

It works fine...

#include "BSNode.h"

BSNode::BSNode(string value)
{
    _data = value;
    _right = NULL;
    _left = NULL;
}

void BSNode::insert(string value) 
{
    if (value.compare(_data) < 0)
    {
        if (_left->is_empty())
        {
            _left = new BSNode(value);
        } else {
            _left->insert(value);
        }
    } else

    if (value.compare(_data) > 0)
    {
        if (_right->is_empty())
        {
            _right = new BSNode(value);
        } else {
            _right->insert(value);
        }
    }
}

bool BSNode::is_empty()
{
    return (this == NULL);
}

bool BSNode::is_leaf()
{
    return (!this->is_empty() && _left->is_empty() && _right->is_empty());
}

void BSNode::print_bfs() 
{
    queue<BSNode*> queue;
    queue.push(this);

    BSNode * tmp;
    while (!queue.empty())
    {
        tmp = queue.front();
        queue.pop();

        if (tmp->is_empty())
            continue;

        cout << tmp->_data << ", ";

        queue.push(tmp->_left);
        queue.push(tmp->_right);
    }

    cout << endl;
}
Luis Daniel
  • 687
  • 7
  • 18