BST 的删除函数中的点错误
point error in BST's delete function
Delete(key, T){
BstTree TmpCell; //one tree node
if(T == NULL)
return Not Found;
else if(key < T->data)
T->LeftChild = Delete(key, T->LeftChild);
else if(key > T->data)
T->RightChild = Delete(key, T->RightChild);
else if(T->LeftChild&& T->RightChild){
TmpCell = Findmin(T->RightChild);
T->data = TmpCell->data;
T->RightChild = Delete(T->data, T->RightChild);
}
else{
TmpCell = T;
if(T->LeftChild = NULL) T= T->RightChild;
if(T->RightChild = NULL) T=T->LeftChild;
free(TmpCell);
}
return T;
}
我在《C中的数据结构与算法分析》中找到了代码
如果我要删除 14
else{
TmpCell = T;
if(T->LeftChild = NULL) T = T->RightChild;
if(T->RightChild = NULL) T =T->LeftChild;
free(TmpCell);
}
找到14后,T(14)移动到T的LeftChild(13),但TmpCell仍指向14,然后我释放了它。但是当我输出树时,它输出13,那么13是如何连接到14的父节点的呢?
"connecting" 部分发生在 Delete
的 return 值被分配给某物时,即在
行中
T->LeftChild = Delete(key, T->LeftChild);
和
T->RightChild = Delete(key, T->RightChild);
在函数本身及其调用者处:
myTree = Delete(x, myTree);
(你应该这样称呼它)。
Delete(key, T){
BstTree TmpCell; //one tree node
if(T == NULL)
return Not Found;
else if(key < T->data)
T->LeftChild = Delete(key, T->LeftChild);
else if(key > T->data)
T->RightChild = Delete(key, T->RightChild);
else if(T->LeftChild&& T->RightChild){
TmpCell = Findmin(T->RightChild);
T->data = TmpCell->data;
T->RightChild = Delete(T->data, T->RightChild);
}
else{
TmpCell = T;
if(T->LeftChild = NULL) T= T->RightChild;
if(T->RightChild = NULL) T=T->LeftChild;
free(TmpCell);
}
return T;
}
我在《C中的数据结构与算法分析》中找到了代码
如果我要删除 14
else{
TmpCell = T;
if(T->LeftChild = NULL) T = T->RightChild;
if(T->RightChild = NULL) T =T->LeftChild;
free(TmpCell);
}
找到14后,T(14)移动到T的LeftChild(13),但TmpCell仍指向14,然后我释放了它。但是当我输出树时,它输出13,那么13是如何连接到14的父节点的呢?
"connecting" 部分发生在 Delete
的 return 值被分配给某物时,即在
T->LeftChild = Delete(key, T->LeftChild);
和
T->RightChild = Delete(key, T->RightChild);
在函数本身及其调用者处:
myTree = Delete(x, myTree);
(你应该这样称呼它)。