2013-05-18 55 views
0

我目前正在研究一個有1個幫助函數的函數,主函數需要2個字符串並搜索第一個(它變成一個引用,就好像它是m_root一樣)並且第二個要在樹中搜索。一旦他們被搜查到,我的幫手功能應該搜索第二個城市,並計算出它的行駛距離,就好像一輛卡車正在朝着那個城市前進。C++試圖找到2個節點之間的距離

int Stree::distance(string origin_city, string destination_city) 
{ 
    int total_distance = 0; 
    Node *new_root = m_root; 
    new_root = find_node(m_root, origin_city); 
    total_distance = get_distance(new_root, total_distance, destination_city); 
    return total_distance; 
} 

int Stree::get_distance(Node* cur, int distance, string destination) 
{ 
    Node *tmp = cur; 
    if(cur == NULL) 
     return 0; 
    if(cur->m_city == destination || tmp->m_city == destination) 
    { 
     //cout << distance + cur->m_parent_distance << endl; 
     return distance += cur->m_parent_distance; 
    } 
    if(tmp->m_left != NULL) 
    { 
     //cout << "checking left" << endl; 
     tmp = cur->m_left; 
     return get_distance(cur->m_left, distance, destination) ; 
    } 
    else 
    { 
     //cout << "checking right" << endl; 
     return get_distance(cur->m_right, distance, destination); 
    } 
} 
+1

這是什麼問題?或者這只是一個狀態報告? – user93353

+0

[通過樹搜索並加起來的城鎮距離]可能的重複(http://stackoverflow.com/questions/16620716/searching-through-a-tree-and-adding-up-the-distances你的路過) – user93353

+0

我的意思是要求當我輸入東西時,它通過樹,然後返回0,不管它在裏面。 –

回答

0

在粗略地看一眼,我沒有看到任何地方,你修改或增加的距離,無論是距離變量或類似的東西:

return 1 + get_distance(cur->m_right, distance, destination); 

所以我會在確保算法感,每走一步計數一次,否則每次肯定會返回0。