C++ 插入到二叉搜索树中

C++ 插入到二叉搜索树中,c++,exception,binary-search-tree,unhandled,C++,Exception,Binary Search Tree,Unhandled,我编写了一个函数,将节点插入到二叉搜索树中。但是,当尝试在Visual Studio 2013中构建解决方案时,我收到以下消息:“BST.exe中0x00FD4CD0处未处理的异常:0xC0000005:访问冲突读取位置0xCCCC。”下面是我的代码 void BST::insert(int value) { Node* temp = new Node(); temp->data = value; if(root == NULL) { root =

我编写了一个函数,将节点插入到二叉搜索树中。但是,当尝试在Visual Studio 2013中构建解决方案时,我收到以下消息:“BST.exe中0x00FD4CD0处未处理的异常:0xC0000005:访问冲突读取位置0xCCCC。”下面是我的代码

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;
    }
}
当我移除bst.insert(10)时;在我的主函数中,我不再接收未处理的异常

下面是我的结构的初始化

struct Node {
    int data;
    Node* leftChild;
    Node* rightChild;
    Node() : leftChild(NULL), rightChild(NULL) {} 
};
struct BST {
    Node* root;
    void insert(int value);
    BST() : root(NULL) {}
};

在插入函数中,您没有将
leftChild
righchild
设置为空

void BST::insert(int value) {
    Node* temp = new Node();
    temp->data = value;
    temp->leftChild = NULL;
    temp->rightChild = NULL;
    if(root == NULL) {
        root = temp;
        return;
    }
另外,我不能确定(因为您没有发布
BST
的构造函数),但是您可能没有在
BST
构造函数中将root设置为NULL。尝试这些修改

从您发布的内容来看,BST中似乎没有构造函数:

struct BST {
    Node* root;
    void insert(int value);
    BST(): root(NULL) { } // add a constructor to initialize root to NULL
};

在插入函数中,您没有将
leftChild
righchild
设置为空

void BST::insert(int value) {
    Node* temp = new Node();
    temp->data = value;
    temp->leftChild = NULL;
    temp->rightChild = NULL;
    if(root == NULL) {
        root = temp;
        return;
    }
另外,我不能确定(因为您没有发布
BST
的构造函数),但是您可能没有在
BST
构造函数中将root设置为NULL。尝试这些修改

从您发布的内容来看,BST中似乎没有构造函数:

struct BST {
    Node* root;
    void insert(int value);
    BST(): root(NULL) { } // add a constructor to initialize root to NULL
};

在底部发布您的
节点
类编辑在底部发布您的
节点
类编辑在底部在我的构造函数BST中(我在底部编辑了它)我将leftChild和rightChild设置为NULL,但不设置为root。我会在BST构造函数或节点构造函数中这样做吗?我在我的原始帖子中编辑了它并进行了编译,这也是“未处理异常”的唯一原因吗?您在
if(root==NULL)
中比较了root和NULL,以查看BST是否为空,但您从未将
root
设置为
NULL
。因此
root
有一个随机垃圾地址,并且
if(root==NULL)
检查失败,执行转到
if
下面的代码部分。所以,这里有很多内存访问冲突。很好,谢谢。但这个错误到底意味着什么呢?当它告诉我有一个未处理的异常时,它是否给出了root所在的地址?它告诉您程序正在尝试访问操作系统不允许的内存地址。阅读我的构造函数BST(我在底部编辑了它),我将leftChild和rightChild设置为NULL,但不是root。我会在BST构造函数或节点构造函数中这样做吗?我在我的原始帖子中编辑了它并进行了编译,这也是“未处理异常”的唯一原因吗?您在
if(root==NULL)
中比较了root和NULL,以查看BST是否为空,但您从未将
root
设置为
NULL
。因此
root
有一个随机垃圾地址,并且
if(root==NULL)
检查失败,执行转到
if
下面的代码部分。所以,这里有很多内存访问冲突。很好,谢谢。但这个错误到底意味着什么呢?当它告诉我有一个未处理的异常时,它是否给出了root所在的地址?它告诉您程序正在尝试访问操作系统不允许的内存地址。阅读