關于searchnode函數的一點解析,這里想了好長時間才轉過來!
?
Node *Node::SearchNode(int nodeindex)
{
? ? if(this->index==nodeindex)//查找當前節點
? ? ? ? return this;?
? ? if(this->pLChild!=NULL)//查找左子節點
? ? {
? ? ? ? if(this->pLChild->index==nodeindex)
? ? ? ? ? ? return this->pLChild;?
else if (this->pLChild->SearchNode(nodeindex))
return this->pLChild->SearchNode(nodeindex);
else//查找右子節點
{
if(this->pRChild!=NULL)
{
if(this->pRChild->index==nodeindex)
return this->pRChild;
if(this->pRChild->SearchNode(nodeindex))
return this->pRChild->SearchNode(nodeindex);
}
}
? ? }
? ? return NULL;
}
其實最簡單的方法就是仿照遍歷函數,搜索只是多了一個限定條件;
這里searchnode函數的返回值是node類型;
只能有一個return null ;
2020-02-26
我覺得這代碼有問題吧,視頻中刪除的最右邊那個節點,按照這個搜尋方法,當遍歷到最左邊這個節點時,此時this指的是最左邊的指針,這樣他肯定不會進入第二個if語句
2019-08-12
well