我想删除使用两种方法的样本二叉搜索树的左子(10)地址:指针 - 传递PTR到PTR或路过的PTR
- 方法一:通过传递指针指向当前节点的指针。方法2:通过将指针的地址传递给当前节点。这不会删除节点,但调用delete会破坏指针排列,导致在打印节点时发生崩溃。
的树是这个样子,我试图删除10,并用5
我有一个指针有一定了解更换。但是,我仍然不清楚指针的这种行为。
#include <iostream>
class Node
{
public:
Node(int key) : leftChild(0), rightChild(0), m_key (key){}
~Node(){}
Node *leftChild;
Node *rightChild;
int m_key;
};
Node* build1234(int, int, int, int);
void print(Node *);
void print1234(Node *);
void removeLeft(Node **nodePtr)
{
Node *oldPtr = *nodePtr;
if(*nodePtr)
{
*nodePtr = (*nodePtr)->leftChild;
delete oldPtr;
}
}
int main()
{
Node *demo1 = build1234(10, 20, 30, 5);
Node *demo2 = build1234(10, 20, 30, 5);
print1234(demo1);
print1234(demo2);
//Method1 - 10 is correctly removed with 5
Node **nodePtr = &demo1;
nodePtr = &(*nodePtr)->leftChild;
removeLeft(nodePtr);
print1234(demo1);
//Method2 - 10 is not removed
Node *node = demo2;
node = node->leftChild;
removeLeft(&node);
print1234(demo2);
return 0;
}
Node* build1234(int B, int A, int C, int D)
{
Node *root = new Node(A);
root->leftChild = new Node(B);
root->rightChild = new Node(C);
root->leftChild->leftChild = new Node(D);
return root;
}
void print(Node *node)
{
if(node)
{
print(node->leftChild);
std::cout << "[" << node->m_key << "]";
print(node->rightChild);
}
}
void print1234(Node *node)
{
std::cout << std::endl;
print(node);
}
注:这个问题是不是BST,但三分球。如果您看到removeLeft(nodePtr)
的两个呼叫和main()
功能中的removeLeft(&node)
。
- 这两个不同?
- 为什么第二种方法无法达到预期效果?
你能再细说一下吗? 'removeNode(nodePtr)'vs'removeNode(&node)'。我同意'nodePtr'和'&node'是不同的,但'* nodePtr'和'node'指向同一个位置。 – FiguringLife
经过很多想法:)和笔/纸工作,我已经明白你想说什么。 – FiguringLife