C ++ / cygwin_exception :: open_stackdumpfile:将堆栈跟踪转储到* .exe.stackdump

时间:2015-12-14 00:45:19

标签: c++ c++11 cygwin

我得到了cygwin_exception :: open_stackdumpfile:尝试运行我的代码时,将堆栈跟踪转储到* .exe.stackdump错误。它是BST树的简单实现,当我执行delete函数时会出现错误。 这是我的代码:

#include <iostream>
#include <cstddef>

using namespace std;

/*
 * BST Trees
 */

struct BST_node {
    int key;
    BST_node *left, *right;
};

BST_node * BST_search(BST_node *node, int key) {
    //search node with given key
    if (node == NULL)
        return NULL;
    if (node->key == key)
        return node;
    if (node->key > key)
        return BST_search(node->left, key);
    else
        return BST_search(node->right, key);
}

bool key_in_tree(BST_node *root, int key) {
    return BST_search(root, key) != NULL;
}

BST_node * BST_insert(BST_node *node, int key) {
    if (!key_in_tree(node, key)) {
        if (node == NULL) {
            BST_node *tmp = new BST_node;
            tmp->key = key;
            tmp->left = tmp->right = NULL;
            return tmp;
        }

        if (key < node->key)
            node->left = BST_insert(node->left, key);
        else
            node->right = BST_insert(node->right, key);
        return node;
    }
}

BST_node * BST_find_parent(BST_node * node, BST_node * root) {
    //find node's parent
    if (root->left == node || root->right == node) {
        return root;
    } else {
        if (root->left != NULL)
            return BST_find_parent(node, root->left);
        if (root->right != NULL)
            return BST_find_parent(node, root->right);
    }

}

BST_node * BST_find_max(BST_node * node) {
    if (node->right != NULL)
        return BST_find_max(node->right);
    else
        return node;
}

void BST_delete(BST_node * node, int key) {
    //delete node with given key, node - root of the tree
    BST_node * node_to_remove = BST_search(node, key);
    BST_node * parent = BST_find_parent(node_to_remove, node);

    if (node_to_remove != NULL) {

        //if node doesn't have children - delete
        if (node_to_remove->left == NULL && node_to_remove->right == NULL) {
            //set null in parent
            if (parent->left == node_to_remove)
                parent->left = NULL;
            else
                parent->right = NULL;
        }

        //node to remove has one child 
        if (node_to_remove->left == NULL && node_to_remove->right != NULL) {
            if (parent->left == node_to_remove)
                parent->left = node_to_remove->right;
            else
                parent->right = node_to_remove->right;
        }

        if (node_to_remove->left != NULL && node_to_remove->right == NULL) {
            if (parent->left == node_to_remove){
                parent->left = node_to_remove->left;
            }
            else{
                parent->right = node_to_remove->left;
            }
        }

        //replace with biggest node in left subtree
        if (node_to_remove->left != NULL && node_to_remove->right != NULL) {
            BST_node * node_to_change = BST_find_max(node_to_remove->left);
            int tmp = node_to_change->key;
            BST_delete(node, tmp);
            node_to_remove->key = tmp;
        }
    }
}

int main(int argc, char** argv) {
    BST_node *root = NULL;
    root = BST_insert(root, 10);
    root = BST_insert(root, 8);
    root = BST_insert(root, 9);
    root = BST_insert(root, 5);
    root = BST_insert(root, 7);
    root = BST_insert(root, 100);
    root = BST_insert(root, 87);
    BST_delete(root, 100);
    return 0;
}

我得到的错误如下: 27 [main] bst 4828 cygwin_exception :: open_stackdumpfile:将堆栈跟踪转储到bst.exe.stackdump

0 个答案:

没有答案