Advertisement
juanjo12x

Verificar_Sub_Arbol

Jun 15th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //Juanjose Tenorio Peña
  5. typedef struct nodo{
  6.     int valor;
  7.     struct nodo* ptrHI;
  8.     struct nodo* ptrHD;
  9. } TNodo;
  10. TNodo* aux;
  11. void crear_arbol(TNodo ** ptrRaiz);
  12. void inserta_nodo(TNodo **ptrRaiz, int valor);
  13. int isSubtree(TNodo*,TNodo*);
  14. int Identicos(TNodo*,TNodo*);
  15. void crear_arbol(TNodo ** ptrRaiz){
  16.     *ptrRaiz = NULL;
  17. }
  18.  
  19. void inserta_nodo(TNodo **ptrRaiz, int valor){
  20.     TNodo *ptrHoja, *ptrRecorrido, *ptrPadre;
  21.     ptrHoja = (TNodo*)malloc(sizeof(TNodo));
  22.     ptrHoja->valor = valor;
  23.     ptrHoja->ptrHI=ptrHoja->ptrHD=NULL;
  24.    
  25.     if(*ptrRaiz){
  26.         ptrRecorrido = *ptrRaiz;
  27.         ptrPadre = NULL;
  28.        
  29.         while (ptrRecorrido){
  30.             ptrPadre = ptrRecorrido;
  31.             if (ptrRecorrido->valor > valor)
  32.                 ptrRecorrido = ptrRecorrido->ptrHI;
  33.             else
  34.                 ptrRecorrido = ptrRecorrido->ptrHD;                  
  35.         }
  36.         if (ptrPadre->valor > valor)
  37.             ptrPadre->ptrHI = ptrHoja;
  38.         else
  39.             ptrPadre->ptrHD = ptrHoja;
  40.     }
  41.     else{
  42.      *ptrRaiz = ptrHoja;  
  43.     }          
  44. }
  45. /* Reviso si las raices de 2 arboles son iguales */
  46. int Identicos(TNodo * root1, TNodo *root2)
  47. {
  48.     /* casos base */
  49.     if(root1 == NULL && root2 == NULL)
  50.         return 1;
  51.  
  52.     if(root1 == NULL || root2 == NULL)
  53.         return 0;
  54.  
  55.     /* Reviso si los valores de las raices son iguales */
  56.     return (root1->valor== root2->valor   &&
  57.             Identicos(root1->ptrHI, root2->ptrHI) &&
  58.             Identicos(root1->ptrHD, root2->ptrHD) );
  59. }
  60.  
  61. int isSubtree(TNodo*T,TNodo*S){
  62.     /* casos base */
  63.     if (S == NULL)
  64.         return 1;
  65.  
  66.     if (T == NULL)
  67.         return 0;
  68.  
  69.     /* Verifico el arbol con el actual nodo */
  70.     if (Identicos(T, S))
  71.         return 1;
  72.  
  73.     /* Si el valor de la raiz no concuerda, reviso en el subarbol izquierdo
  74.      *  y derecho */
  75.     return isSubtree(T->ptrHI, S) ||
  76.            isSubtree(T->ptrHD, S);
  77. }
  78. int main(int argc, char** argv) {
  79.    TNodo *ptrRaiz;TNodo * ptr2;
  80.    crear_arbol(&ptrRaiz);
  81.    inserta_nodo(&ptrRaiz, 4);
  82.     inserta_nodo(&ptrRaiz, 2);
  83.     inserta_nodo(&ptrRaiz, 1);
  84.     inserta_nodo(&ptrRaiz, 3);
  85.     inserta_nodo(&ptrRaiz, 5);
  86.     inserta_nodo(&ptr2, 10);
  87.     inserta_nodo(&ptr2, 15);
  88.    
  89.     if( isSubtree(ptrRaiz, ptr2) )
  90.         printf("El arbol 2 es subarbol del arbol 1");
  91.     else
  92.         printf("El arbol 2 no es subarbol del arbol 1");
  93.  
  94.     getchar();
  95.     return (EXIT_SUCCESS);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement