我很困惑你如何初始化m_parent指向子節點上方的節點。C++如何在二叉樹中爲父指定一個指針
class Node
{
public:
Node(string city);
~Node();
string m_city;
int m_parent_distance;
Node *m_left;
Node *m_right;
Node *m_parent;
};
Node *m_root;
//void test_print(string target, Node *cur_root);
Node * find_node(Node *m_root, string target);
這是節點構造
Stree::Node::Node(string city)
{
m_city = city;
m_left = NULL;
m_right = NULL;
//what do i do with m_parent??
m_parent_distance = 0;
}
非常有幫助,謝謝,這讓我想起了一個事實,即我可以在創建節點本身時初始化m_parent。即m_root-> m_left =新節點(city2,m_root);你可以很容易地看到m_root成爲父母! –