Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct NodoArbol
  6. {
  7.     int info;
  8.     NodoArbol *hi;
  9.     NodoArbol *hd;
  10. };
  11. typedef NodoArbol *arbol;
  12.  
  13. void inserta(arbol &,int);
  14. void preOrden(arbol);
  15. void enOrden(arbol);
  16. void postOrden(arbol);
  17. arbol buscar(arbol,int);
  18. int numNodos(arbol);
  19. int elemDer(arbol);
  20. arbol mayorElemRecur(arbol);
  21. int mayorIterativo(arbol);
  22. void camino(arbol,int);
  23. int longitudCamino(arbol,int);
  24. void diferencia(arbol);
  25. int segundoElem(arbol);
  26. int main()
  27. {
  28.     arbol abb = NULL;
  29.     int n, i, x, val,val1;
  30.  
  31.     cout<<"Numero de elementos: ";
  32.     cin>>n;
  33.  
  34.     for(i=1;i<=n;i++)
  35.     {
  36.         cout<<"Ingrese elemento: ";
  37.         cin>>x;
  38.         inserta(abb,x);
  39.     }
  40.  
  41.     /*cout<<"\nPreOrden"<<endl;
  42.     preOrden(abb);
  43.  
  44.     cout<<"\nEnOrden"<<endl;
  45.     enOrden(abb);
  46.  
  47.     cout<<"\nPost-Orden"<<endl;
  48.     postOrden(abb);
  49.  
  50.     cout<<"\nIngrese elemento a buscar: ";
  51.     cin>>val;
  52.  
  53.     arbol p;
  54.     p = buscar(abb,val);
  55.     if(p!=NULL)
  56.     {
  57.         cout<<"El elemento se encuentra en el arbol"<<endl;
  58.     }
  59.     else
  60.     {
  61.         cout<<"El elemento no se encuentra en el arbol"<<endl;
  62.     }
  63.  
  64.     cout<<"El numero de nodos es: "<<numNodos(abb)<<endl;
  65.     cout<<"Elementos a la derecha del nodo.. :";
  66.     cin>>val1;
  67.     p = buscar(abb,val1);
  68.     cout<<"Hay "<<elemDer(p)-1;
  69.  
  70.     p = mayorElemRecur(abb);
  71.     if(p!=NULL)
  72.     {
  73.         cout<<"\nEl mayor elemento es: "<<p->info<<endl;
  74.     }
  75.  
  76.     cout<<"EL mayor elemento iterativamente: "<<mayorIterativo(abb);
  77.  
  78.     cout<<"\nIngrese nodo para ver su camino: ";
  79.     cin>>x;
  80.  
  81.     camino(abb,x);
  82.  
  83.     cout<<"\nEl camino recorrido es: "<<longitudCamino(abb,x);*/
  84.  
  85.     diferencia(abb);
  86.  
  87.     cout<<"El segundo elemento menor del arbol es: "<<segundoElem(abb);
  88.  
  89. }
  90.  
  91. void inserta(arbol &abb, int x)
  92. {
  93.     if(abb == NULL)
  94.     {
  95.         abb = new NodoArbol;
  96.         abb->info = x;
  97.         abb->hi = NULL;
  98.         abb->hd = NULL;
  99.     }
  100.     else
  101.     {
  102.         if(x<abb->info)
  103.         {
  104.             inserta(abb->hi,x);
  105.         }
  106.         else
  107.         {
  108.             if(x>abb->info)
  109.             {
  110.                 inserta(abb->hd,x);
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. void preOrden(arbol abb)
  117. {
  118.     if(abb != NULL)
  119.     {
  120.         cout<<abb->info<<" ";
  121.         preOrden(abb->hi);
  122.         preOrden(abb->hd);
  123.     }
  124. }
  125.  
  126. void enOrden(arbol abb)
  127. {
  128.     if(abb != NULL)
  129.     {
  130.        enOrden(abb->hi);
  131.        cout<<abb->info<<" ";
  132.        enOrden(abb->hd);
  133.     }
  134. }
  135. void postOrden(arbol abb)
  136. {
  137.     if(abb != NULL)
  138.     {
  139.         postOrden(abb->hi);
  140.         postOrden(abb->hd);
  141.         cout<<abb->info<<" ";
  142.     }
  143. }
  144.  
  145. arbol buscar(arbol abb,int x)
  146. {
  147.     if(abb != NULL)
  148.     {
  149.         if(abb->info == x)
  150.         {
  151.             return abb;
  152.         }
  153.         else
  154.         {
  155.             if(x < abb->info)
  156.             {
  157.                 return buscar(abb->hi,x);
  158.             }
  159.             else
  160.             {
  161.                 return buscar(abb->hd,x);
  162.             }
  163.         }
  164.     }
  165.     else
  166.     {
  167.         return NULL;
  168.     }
  169. }
  170.  
  171. int numNodos(arbol abb)
  172. {
  173.     if(abb!=NULL)
  174.     {
  175.         return 1+numNodos(abb->hi)+numNodos(abb->hd);
  176.     }
  177.     else
  178.         return 0;
  179. }
  180. int elemDer(arbol abb)
  181. {
  182.     if(abb!=NULL)
  183.     {
  184.             return 1+elemDer(abb->hd);
  185.     }
  186.     else
  187.     {
  188.         return 0;
  189.     }
  190. }
  191. arbol mayorElemRecur(arbol abb)
  192. {
  193.     if(abb != NULL)
  194.     {
  195.         if(abb->hd != NULL)
  196.         {
  197.             return mayorElemRecur(abb->hd);
  198.         }
  199.         else
  200.         {
  201.             return abb;
  202.         }
  203.     }
  204.     else
  205.     {
  206.         return NULL;
  207.     }
  208. }
  209. int mayorIterativo(arbol abb)
  210. {
  211.     arbol p = abb;
  212.     while(p->hd != NULL)
  213.         p = p->hd;
  214.     return p->info;
  215. }
  216.  
  217. void camino(arbol abb,int x)
  218. {
  219.     int num = buscar(abb,x)->info;
  220.  
  221.     while(abb!=NULL)
  222.     {
  223.         if(abb->info > num)
  224.         {
  225.             cout<<abb->info<<" "<<endl;
  226.             abb = abb->hi;
  227.         }
  228.         else
  229.         {
  230.             cout<<abb->info<<" "<<endl;
  231.             abb = abb->hd;
  232.         }
  233.     }
  234. }
  235.  
  236. int longitudCamino(arbol abb, int x)
  237. {
  238.     int c = 0;
  239.     int num = buscar(abb,x)->info;
  240.     while(abb!=NULL)
  241.     {
  242.         if(abb->info > num)
  243.         {
  244.             c++;
  245.             abb = abb->hi;
  246.         }
  247.         else
  248.         {
  249.             c++;
  250.             abb = abb->hd;
  251.         }
  252.     }
  253.     return c;
  254. }
  255.  
  256. void diferencia(arbol abb)
  257. {
  258.     int dif = 0;
  259.     arbol p,q;
  260.     p = abb;
  261.     q = abb;
  262.  
  263.     while(p->hd!=NULL)
  264.     {
  265.         p = p->hd;
  266.     }
  267.     cout<<"Mayor elemento: "<<p->info<<endl;
  268.     while(q->hi!=NULL)
  269.     {
  270.         q = q->hi;
  271.     }
  272.     cout<<"Menor elemento: "<<q->info<<endl;
  273.     dif = p->info-q->info;
  274.     cout<<"Diferencia entre ambos: "<<dif<<endl;
  275. }
  276.  
  277. int segundoElem(arbol abb)
  278. {
  279.     arbol p = abb;
  280.  
  281.     while(p->hi!=NULL)
  282.     {
  283.         if(p->hi > p->hd)
  284.         {
  285.             return p->info;
  286.         }
  287.         p = p->hi;
  288.     }
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement