C++ 在树上搜索并计算你经过的城镇的距离

C++ 在树上搜索并计算你经过的城镇的距离,c++,tree,binary-tree,C++,Tree,Binary Tree,这两个函数一起将返回直接到达节点所需的总距离。目前,它无法处理更长、更复杂的事情。不过,简单的事情就行了 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 = g

这两个函数一起将返回直接到达节点所需的总距离。目前,它无法处理更长、更复杂的事情。不过,简单的事情就行了

    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);
    }
}
int Stree::距离(字符串起点城市,字符串终点城市)
{
int总距离=0;
Node*new_root=m_root;
new_root=查找_节点(m_root,原点城市);
总距离=获取距离(新根、总距离、目的地城市);
返回总距离;
}
int Stree::get_distance(节点*cur,int distance,字符串目标)
{
节点*tmp=cur;
如果(cur==NULL)
返回0;
如果(cur->m|u city==目的地| tmp->m|u city==目的地)
{
//cout m_parent_distance m_parent_distance;
}
如果(tmp->m_left!=NULL)
{
//左库特(cout m_)、距离、目的地);
}
其他的
{

//cout只是一个猜测:我认为代码循环的唯一方式应该是当树包含循环时——即根本不是树

请尝试此调试帮助程序:

#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
...
static vector<Node*> visited;

int Stree::get_distance(Node* cur, int distance, string destination)
{
    if (find(visited.begin(), visited.end(), cur) != visited.end())
        throw runtime_error("looping " + cur->m_city);

    Node *tmp = cur;
    if(cur == NULL)
        return 0;

    int ret;
    visited.push_back(cur);
    if(cur->m_city == destination || tmp->m_city == destination)
    {
        //cout << distance + cur->m_parent_distance << endl;
        ret = distance += cur->m_parent_distance;
    }
    if(tmp->m_left != NULL)
    {
        //cout << "checking left" << endl;
        tmp = cur->m_left;
        ret = get_distance(cur->m_left, distance, destination) ;
    }
    else
    {
        //cout << "checking right" << endl;
        ret = get_distance(cur->m_right, distance, destination);
    }

    visited.pop_back();
    return ret;
}
#包括
#包括
#包括
使用名称空间std;
...
静态向量机;
int Stree::get_distance(节点*cur,int distance,字符串目标)
{
if(find(visited.begin(),visited.end(),cur)!=visited.end())
抛出运行时错误(“循环”+cur->m_city);
节点*tmp=cur;
如果(cur==NULL)
返回0;
int ret;
访问。推回(cur);
如果(cur->m|u city==目的地| tmp->m|u city==目的地)
{
//cout m_parent_distance m_parent_distance;
}
如果(tmp->m_left!=NULL)
{
//左库特(cout m_)、距离、目的地);
}
其他的
{

//您能为有效的案例和无效的案例提供一些输入数据,帮助我们理解您所说的“长而复杂”而不是“简单”的含义吗?输入在另一个函数中,该函数将它们全部放入树中,然后这个函数将查找它们。这就是我在代码顶部的意思::我目前正在处理一个有1个帮助函数的函数,主函数接收2个字符串并搜索第一个字符串(它将成为一个引用,就像它是m_根一样)在树中搜索第二个。一旦搜索到第二个城市,“我的助手”功能将搜索第二个城市,并计算它必须行驶的距离,就好像一辆卡车正驶向该城市一样。如果要让人们有机会帮助你,他们需要比你提供的更多的信息。你说的是什么在这种情况下,“长而复杂”与“简单”相对?它只是意味着,如果只输入1或3个城市,它工作正常,但一旦它必须开始检查更复杂的树,它就不工作了。