finding distance between two nodes in binary tree

Hey guys I'm going to try and explain this the best I can:
I have created a binary tree, it's been tested and works.
I'm making a binary tree for a train schedule I need to find the distance between two nodes if the trains are in the same route.
The input I put in is:
1
2
3
4
5
6
7
8
9
10
11
12
tree.insert("chico","redding"); //chico left redding shouldnt be able to connect with sac
 tree.insert("chico","sacramento");//chico right
 tree.insert("sacramento","fg");//chico right sac left
 tree.insert("sacramento","needles");//chico right sac right
 tree.print("chico");
 tree.print("sacramento");
 tree.distance("chico","needles");
 tree.distance("redding","sacramento");
 tree.distance("chico","chico");
 tree.distance("chico","sacramento");
 tree.insert("needles", "laughlin");
 tree.distance("chico","laughlin");


Code chunk is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Stree::distance(string origination, string destination)
{
  Node *dnode = find_node(destination,m_root);
  Node *onode = find_node(origination,m_root);
  int count = 0;
  if(m_root == NULL)
    return -1;
  while (dnode != onode && onode != NULL)
  {
    count++;
    dnode = dnode->m_parent;
    if (dnode == NULL)
    {
      cout << "error: destination not in tree"<< endl;
      return -1;
    } 
  }
  cout << count << endl;
}



The output is supposed to read the trains that are connected to the two cities, (which it does) but When I look for distance I get wrong data. The data I get is:

1
error: route not in tree;
0
1
2

the first and the last numbers are wrong by 1 digit but the middle three are correct. What I am looking for is:
2
error: route not in tree
0
1
3


any ideas??!
thank you.
Topic archived. No new replies allowed.