Advertisement
JosepRivaille

X09609: Cerca de subarbres binaris

Jan 10th, 2016
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. static node_arbre* sub_arrel_immersive(node_arbre *n, const T &x, int &depth)
  2. {
  3.     node_arbre* p = NULL;
  4.     if (n != NULL) {
  5.         if (n->info == x) {
  6.             p = copia_node_arbre(n);
  7.         }
  8.         else {
  9.             int depth1, depth2;
  10.             depth1 = depth2 = depth + 1;
  11.             node_arbre *p_aux = NULL;
  12.             p = sub_arrel_immersive(n->segE, x, depth1);
  13.             p_aux = sub_arrel_immersive(n->segD, x, depth2);
  14.             if (p == NULL) {
  15.                 p = p_aux;
  16.                 depth += depth2;
  17.             }
  18.             else if (p != NULL && p_aux != NULL) {
  19.                 if (depth1 > depth2) {
  20.                     p = p_aux;
  21.                     depth += depth2;
  22.                 }
  23.             }
  24.             else depth += depth1;
  25.         }
  26.     }
  27.     return p;
  28. }
  29.  
  30. void sub_arrel(Arbre& asub, const T& x)
  31. /* Pre: p.i. = A, asub es buit */
  32. /* Post: si A conte x, asub es el subarbre d'A resultat de la cerca;
  33.    si A no conte x, asub es buit */
  34. {
  35.     int depth = 0;
  36.     asub.primer_node = sub_arrel_immersive(this->primer_node, x, depth);
  37.  
  38. }
  39.  
  40. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement