Advertisement
JosepRivaille

X84593: Cerca d'estudiants en un arbre binari d'estudiants

Oct 27th, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include "ArbIOest.hh"
  2.  
  3.  
  4. bool search(Arbre<Estudiant>& a, int ident, int& depth, int& mark) {
  5.   if (a.es_buit()) return false;
  6.   else if (a.arrel().consultar_DNI() == ident) {
  7.     if (a.arrel().te_nota()) mark = a.arrel().consultar_nota();
  8.     else mark = -1;
  9.     return true;
  10.   }
  11.   else {
  12.     Arbre<Estudiant> a1, a2;
  13.     a.fills(a1, a2);
  14.     ++depth;
  15.     bool s = search(a1, ident, depth, mark);
  16.     if (s) return true;
  17.     else s = search(a2, ident, depth, mark);
  18.     if (!s) --depth;
  19.     return s;
  20.   }
  21. }
  22.  
  23.  
  24. bool f_search(Arbre<Estudiant> a, int ident, int& depth, int& mark) {
  25.   return search(a, ident, depth, mark);
  26. }
  27.  
  28.  
  29. int main() {
  30.   Arbre<Estudiant> a;
  31.   llegir_arbre_est(a, 0);
  32.   int ident;
  33.   while (cin >> ident) {
  34.     int depth = 0;
  35.     int mark;
  36.     if (f_search(a, ident, depth, mark)) {
  37.       cout << "L'estudiant " << ident << " té profunditat " << depth;
  38.       if (mark == -1) cout << ", però no té nota" << endl;
  39.       else cout << " i la seva nota és " << mark << endl;
  40.     }
  41.     else cout << "L'estudiant " << ident << " no hi és" << endl;
  42.   }
  43. }
  44.  
  45. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement