【发布时间】:2017-05-11 02:51:27
【问题描述】:
我想使用后序遍历删除二叉树。这意味着应该首先删除树的左侧部分,然后删除右侧的然后在随后的第二个函数中删除整个树并释放内存。我不能改变函数的参数,只能在里面玩:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "telefonbuch.h"
static inline bstree * create_node(unsigned long phone, char * name)
{
bstree * newNode = (bstree *) malloc(sizeof(bstree));
newNode->key.phone = phone;
strcpy(newNode->key.name, name);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void bst_insert_node(bstree * bst, unsigned long phone, char * name)
{
if (bst == NULL)
{
return;
}
if (bst->key.phone > phone)
{
if (bst->left == NULL)
{
bst->left = create_node(phone, name);
return;
}
bst_insert_node(bst->left, phone, name);
}
else
{
if (bst->right == NULL)
{
bst->right = create_node(phone, name);
return;
}
bst_insert_node(bst->right, phone, name);
}
}
bst_node * find_node(bstree* bst, unsigned long phone) {
if (bst == NULL)
{
return NULL;
}
if(bst->key.phone > phone)
{
return find_node(bst->left, phone);
}
else if (bst->key.phone < phone)
{
return find_node(bst->right, phone);
}
return &(bst->key);
}
void bst_in_order_walk_node(bst_node* node) {
}
void bst_in_order_walk(bstree* bst) {
int temp = 0;
int level;
while(temp < level)
{
printf("-");
++temp;
}
printf(" (%ld-%s)\n", bst->key.phone, bst->key.name);
if (bst->left != NULL)
{
print_tree(bst->left, level + 1);
}
if (bst->right != NULL)
{
print_tree(bst->right, level + 1);
}
}
void bst_free_subtree(bst_node* node) {
…what goes here?…
}
void bst_free_tree(bstree* bst) {
if(bst==NULL)
return;
bst_free_tree(bst->left);
printf("Deleting %d node.\n",bst->key);
free(bst);
bst_free_tree(bst->right);
}
以下是结构定义:
typedef struct _bst_node {
char name[60];
unsigned long phone;
} bst_node;
typedef struct _bstree {
bst_node key;
struct _bstree * left;
struct _bstree * right;
} bstree;
您能帮我完成/更正我的代码吗?
【问题讨论】:
-
我不明白
bst_free_subtree应该做什么。它的参数是一个节点,所以你不能用它释放整个子树。顺便说一句,您的函数bst_free_tree部分错误,因为在释放bst之后使用了bst->right。您必须在通话后 移动声明free(bst)。 -
嘿 Ahmed,我也遇到了麻烦,你确定 free_subtree 的签名有效吗?您能否发布所有功能的所有签名?我可以为你解决这个问题:)
-
在我看来,
bst_free_tree独自完成了整个工作。你需要bst_free_subtree做什么? (如果bstree结构的定义包含bst_node *key;而不是bst_node key;,情况会有所不同。) -
我发布了整个程序:)
标签: c algorithm recursion tree binary-search-tree