2013-02-04 60 views
3

試圖教自己C++(我通常使用Python)並編寫了這段代碼。這是指針問題嗎?

#include <iostream> 
using namespace std; 


class TreeNode { 
    public: 
     TreeNode *left; 
     TreeNode *right; 
     int value; 

     TreeNode(int _value); 
     void add_node(TreeNode node); 
}; 

TreeNode::TreeNode(int _value) { 
    left = 0; 
    right = 0; 
    value = _value; 
    cout << "Creating node with value: " << value << endl; 
} 

void TreeNode::add_node(TreeNode node) { 
    cout << "Adding " << node.value << " to " << value << endl; 
    if (node.value < value) { 
     cout << node.value << " < " << value << endl; 
     if (left) { 
      cout << "Left node of " << value << " exists " << left->value << endl; 
      left->add_node(node); 
     } else { 
      cout << "Left node of " << value << " does not exist" << endl; 
      left = &node; 
     } 
    } 
    if (node.value > value) { 
     cout << node.value << " > " << value << endl; 
     if (right) { 
      cout << "Right node of " << value << " exists " << right->value << endl; 
      right->add_node(node); 
     } else { 
      cout << "Right node of " << value << " does not exist" << endl; 
      right = &node; 
     } 
    } 
} 

int main() 
{ 
    TreeNode root(25); 
    TreeNode n1(15); 
    TreeNode n2(30); 
    TreeNode n3(20); 

    root.add_node(n1); 
    root.add_node(n2); 
    root.add_node(n3); 

    cout << root.left->value << endl; 
    cout << root.right->value << endl; 

    return 0; 
} 

該程序編譯但運行的結果,我不明白。

Creating node with value: 25 
Creating node with value: 15 
Creating node with value: 30 
Creating node with value: 20 
Adding 15 to 25 
15 < 25 
Left node of 25 does not exist 
Adding 30 to 25 
30 > 25 
Right node of 25 does not exist 
Adding 20 to 25 
20 < 25 
Left node of 25 exists 20 
Adding 20 to 20 
20 
20 

我期待的最後一位不同。

Adding 20 to 25 
20 < 25 
Left node of 25 exists 15 
Adding 20 to 15 
20 > 15 
Right node of 15 does not exist 
15 
30 

有人可以解釋這裏發生了什麼嗎?

+2

'左= &node;' - http://stackoverflow.com/questions/6441218/can-a-local-variables-memory被訪問的範圍之外 – chris

+1

如果你還沒有理解指針,那麼最好先學習C吧,C++有一些麻煩,**會把你混淆爲初學者。 – 2013-02-04 19:09:40

+0

更好的是,完全避免生成指針並使用現代C++習慣用法。 – juanchopanza

回答

3

您正在複製TreeNode副本的地址,而不是主節點中TreeNode的地址。
注意到註釋掉函數調用,並閱讀: How to pass objects to functions in C++?

//Output with TreeNode node as arg 
//Creating node with value: 25 
//Creating node with value: 15 
//Creating node with value: 30 
//Creating node with value: 20 
//Adding 15 to 25 
//15 < 25 
//Left node of 25 does not exist 
//Adding 30 to 25 
//30 > 25 
//Right node of 25 does not exist 
//Adding 20 to 25 
//20 < 25 
//Left node of 25 exists 20 
//Adding 20 to 20 
//20 
//20 

//Output with TreeNode & node as arg 
//Creating node with value: 25 
//Creating node with value: 15 
//Creating node with value: 30 
//Creating node with value: 20 
//Adding 15 to 25 
//15 < 25 
//Left node of 25 does not exist 
//Adding 30 to 25 
//30 > 25 
//Right node of 25 does not exist 
//Adding 20 to 25 
//20 < 25 
//Left node of 25 exists 15 
//Adding 20 to 15 
//20 > 15 
//Right node of 15 does not exist 
//15 
//30 

#include <iostream> 
using namespace std; 


class TreeNode { 
    public: 
     TreeNode *left; 
     TreeNode *right; 
     int value; 

     TreeNode(int _value); 
     //void add_node(TreeNode node); 
     void add_node(TreeNode & node); 
}; 

TreeNode::TreeNode(int _value) { 
    left = 0; 
    right = 0; 
    value = _value; 
    cout << "Creating node with value: " << value << endl; 
} 

//void TreeNode::add_node(TreeNode node) { 
void TreeNode::add_node(TreeNode & node) { 
    cout << "Adding " << node.value << " to " << value << endl; 
    if (node.value < value) { 
     cout << node.value << " < " << value << endl; 
     if (left) { 
      cout << "Left node of " << value << " exists " << left->value << endl; 
      left->add_node(node); 
     } else { 
      cout << "Left node of " << value << " does not exist" << endl; 
      left = &node; 
     } 
    } 
    if (node.value > value) { 
     cout << node.value << " > " << value << endl; 
     if (right) { 
      cout << "Right node of " << value << " exists " << right->value << endl; 
      right->add_node(node); 
     } else { 
      cout << "Right node of " << value << " does not exist" << endl; 
      right = &node; 
     } 
    } 
} 

int main() 
{ 
    TreeNode root(25); 
    TreeNode n1(15); 
    TreeNode n2(30); 
    TreeNode n3(20); 

    root.add_node(n1); 
    root.add_node(n2); 
    root.add_node(n3); 

    cout << root.left->value << endl; 
    cout << root.right->value << endl; 

    return 0; 
} 
+0

雖然這可以工作,但值得注意的是,在真正的軟件中,我們通常不會構建這樣的樹。我們使用*動態分配*並將*指針傳遞給節點*。 –

+1

真的嗎?我通常添加*元素*並讓樹在內部處理節點的分配和放置。 – paddy

+0

@ n.m。如果數據結構包含常量數據,我會考慮以這種方式構建它。 –