-3
設置父節點我有下面的類:C++在樹上
class Node
{
private:
Node* leftChild;
Node* rightChild;
Node* father;
public:
Node() { leftChild = rightChild = father = NULL; };
Node* getLeftChild() { return leftChild; };
Node* getRightChild() { return rightChild; };
Node* getFather() { return father; }
void setRightChild(Node* child) { rightChild = child; }
void setLeftChild(Node* child) { leftChild = child; };
void setFather(Node* f) { father = f; };
};
設置左子和右子的時候我會還設置了父節點。我嘗試:
void setLeftChild(Node* child)
{
leftChild = child;
child->setFather(this);
};
Node* node = new Node();
Node* node2 = new Node();
node->setLeftChild(node2);
由於錯誤的使用,我收到一個隨機錯誤。我應該如何設置功能setLeftChild()
和setRightChild()
? 謝謝。
爲什麼不是母節點?這是性別歧視。這就是我們稱之爲父母的原因。 *觸發* – arminb
請詳細說明錯誤 – noelicus
您可能會發現**很多**更容易使用標準容器,例如['std :: deque <>'](http://en.cppreference.com/ w/cpp/container/deque) –