Recent Posts
None | 14 sec ago
C | 25 sec ago
None | 49 sec ago
Lua | 49 sec ago
PAWN | 56 sec ago
None | 1 min ago
VB.NET | 1 min ago
C++ | 2 min ago
C++ | 2 min ago
None | 3 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 18th of Mar 2009 04:58:52 PM Download | Raw | Embed | Report
  1. //arbol en java inorden, posorden, preorden */
  2.  
  3. //definicion de la clase NodoArbol
  4. class NodoArbol {
  5.        
  6.         //miembros de acceso
  7.         NodoArbol nodoizquierdo;
  8.         int datos;
  9.         NodoArbol nododerecho;
  10.        
  11.         //iniciar dato y hacer de este nodo un nodo hoja
  12.         public NodoArbol(int datosNodo)
  13.         {
  14.                 datos = datosNodo;
  15.                 nodoizquierdo = nododerecho = null; //el nodo no tiene hijos
  16.         }
  17.        
  18.         //buscar punto de insercion  e insertar nodo nuevo
  19.         public synchronized void insertar(int valorInsertar)
  20.         {
  21.                 //insertar en subarbol izquierdo
  22.                 if (valorInsertar < datos){
  23.                
  24.                         //inserta nuevo nodoarbol
  25.                         if (nodoizquierdo == null)
  26.                                 nodoizquierdo = new NodoArbol(valorInsertar);
  27.                         else //continua recorriendo subarbol izquierdo
  28.                                 nodoizquierdo.insertar(valorInsertar);
  29.                 }
  30.                
  31.                 //insertar nodo derecho
  32.                 else if(valorInsertar > datos){
  33.                
  34.                 //insertar nuevo nodoarbol
  35.                 if (nododerecho == null)
  36.                         nododerecho = new NodoArbol(valorInsertar);
  37.                 else //continua recorriendo subarbol derecho
  38.                         nododerecho.insertar(valorInsertar);
  39.                 }
  40.         } //fin del metodo insertar
  41.                
  42. } //fin clase nodoarbol
  43.  
  44. //---------- CLASE ARBOL------------------
  45.  class Arbol{
  46.         private NodoArbol raiz;
  47.        
  48.         //contruir un arbol vacio
  49.         public Arbol()
  50.         {
  51.                 raiz = null;
  52.         }
  53.        
  54.         //insertar un nuevo nodo en el arbol de busqueda binaria
  55.         public synchronized void insertarNodo(int valorInsertar)
  56.         {
  57.                 if(raiz == null)
  58.                         raiz = new NodoArbol(valorInsertar); //crea nodo raiz
  59.                
  60.                 else
  61.                         raiz.insertar(valorInsertar); // llama al metodo insertar              
  62.         }
  63.        
  64.         //--------------- EMPESAR EL RECORRIDO EN PREORDEN-----------------------
  65.         public synchronized void recorridoPreorden()
  66.         {
  67.                 ayudantePreorden(raiz);
  68.         }
  69.         //metodo recursivo para recorrido en preorden
  70.        
  71.         private void ayudantePreorden(NodoArbol nodo)
  72.         {
  73.                 if (nodo == null)
  74.                         return;
  75.                
  76.                
  77.                 System.out.print(nodo.datos + " "); // mostrar datos del nodo
  78.                 ayudantePreorden(nodo.nodoizquierdo); //recorre subarbol izquierdo
  79.                 ayudantePreorden(nodo.nododerecho); //recorre subarbol derecho
  80.         }
  81.        
  82.         //--------------EMPEZAR RECORRIDO INORDEN-----------------------------------
  83.         public synchronized void recorridoInorden()
  84.         {
  85.                 ayudanteInorden(raiz);
  86.         }
  87.        
  88.         // metodo recursivo para recorrido inorden
  89.        
  90.         private void ayudanteInorden(NodoArbol nodo)
  91.         {
  92.                 if (nodo == null)
  93.                         return;
  94.                
  95.                
  96.                 ayudanteInorden(nodo.nodoizquierdo);
  97.                 System.out.print(nodo.datos + " ");
  98.                 ayudanteInorden(nodo.nododerecho);
  99.         }
  100.        
  101.         //-----------------------------EMPEZAR RECORRIDO POSORDEN---------------------------------
  102.         public synchronized void recorridoPosorden()
  103.         {
  104.                 ayudantePosorden(raiz);
  105.         }
  106.        
  107.         //metodo recursivo para recorrido posorden
  108.        
  109.         private void ayudantePosorden(NodoArbol nodo)
  110.         {
  111.                 if (nodo == null)
  112.                         return;
  113.                        
  114.                 ayudantePosorden(nodo.nodoizquierdo);
  115.                 ayudantePosorden(nodo.nododerecho);
  116.                 System.out.print(nodo.datos + " ");
  117.         }
  118.        
  119. }//fin clase arbol
  120.  
  121. //programa para probar la clase arbol
  122.  
  123. public class PruebaArbol {
  124.         public static void main(String args[])
  125.         {
  126.                 Arbol arbol = new Arbol();
  127.                 int valor;
  128.        
  129.                 System.out.println( "Insertando los siguientes valores: ");
  130.                
  131.                 //insertando 10 numeros aleatorios del 0 al 99 en el arbol
  132.                 for (int i = 1; i<=10 ; i++)
  133.                 {
  134.                         valor = (int) (Math.random() * 100);
  135.                         System.out.print(valor + " ");
  136.                         arbol.insertarNodo(valor);
  137.                 }
  138.                
  139.                 System.out.println("\n\nRecorrido preorden");
  140.                 arbol.recorridoPreorden();
  141.                
  142.                 System.out.println("\n\nRecorrido inorden");
  143.                 arbol.recorridoInorden();
  144.                
  145.                 System.out.println("\n\nRecorrido posorden");
  146.                 arbol.recorridoPosorden();
  147.         }
  148. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: