【发布时间】:2019-08-31 18:37:52
【问题描述】:
已经用tsearch 填充了一个 POSIX 二叉树,如何清理整个树? GCC 提供了tdestroy 作为扩展,但是如果你想使用 POSIX-only 函数怎么做呢?
我当前的实现使用twalk 遍历树,对于endorder 和leaf 节点,调用tdelete,但可以理解的是,这显示了关于const-correctness 的警告:
static void free_tree(const void *node, const VISIT which, const int depth)
{
struct search_entry *entry;
switch (which) {
case endorder:
case leaf:
entry = *(struct search_entry **)node;
tdelete(entry->key, &node, search_entry_compare);
free(entry);
}
}
对于符合 POSIX 的应用程序的预期方法是什么?
【问题讨论】: