Example #1
0
File: sumas.cs Project: hvzzzz/-
 // ----------------------------------------------------------------------------
 public void Agregar(object pElem)
 {
     if (Raiz == null)
     {
         Raiz = pElem;
     }
     else
     if (pElem.ToString().CompareTo(Raiz.ToString()) < 0)
     {
         if (SubArbolIzq == null)
         {
             SubArbolIzq = new CArbolBB(pElem);
         }
         else
         {
             SubArbolIzq.Agregar(pElem);
         }
     }
     else      // pElem.ToString().CompareTo(arbol.Raiz.ToString()) > 0
     {
         if (SubArbolDer == null)
         {
             SubArbolDer = new CArbolBB(pElem);
         }
         else
         {
             SubArbolDer.Agregar(pElem);
         }
     }
 }
Example #2
0
File: sumas.cs Project: hvzzzz/-
        static void Agregar(CArbolBB arbol, object pElem)
        {
            if (arbol.Raiz == null)
            {
                arbol.Raiz = pElem;
            }
            else
            if (pElem.ToString().CompareTo(arbol.Raiz.ToString()) < 0)
            {
                if (arbol.SubArbolIzq == null)
                {
                    arbol.SubArbolIzq = new CArbolBB(pElem);
                }
                else
                {
                    Agregar(arbol.SubArbolIzq, pElem);
                }
            }

            if (pElem.ToString().CompareTo(arbol.Raiz.ToString()) > 0)
            {
                if (arbol.SubArbolDer == null)
                {
                    arbol.SubArbolDer = new CArbolBB(pElem);
                }
                else
                {
                    Agregar(arbol.SubArbolDer, pElem);
                }
            }
        }
Example #3
0
File: sumas.cs Project: hvzzzz/-
 // Métodos
 // -- Constructores
 public CArbolBB(object pRaiz          = null, CArbolBB pSubArbolIzq = null,
                 CArbolBB pSubArbolDer = null)
 {
     Raiz        = pRaiz;
     SubArbolIzq = pSubArbolIzq;
     SubArbolDer = pSubArbolDer;
 }
Example #4
0
File: sumas.cs Project: hvzzzz/-
 static void DIR(CArbolBB arbol)
 {
     if (arbol.Raiz != null)
     {
         if (arbol.SubArbolDer != null)
         {
             DIR(arbol.SubArbolDer);
         }
         if (arbol.SubArbolIzq != null)
         {
             DIR(arbol.SubArbolIzq);
         }
         ans = ans + Convert.ToInt64(arbol.Raiz);
     }
 }
Example #5
0
File: sumas.cs Project: hvzzzz/-
 static void RDI(CArbolBB arbol)
 {
     if (arbol.Raiz != null)
     {
         ans = ans + Convert.ToInt64(arbol.Raiz);
         if (arbol.SubArbolDer != null)
         {
             RDI(arbol.SubArbolDer);
         }
         if (arbol.SubArbolIzq != null)
         {
             RDI(arbol.SubArbolIzq);
         }
     }
 }
Example #6
0
File: sumas.cs Project: hvzzzz/-
 static void PosOrden(CArbolBB arbol)
 {
     if (arbol.Raiz != null)
     {
         // -- procesar hijo izquierdo
         if (arbol.SubArbolIzq != null)
         {
             PosOrden(arbol.SubArbolIzq);
         }
         // -- procesar hijo derecho
         if (arbol.SubArbolDer != null)
         {
             PosOrden(arbol.SubArbolDer);
         }
         // -- procesar raiz
         ans = ans + Convert.ToInt64(arbol.Raiz);
     }
 }
Example #7
0
File: sumas.cs Project: hvzzzz/-
 static void InOrden(CArbolBB arbol)
 {
     if (arbol.Raiz != null)
     {
         // -- procesar hijo izquierdo
         if (arbol.SubArbolIzq != null)
         {
             InOrden(arbol.SubArbolIzq);
         }
         // -- procesar raiz(hazlo que quieras con la raiz)
         ans = ans + Convert.ToInt64(arbol.Raiz);
         // -- procesar hijo derecho
         if (arbol.SubArbolDer != null)
         {
             InOrden(arbol.SubArbolDer);
         }
     }
 }
Example #8
0
File: sumas.cs Project: hvzzzz/-
        static void Main(string[] args)
        {
            CArbolBB arbol = new CArbolBB();

            Agregar(arbol, "36");
            Agregar(arbol, "81");
            Agregar(arbol, "25");
            Agregar(arbol, "35");
            Agregar(arbol, "33");
            Agregar(arbol, "74");
            Agregar(arbol, "10");
            Agregar(arbol, "90");
            RDI(arbol);
            //DRI(arbol);
            //DIR(arbol);
            //PreOrden(arbol);
            //InOrden(arbol);
            //PosOrden(arbol);
            Console.WriteLine(ans);
        }
Example #9
0
File: sumas.cs Project: hvzzzz/-
 // ----------------------------------------------------------------------------
 public virtual void Eliminar(Object pRaiz)
 {
     if (EsVacio())
     {
         Console.WriteLine("ERROR. Elemento no encontrado...");
     }
     else
     {
         // ----- Verificar si la raiz es el elemento que se desea eliminar
         if (pRaiz.Equals(Raiz))
         {
             // Si no tiene hijos, eliminar la raiz o una hoja
             if (SubArbolIzq == null && SubArbolDer == null)
             {
                 Raiz = null;
             }
             else // árbol tiene por lo menos un hijo
             if (SubArbolIzq == null)     // ----- Solo tiene hijo derecho
             {
                 Raiz        = SubArbolDer.Raiz;
                 SubArbolIzq = SubArbolDer.SubArbolIzq;
                 SubArbolDer = SubArbolDer.SubArbolDer;
             }
             else
             if (SubArbolDer == null)         // ----- Solo tiene hijo izquierdo
             {
                 Raiz        = SubArbolIzq.Raiz;
                 SubArbolDer = SubArbolIzq.SubArbolDer;
                 SubArbolIzq = SubArbolIzq.SubArbolIzq;
             }
             else         // Tiene ambos hijos
             {
                 Raiz = SubArbolDer.Minimo();
                 SubArbolDer.Eliminar(Raiz);
             }
         }
         else
         // ----- Verificar si el elemento a eliminar esta en el hijo Izq
         if (pRaiz.ToString().CompareTo(Raiz.ToString()) < 0)
         {
             if (SubArbolIzq != null)
             {
                 SubArbolIzq.Eliminar(pRaiz);
             }
         }
         else
         // ----- Elemento a eliminar esta en el hijo Der
         if (SubArbolDer != null)
         {
             SubArbolDer.Eliminar(pRaiz);
         }
         // Verificar si los hijos son hojas vacias
         if (SubArbolIzq != null && SubArbolIzq.EsVacio())
         {
             SubArbolIzq = null;
         }
         if (SubArbolDer != null && SubArbolDer.EsVacio())
         {
             SubArbolDer = null;
         }
     }
 }