我明天早上参加考试,对考试的一些代码有一些疑问。我得到的代码是我需要识别错误并描述在运行时发生的事情。
二进制搜索树算法 没有孩子,只留下孩子和右孩子的删除案例是正确的,需要纠正和描述的是左右孩子存在。 此代码用于战术性地删除BST中的单个节点
/* find the immediate successor */
Tree * su = root -> right ; /* su must not be NULL */
while (( su -> left ) != NULL )
{
su = su -> left ;
}
/* su is root ’ s immediate successor */
/* swap their values */
root -> value = su -> value ;
//Error here, su is not updated to the new value.
/* delete the successor */
root -> right = Tree_delete ( root , val );
//Error here, the Tree_delete call should be root->right, not root.
return root;
我确定了错误,但有人可以描述运行时会发生什么,以及是否存在内存泄漏?