C - 删除n-ary树节点

时间:2015-06-04 15:13:05

标签: c tree n-ary-tree

我已经在C中使用AI实现了m,n,k游戏。游戏运行正常,但是当我必须释放决策树时,它总是抛出一个"访问违规读取位置"例外。

这是决策树结构的实现:

typedef struct decision_tree_s {
    unsigned short **board;
    status_t status;
    struct decision_tree_s *brother;
    struct decision_tree_s *children;
} decision_tree_t;


这是delete_tree函数的实现:

void delete_tree(decision_tree_t **tree) {
    decision_tree_t *tmp;

    if (*tree != NULL) {
        delete_tree((*tree)->children);
        delete_tree((*tree)->brother);

        free(*tree);
        *tree = NULL;
    }
}

1 个答案:

答案 0 :(得分:0)

你正在摧毁children成员的两倍:第一次在for循环中,第二次在循环之后。

您可能想要编写for循环:

for (tmp = tree->brother; tmp != NULL; tmp = tmp->brother) {
    delete_tree(tmp);
}