因此,我正在研究BST,構建刪除工具。修改遞歸中的調用指針
我的代碼序列似乎工作正確 - 保存沒有更新父或根,並設置將它發送到刪除的節點的地址爲NULL的指針。
我傳遞了一個指針指向我的Erase和RemoveNode函數中的一個指針,以便直接影響實際導致遞歸調用的左,右或根數據成員。在遍歷代碼時,它將removeN函數中的* N設置爲NULL,但這不會在調用對象的數據中反映出來。我在使用指針指針方法時不正確嗎?如果是這樣,是否有一種方法可以遞歸地刪除並能夠修改先前的節點,如果鏈接被銷燬?
節點結構:
struct tNode
{
tNode(int n)
{
data = n;
left = NULL;
right = NULL;
}
//Works, cleans all linked objects.
//Must remember to null links when removing wanted nodes
~tNode(void)
{
//cout << "Deleting " << data << endl;
delete left;
delete right;
}
// Data members
int data;
tNode* left;
tNode* right;
};
橡皮擦功能遞歸在樹:
void BinSearchTree::Erase(int n, tNode** N)
{
tNode* node = *N;
if (root)
{
if (node->data > n) // post order, to avoid moving many times over
{
if (node->left)
{
Erase(n, &node->left);
}
}
else
{
if (node->right)
{
Erase(n, &node->right);
}
}
if (node->data == n)
{
RemoveNode(&node);
}
}
}
而且的removeNode函數來處理實際刪除:
void BinSearchTree::RemoveNode(tNode** N)
{
tNode* node = *N;
if (!node->left && !node->right) // is leaf
{
delete node; // remove node
size--;
*N = NULL; // null pointer for above node/structure
}
else if (!node->left) // right child
{
tNode* temp = node->right; // to strip out copied node when finished
node->data = node->right->data; // copy right node into current node
node->right = node->right->right;
node->left = node->right->left;
temp->right = NULL; // NULL because node destructor is recursive
temp->left = NULL; // ^^
delete temp;
size--;
}
else if (!node->right) // left child
{
tNode* temp = node->left; // to strip out copied node when finished
node->data = node->left->data; // copy left node into current node
node->right = node->left->right;
node->left = node->left->left;
temp->right = NULL; // NULL because node destructor is recursive
temp->left = NULL; // ^^
delete temp;
size--;
}
else // 2 children
{
tNode* temp = node->right; // find ideal child -> left-most right child
tNode* parent = NULL; // keep track of owner of ideal child
while (temp->left)
{
parent = temp;
temp = temp->left;
}
node->data = temp->data; // copy ideal child to root
if (parent)
{
parent->left = temp->right; // case that left-most child has right child of it's own
}
RemoveNode(&temp);
size--;
}
}
您是否有真正的問題? –
@KerrekSB對不起,我一定領先於自己。感謝您指出。 – DivinusVox