0

私は現在、1 つのヘルパー関数を持つ関数に取り組んでいます。メイン関数は 2 つの文字列を受け取り、最初の文字列 (m_root であるかのように参照になります) とツリーで検索される 2 番目の文字列を検索します。それらが検索されると、私のヘルパー関数は 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);
    }
}
4

1 に答える 1