Hello,

I'm having difficulties with a problem. I made a binary tree with 2 child coming from one parent but now they ask me to create another child meaning, the parent has 3 child. I can manage to create the third child only in the first parent, after that the tree grows from the left and right child. Here is part of the code, the function Insertar is the one in charge of inserting. Any ideas will be welcome, thank you.

public class NodoT
    {
        public NodoT NodoIzquierdo;
        public NodoT NodoMedio;
        public int Informacion;
        public NodoT NodoDerecho;
        //Constructor
        public NodoT()
        {
            this.NodoIzquierdo = null;
            this.NodoMedio = null;
            this.Informacion = 0;
            this.NodoDerecho = null;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int Opcion = 0;
            NodoT Raiz = null;
            int Dato;
            do
            {
                Opcion = Menu();
                switch (Opcion)
                {
                    case 1:
                        Console.Write("Valor del Nuevo Nodo: ");
                        Dato = int.Parse(Console.ReadLine());
                        if (Raiz == null)
                        {
                            NodoT NuevoNodo = new NodoT();
                            NuevoNodo.Informacion = Dato;
                            Raiz = NuevoNodo;
                        }
                        else
                        {
                            Insertar(Raiz, Dato);
                        }
                        Console.Clear();
                        break;
                    case 2:
                        RecorridoPreorden(Raiz);
                        Console.WriteLine("Fin del Recorrido,...");
                        Console.ReadLine();
                        Console.Clear();
                        break;
                    case 3:
                        Console.Write("Teclee el Dato a Buscar: ");
                        Dato = int.Parse(Console.ReadLine());
                        if (Raiz != null)
                        {
                            BuscarNodo(Raiz, Dato);
                        }
                        else
                        {
                            Console.WriteLine("ERROR, Arbol Vacio....");
                        }
                        Console.Clear();
                        break;
                    case 4:
                        Console.Write("Teclee el Dato a Eliminar: ");
                        Dato = int.Parse(Console.ReadLine());
                        if (Raiz != null)
                        {
                            EliminarNodo(ref Raiz, Dato);
                        }
                        else
                        {
                            Console.WriteLine("ERROR, Arbol Vacio....");
                        }
                        Console.Clear();
                        break;
                    case 5:
                        Finalizar();
                        break;
                    default:
                        Console.WriteLine("ERROR, Opcion Invalida....");
                        Console.ReadLine();
                        Console.WriteLine("");
                        break;
                }
            } while (Opcion != 5);
        }
        static int Menu()
        {
            int Resultado = 0;
            Console.WriteLine("MENU DE ARBOLES");
            Console.WriteLine("");
            Console.WriteLine("1.- Registrar un Nuevo Nodo");
            Console.WriteLine("2.- Mostrar/Recorrer el Arbol");
            Console.WriteLine("3.- Buscar un Nodo");
            Console.WriteLine("4.- Eliminar un Nodo");
            Console.WriteLine("5.- Finalizar el Programa");
            Console.WriteLine("");
            Console.Write("Opcion: ");
            Resultado = int.Parse(Console.ReadLine());
            Console.WriteLine("");
            return Resultado;
        }
        //Insertar en un arbol binario
        static void Insertar(NodoT Raiz, int Dato)
        {

            if (Dato < Raiz.Informacion)
            {
                if (Raiz.NodoIzquierdo == null)
                {
                    NodoT NuevoNodo = new NodoT();
                    NuevoNodo.Informacion = Dato;
                    Raiz.NodoIzquierdo = NuevoNodo;
                }
                else
                {
                    if (Dato > Raiz.NodoIzquierdo.Informacion && Raiz.NodoMedio == null)
                    {
                        NodoT NuevoNodo = new NodoT();
                        NuevoNodo.Informacion = Dato;
                        Raiz.NodoMedio = NuevoNodo;
                    }

                    else
                    {
                        if (Dato < Raiz.NodoIzquierdo.Informacion && Raiz.NodoMedio == null)
                        {
                            NodoT NuevoNodo = new NodoT();
                            NuevoNodo.Informacion = Dato;
                            Raiz.NodoMedio = Raiz.NodoIzquierdo;
                            Raiz.NodoIzquierdo = NuevoNodo;
                        }

                        else
                        {//Llamada recursiva
                            Insertar(Raiz.NodoIzquierdo, Dato);
                        }
                    }
                }
            }
            else
            {//Buscar por el lado derecho
                if (Dato > Raiz.Informacion)
                {
                    if (Raiz.NodoDerecho == null)
                    {
                        NodoT NuevoNodo = new NodoT();
                        NuevoNodo.Informacion = Dato;
                        Raiz.NodoDerecho = NuevoNodo;
                    }
                    else
                    {
                        if (Dato < Raiz.NodoDerecho.Informacion && Raiz.NodoMedio == null)
                        {
                            NodoT NuevoNodo = new NodoT();
                            NuevoNodo.Informacion = Dato;
                            Raiz.NodoMedio = NuevoNodo;
                        }
                        else
                        {
                            if (Dato > Raiz.NodoDerecho.Informacion && Raiz.NodoMedio == null)
                            {
                                NodoT NuevoNodo = new NodoT();
                                NuevoNodo.Informacion = Dato;
                                Raiz.NodoMedio = Raiz.NodoDerecho;
                                Raiz.NodoDerecho = NuevoNodo;
                            }
                            else
                            {
                                //Llamada recursiva por el lado derecho
                                Insertar(Raiz.NodoDerecho, Dato);
                            }
                        }
                    }
                }
  else
                {
                    //El Nodo existe en el Arbol
                    Console.WriteLine("Nodo Existente, Imposible Insertar...");
                    Console.ReadLine();
                }


            }
        }

Thank you for your reply ddanbe, i used that exact article to create my tree and the different functions. The binary tree that I have is working fine the problem is adding one more child to the parent. I created a node called NodoMedio which is the third node to be added to the parent. I tried adding it with the function above "Insertar" but the third node is added only once, then the tree grows from the side. I can't manage to make it recursively like the left and right node.

I have this binary tree http://skrud.net/files/binary_tree_boat_race.png Now imagine between node A and node B I need to add another node. Now Root will have 3 nodes instead of two. I can manage to put the third node only in the first node (root) but when I add more nodes, the tree grows only in node A and B, and wont add more nodes to the third node that is in between.

If you have three nodes in the root or in the leaves, it stops being a binary three.
Look on the net for ternary three, B-three, B+three etc.

This article has been dead for over six months. Start a new discussion instead.