Advertisement
JosepRivaille

X96849: Cerca en un arbre de parells d'enters

Oct 27th, 2015
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include "ArbIOparint.hh"
  2.  
  3.  
  4. bool search(Arbre<Pari>& a, int n, int& depth, int& m8) {
  5.   if (a.es_buit()) return false;
  6.   else if (a.arrel().first == n) {
  7.     m8 = a.arrel().second;
  8.     return true;
  9.   }
  10.   else {
  11.     Arbre<Pari> a1, a2;
  12.     a.fills(a1, a2);
  13.     ++depth;
  14.     bool s1, s2;
  15.     s1 = search(a1, n, depth, m8);
  16.     s2 = search(a2, n, depth, m8);
  17.     if (!s1 && !s2) --depth;
  18.     return (s1 || s2);
  19.   }
  20. }
  21.  
  22.  
  23. bool f_search(Arbre<Pari> a, int n, int& depth, int& m8) {
  24.   return search(a, n, depth, m8);
  25. }
  26.  
  27.  
  28. int main() {
  29.   Arbre<Pari> a;
  30.   llegir_arbre_parint(a, 0);
  31.   int n;
  32.   while (cin >> n) {
  33.     int depth = 0;
  34.     int m8;
  35.     if (f_search(a, n, depth, m8)) cout << n << " " << m8 << " " << depth << endl;
  36.     else cout << -1 << endl;
  37.   }
  38. }
  39.  
  40. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement