2013-05-18 54 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。