当树中不可用的节点被删除时,会出现问题。当树中可用的整数被删除时,它会很有效。非常感谢。
int delNode(struct node* ptr,int item)
{ struct node* y;
if(!ptr)
return 0;
if(item==ptr->left->data){
y=ptr->left;
ptr->left=NULL;
free(y);}
else if(item==ptr->right->data){
y=ptr->right;
ptr->right=NULL;
free(y);}
else if(item>ptr->data)
delNode(ptr->right,item);
else
delNode(ptr->left,item);
}
void main()
{
struct node* treeNode;
treeNode=NULL;
addItem(&treeNode,5);
addItem(&treeNode,3);
addItem(&treeNode,2);
addItem(&treeNode,4);
addItem(&treeNode,8);
addItem(&treeNode,7);
addItem(&treeNode,9);
preorder(treeNode);
search(treeNode,9)?printf("Found"):printf("Not Found");
delNode(treeNode,9);
preorder(treeNode);
//printf("Depth:%i",depth(treeNode));
}