Advertisement
s00rk

Martin

Dec 12th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package Arboles;
  2.  
  3. public class Metodos
  4. {
  5.     public static void SOP(String msj)
  6.     {
  7.         System.out.println(msj);
  8.     }
  9.    
  10.     public static boolean Metodo1(int C, pNodoArbol nodo)
  11.     {
  12.         if(nodo == null)
  13.             return false;
  14.         if(nodo.X == C)
  15.             return true;
  16.         if(C < nodo.X)
  17.             return Metodo1(C, nodo.LigaIzq);
  18.         else if(C > nodo.X)
  19.             return Metodo1(C, nodo.LigaDer);
  20.         return false;
  21.     }
  22.    
  23.     public static boolean Metodo2(int X, int Y, pNodoArbol nodo)
  24.     {
  25.         if(nodo == null)
  26.             return false;
  27.         if(nodo.X == X)
  28.         {
  29.             if(nodo.LigaIzq != null && nodo.LigaIzq.X == Y)
  30.                 return true;
  31.             else if(nodo.LigaDer != null && nodo.LigaDer.X == Y)
  32.                 return true;
  33.             else
  34.                 return false;
  35.         }
  36.         if(X < nodo.X)
  37.             return Metodo2(X, Y, nodo.LigaIzq);
  38.         else if(X > nodo.X)
  39.             return Metodo2(X, Y, nodo.LigaDer);
  40.         return false;
  41.     }
  42.    
  43.     public static void Metodo3(int X, pNodoArbol nodo)
  44.     {
  45.         if(nodo == null)
  46.             return;
  47.         if(nodo.X == X)
  48.         {
  49.             if(nodo.LigaIzq != null)
  50.                 SOP(nodo.LigaIzq.X+"");
  51.             if(nodo.LigaDer != null)
  52.                 SOP(nodo.LigaDer.X+"");
  53.         }
  54.         if(X < nodo.X)
  55.             Metodo3(X, nodo.LigaIzq);
  56.         else if(X > nodo.X)
  57.             Metodo3(X, nodo.LigaDer);
  58.     }
  59.    
  60.     public static void Metodo4(pNodoArbol nodo)
  61.     {
  62.         if(nodo == null)
  63.             return;
  64.         Metodo4(nodo.LigaIzq);
  65.         if(nodo.LigaIzq == null && nodo.LigaDer == null)
  66.             SOP(nodo.X+"");
  67.         Metodo4(nodo.LigaDer);
  68.     }
  69.  
  70.     public static void Metodo5(pNodoArbol nodo, int Raiz)
  71.     {
  72.         if(nodo == null)
  73.             return;
  74.         Metodo5(nodo.LigaIzq, Raiz);
  75.         if((nodo.LigaIzq != null || nodo.LigaDer != null) && nodo.X != Raiz)
  76.             SOP(nodo.X+"");
  77.         Metodo5(nodo.LigaDer, Raiz);
  78.     }
  79.  
  80.     public static boolean Metodo6(int X, int Y, pNodoArbol nodo)
  81.     {
  82.         if(nodo.LigaDer == null && nodo.LigaIzq == null)
  83.             return false;
  84.         if(nodo.LigaIzq.X == X || nodo.LigaDer.X == X)
  85.         {
  86.             if(nodo.LigaDer.X == Y || nodo.LigaIzq.X == Y)
  87.                 return true;
  88.         }
  89.         if(X < nodo.X)
  90.             return Metodo6(X, Y, nodo.LigaIzq);
  91.         else if(X > nodo.X)
  92.             return Metodo6(X, Y, nodo.LigaDer);
  93.         return false;
  94.     }
  95.    
  96.     public static boolean Metodo7(int X, int Y, pNodoArbol nodo)
  97.     {
  98.         if(nodo == null)
  99.             return false;
  100.        
  101.         if(nodo.X == X)
  102.         {
  103.             pNodoArbol aux;
  104.             if(nodo.LigaIzq != null)
  105.             {
  106.                 aux = nodo.LigaIzq;
  107.                 if(aux.LigaIzq != null && aux.LigaIzq.X == Y)
  108.                     return true;
  109.                 else if(aux.LigaDer!= null && aux.LigaDer.X == Y)
  110.                     return true;
  111.             }else if(nodo.LigaDer != null){
  112.                 aux = nodo.LigaDer;
  113.                 if(aux.LigaDer != null && aux.LigaDer.X == Y)
  114.                     return true;
  115.                 else if(aux.LigaIzq != null && aux.LigaIzq.X == Y)
  116.                     return true;
  117.             }
  118.            
  119.             return false;
  120.         }
  121.         if(X < nodo.X)
  122.             return Metodo7(X, Y, nodo.LigaIzq);
  123.         else if(X > nodo.X)
  124.             return Metodo7(X, Y, nodo.LigaDer);
  125.         return false;
  126.     }
  127.    
  128.     public static void Metodo8(int X, int nivel, pNodoArbol nodo)
  129.     {
  130.         if(!Metodo1(X, nodo))
  131.         {
  132.             SOP("\nNo se encuentra!");
  133.             return;
  134.         }          
  135.         if(nodo.X == X)
  136.         {
  137.             SOP("\nEsta en el nivel: " + nivel);
  138.             return;
  139.         }
  140.         if(X < nodo.X)
  141.             Metodo8(X, nivel+1, nodo.LigaIzq);
  142.         else if(X > nodo.X)
  143.             Metodo8(X, nivel+1, nodo.LigaDer);
  144.     }
  145.  
  146.     public static int Metodo9(pNodoArbol nodo)
  147.     {
  148.         if (nodo == null)
  149.             return -1;
  150.         return 1 + Math.max(Metodo9(nodo.LigaIzq), Metodo9(nodo.LigaDer)); 
  151.     }
  152.    
  153.     public static pNodoArbol Metodo10(pNodoArbol nodo)
  154.     {
  155.         if(nodo != null)
  156.         {
  157.                 Metodo10(nodo.LigaIzq);
  158.                 Metodo10(nodo.LigaDer);
  159.                 nodo = null;
  160.         }
  161.         return nodo;
  162.     }
  163.    
  164.     public static int Metodo11(pNodoArbol nodo,int i)
  165.     {
  166.        
  167.        
  168.     }
  169.    
  170.     public static int Metodo12(pNodoArbol nodo)
  171.     {
  172.         if(nodo == null)
  173.             return 0;
  174.         else
  175.             return (1 + Metodo12(nodo.LigaIzq) + Metodo12(nodo.LigaDer));
  176.     }
  177.    
  178.     public static int Metodo13(pNodoArbol nodo)
  179.     {
  180.         int resp = 0;
  181.         if(nodo != null && nodo.LigaDer != null)
  182.             resp = Metodo12(nodo.LigaDer);
  183.         return resp;           
  184.     }
  185.    
  186.     public static int Metodo14(pNodoArbol nodo)
  187.     {
  188.         int resp = 0;
  189.         if(nodo != null && nodo.LigaIzq != null)
  190.             resp = Metodo12(nodo.LigaIzq);
  191.         return resp;           
  192.     }
  193.    
  194.     public static boolean Metodo15(pNodoArbol nodo)
  195.     {
  196.         return (Metodo13(nodo)==Metodo14(nodo)) ? true : false;
  197.     }
  198. }
  199.  
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement