删除整个二叉树的内存

huangapple go评论70阅读模式
英文:

Delete whole binary tree from memory

问题

我正在开发一个测试一些函数并计算它们在二叉搜索树上的执行时间的程序。为了进行这些测试,我将这些函数放入了一个循环中,每次循环都增加插入操作次数。问题是,由于树占用内存,我必须释放这些内存,因此我编写了一个用于释放内存的函数,但当我使用它时,结果与不使用它时相同;RAM使用量显著增加。
以下是该函数的代码:

void deleteTree(treeNode* x){
    if(x){
        deleteTree(x->left);
        deleteTree(x->right);
        free(x->left);
        free(x->right);
    }
}

为了创建树的节点,我使用以下结构体:

typedef struct _treeNode{
    int key;
    struct _treeNode* left;
    struct _treeNode* right;
    struct _treeNode* parent;
}treeNode;

以及以下函数:

treeNode* createTreeNode(int key){
    treeNode* a = (treeNode*)malloc(sizeof(struct _treeNode));
    a->key = key;
    a->left = NULL;
    a->right = NULL;
    a->parent = NULL;
    return a;
}

希望这能帮助你解决问题。

英文:

I am working on a program that tests some functions and counts their elapsed time on binary search trees and to do these tests I've put these functions into a loop increasing the insertions for every circle. The problem is that since the trees take up memory , I have to free that memory so I've wroten a function to do it, but when I use it the resaults are the same as if I didn't; the RAM usage increases significantly.
Here is the function:

void deleteTree(treeNode* x){
    if(x){
        deleteTree(x->left);
        deleteTree(x->right);
        free(x->left);
        free(x->right);
    }
}

To create the nodes for the tree I use this structure:

typedef struct _treeNode{
    int key;
    struct _treeNode* left;
    struct _treeNode* right;
    struct _treeNode* parent;
}treeNode;

and this function:

treeNode* createTreeNode(int key){
    treeNode* a = (treeNode*)malloc(sizeof(struct _treeNode));
    a->key = key;
    a->left = NULL;
    a->right = NULL;
    a->parent = NULL;
    return a;
}

答案1

得分: 2

你的操作位于需要的级别之外。尝试:

void deleteTree(treeNode* x){
    if(x){
        deleteTree(x->left);
        deleteTree(x->right);
        free(x);
    }
}
英文:

You are operating one level off where you need to be. Try:

void deleteTree(treeNode* x){
    if(x){
        deleteTree(x->left);
        deleteTree(x->right);
        free(x);
    }
}

答案2

得分: 0

free() 用于释放由 malloc() 或其替代函数分配的内存;已分配的内存块被释放以供下一次内存分配调用使用,但它不会清除内存。它只是将内存“释放”,以便将其留待进一步使用,而释放后内存的状态取决于具体实现。

此外,如果尝试访问已释放的内存,可能会得到正确的数据,但这种行为是未定义的,通常但不总是会导致 Seg Fault(段错误)。

前述理由解释了释放操作对执行时间几乎没有影响的原因。至于RAM使用增加,这可以解释为操作系统尝试合并几个已释放的内存块。

英文:

free () is used for freeing memory allocated by malloc () or alternatives; the chunk of allocated memory block is released to be used by next call of memory allocation but it doesn't clears memory. It just 'releases' it as in leaves it for further use and whatever happened to memory after freeing it, is implementation dependent.

Moreover, If you'd try to access freed memory you might get correct data but this behavior is undefined and often but not always results in Seg Fault.

Aforementioned reasoning explains little to no difference in execution time caused by free. As far as increase in RAM usage is concerned, this can be explained by OS's attempt at clubbing several 'free-d' chunks of memory.

huangapple
  • 本文由 发表于 2020年1月4日 01:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定