2013-05-18 101 views
-3

在我的程序中,我遞歸地試圖在給定目標的同時在樹中找到一個節點,但是我無法讓它工作!C++搜索樹中的一個節點

Stree::Node * Stree::find_node(Node* cur, string target) 
{ 
    Node *tmp = cur;; 
    if(cur == NULL || tmp == NULL) 
     return NULL; 
    if(cur->m_city == target || tmp->m_city == target) 
     return cur; 
    if(find_node(tmp->m_left, target)) 
    { 
     return tmp; 
    } 
    else return find_node(cur->m_right, target); 
+2

錯在何處?你會介意給我們一個最小的完整例子嗎? – Beta

+0

無論如何你要做線性搜索,爲什麼要用樹?樹的點通常是獲得O(log N)搜索,但是你沒有這樣做。 –

回答

3

的問題是在這裏:

if(find_node(tmp->m_left, target)) 
{ 
    return tmp; 
} 

您應該返回的find_node有結果:

tmp = find_node(cur->m_left, target); 
if (tmp) return tmp; 
+0

不要喂幫助吸血鬼。 – Puppy