请高手帮忙关于binary search tree
我想问问怎么FREE 一个BST......谢谢..下面是我写的.大家看看对不.有没有别的写法struct bstnode {
char *key;
bst left;
bst right;
};
bst bst_new() {
bst b = NULL;
return b;
}
bst bst_insert(bst b, char *key) {
if(b == NULL){
b = emalloc(sizeof *b);
b->key = emalloc((strlen(key)+1) * sizeof b->key[0]);
strcpy(b->key,key);
} else if (strcmp(b->key,key)>0){
b->left = bst_insert(b->left,key);
} else if (strcmp(b->key,key)<0){
b->right = bst_insert(b->right,key);
}
return b;
}
....
....
....
....
bst bst_destroy(bst b){
if ( b == NULL) return;
bst_destroy(b->left);
bst_destroy(b->right);
free(b->key);
free(b);
b = NULL;
return b;
}