我有一个二叉搜索树的插入功能,但我不知道为什么会出现该错误。即使我返回 FALSE;在函数结束之前它仍然会发生。任何帮助表示赞赏。
boolean insert(NODE **root, Employee e){
NODE *cursor,*temp;
// boolean status;
temp = (NODE *)malloc(sizeof(NODE));
assert(temp != NULL);
temp->element = (Employee *)malloc(sizeof(Employee));
assert(temp -> element != NULL);
*(temp -> element) = e;
temp -> left = NULL;
temp -> right = NULL;
if (*root == NULL) {
*root = temp;
return TRUE;
}
// tree is non-empty
cursor = *root;
while (cursor != NULL) {
if(e.ID < cursor -> element -> ID){
if(cursor -> left == NULL){
cursor -> left = temp;
return TRUE;
}
else cursor = cursor -> left;
}
//e goes to the right
else {
if (e.ID > cursor -> element -> ID){
if(cursor -> right == NULL){
cursor -> right = temp;
return TRUE;
}
else
cursor = cursor -> right;
}
else { // e is already in the tree
free(temp -> element);
free(temp);
return FALSE;
}
}
} // while cursor != NULL
} // insert