java 删除二进制内容_二进制搜索树节点删除
我正在實現從二叉搜索樹中刪除節點的功能 . 該功能的原型已設置,我無法更改它,這是一項學校作業 . 我的代碼:
typedef struct tBSTNode {
char Key;
struct tBSTNode * LPtr;
struct tBSTNode * RPtr;
} *tBSTNodePtr;
void BSTDelete (tBSTNodePtr *RootPtr, char K) {
tBSTNodePtr *tmp;
if (*RootPtr != NULL) {
if (K < (*RootPtr)->Key)
BSTDelete(&(* RootPtr)->LPtr, K);
else if (K > (*RootPtr)->Key)
BSTDelete(&(* RootPtr)->RPtr, K);
else {
if ((* RootPtr)->LPtr == NULL) {
/* there is only right branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->RPtr;
free(*tmp);
*tmp = NULL;
}
else if ((* RootPtr)->RPtr == NULL) {
/* there is only left branch or none*/
tmp = RootPtr;
*RootPtr = (* RootPtr)->LPtr;
free(*tmp);
*tmp = NULL;
}
else
/* there are both branches, but that is for another topic*/
}
}
}
如果沒有分支連接到我正在刪除的節點,此代碼可以正常工作 . 我希望 *tmp = NULL; 行存在問題,我將地址丟失到分支的其余部分,但另一方面,如果不包括此行,我會得到一個SEGFAULT,我試圖找出原因 .
編輯:
好的,現在我知道錯誤在哪里 . 這是愚蠢的錯誤,我應該使用 tBSTNodePtr tmp; 而不是 *tBSTNodePtr tmp;
總結
以上是生活随笔為你收集整理的java 删除二进制内容_二进制搜索树节点删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell 安装java_Shell脚本
- 下一篇: mac怎么用python2和3_Mac同