【发布时间】:2011-07-25 14:01:08
【问题描述】:
好的,所以我有一个仅使用 C 结构和指针构建的二叉搜索树,因为我疯了,不想使用 C++。无论如何,我有一些严重的内存泄漏,因为我假设 free(tree),树是下面结构的一个实例,并没有释放所有的孩子那棵树。
这是我的节点:
struct node{
struct node* parent;
struct node* left;
struct node* right;
int key; //the value of the node
};
这是我的bst:
struct bst{
struct node* root;
int elements; //number of nodes in the bst
};
所以我的问题是,有没有比递归调用删除函数更好的方法呢?例如(当场写这个):
void delete_tree(struct node* n){
if(n == NULL) return;
struct node* left = n->left;
struct node* right = n->right;
free(n);
delete_tree(left);
delete_tree(right);
}
【问题讨论】:
标签: c data-structures memory-management struct free