tikimyster

distance

May 11th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. bool tree::distance(std::string origin, std::string sDestination, int &NumOfEdges)
  2. {
  3.     Node *origination = mRoot;
  4.     Node *destination = mRoot;
  5.     origination = lookup(origin, origination);
  6.     destination = lookup(sDestination, destination);
  7.  
  8.     if (!mRoot)
  9.         return false;    // No root city
  10.  
  11.     else if (!origination || !destination)
  12.         return false;   // Either origination or destination are not found
  13.    
  14.     else
  15.     {
  16.         // climb up the parents from the destination until origination iterating distance
  17.         NumOfEdges = 0;
  18.         while (destination->mString != origination->mString)
  19.         {
  20.             destination = destination->mParent;
  21.             NumOfEdges++;
  22.         }
  23.                 return true;
  24.     }
  25.     return false;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment