Пример #1
0
 public Nodo(string nombre, Nodo izquierdo, Nodo derecho)
 {
     Nombre    = nombre;
     Izquierdo = izquierdo;
     Derecho   = derecho;
 }
Пример #2
0
Файл: Nodo.cs Проект: Jocagi/EDI
 public bool eliminarHijo(Nodo n)
 {
     return(Hijos.Remove(n));
 }
Пример #3
0
 public Nodo(Nodo NodoIzq, Object valor, Nodo NodoDer)
 {
     this.Dato          = valor;
     this.NodoIzquierdo = NodoIzq;
     this.NodoDerecho   = NodoDer;
 }
Пример #4
0
 public Nodo()
 {
     this.Left  = null;
     this.Right = null;
 }
Пример #5
0
 public ArbolBinario()
 {
     raiz = null;
 }
Пример #6
0
        private void RemoveAt(T dato, Nodo <T> currentRoot, Nodo <T> padre)
        {
            int p = 0;

            if (currentRoot is null)
            {
                throw new Exception("No encontrado");
            }
            var comparacion = comparador.Invoke(currentRoot.Value, dato);

            if (comparacion == 0)//ya encontro el que quiere borrar
            {
                if (currentRoot.EsHoja)
                {
                    if (padre is null)
                    {
                        root = null;
                        return;
                    }
                    if (padre.Left != null && padre.Right != null)
                    {
                        if (comparador(padre.Left.Value, dato) == 0)//si es el de la izquierda el que quiere borrar
                        {
                            padre.Left = null;
                        }
                        else
                        {
                            padre.Right = null;
                        }
                    }
                    else
                    {
                        if (padre.Left != null)
                        {
                            padre.Left = null;
                        }
                        else
                        {
                            padre.Right = null;
                        }
                    }
                }
                else if (currentRoot.TieneDosHijos)
                {
                    Nodo <T> padreSucesor = BuscarPadredelSucesor(currentRoot); //currentRoot el que quiere eliminar
                    Nodo <T> sucesor      = Sucesor(currentRoot);
                    if (padre == null)
                    {
                        padreSucesor.Right = sucesor.Left;
                        sucesor.Left       = currentRoot.Left;
                        sucesor.Right      = currentRoot.Right;
                        root = sucesor;
                    }
                    else
                    {
                        if (padreSucesor == currentRoot)
                        {
                            sucesor.Right = currentRoot.Right;
                            if (padre.Left != null && padre.Right != null)
                            {
                                if (comparador(padre.Left.Value, dato) == 0)
                                {
                                    padre.Left = sucesor;
                                }
                                else
                                {
                                    padre.Right = sucesor;
                                }
                            }
                            else
                            {
                                if (padre.Left != null)
                                {
                                    padre.Left = sucesor;
                                }
                                else
                                {
                                    padre.Right = sucesor;
                                }
                            }
                        }
                        else
                        {
                            padreSucesor.Right = sucesor.Left;
                            sucesor.Left       = currentRoot.Left;
                            sucesor.Right      = currentRoot.Right;

                            if (padre.Left != null && padre.Right != null)
                            {
                                if (comparador(padre.Left.Value, dato) == 0)
                                {
                                    padre.Left = sucesor;
                                }
                                else
                                {
                                    padre.Right = sucesor;
                                }
                            }
                            else
                            {
                                if (padre.Left != null)
                                {
                                    padre.Left = sucesor;
                                }
                                else
                                {
                                    padre.Right = sucesor;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (currentRoot.Left != null)
                    {
                        if (padre is null)
                        {
                            root = currentRoot.Left;
                        }
                        if (padre.Left != null && padre.Right != null)
                        {
                            if (comparador(padre.Left.Value, dato) == 0)//si es el de la izquierda el que quiere borrar
                            {
                                padre.Left = currentRoot.Left;
                            }
                            else
                            {
                                padre.Right = currentRoot.Left;
                            }
                        }
                        else
                        {
                            if (padre.Left != null)
                            {
                                padre.Left = currentRoot.Left;
                            }
                            else
                            {
                                padre.Right = currentRoot.Left;
                            }
                        }
                    }
                    else
                    {
                        if (padre is null)
                        {
                            root = currentRoot.Right;
                        }
                        if (padre.Left != null && padre.Right != null)
                        {
                            if (comparador(padre.Left.Value, dato) == 0)//si es el de la izquierda el que quiere borrar
                            {
                                padre.Left = currentRoot.Right;
                            }
                            else
                            {
                                padre.Right = currentRoot.Right;
                            }
                        }
                        else
                        {
                            if (padre.Left != null)
                            {
                                padre.Left = currentRoot.Right;
                            }
                            else
                            {
                                padre.Right = currentRoot.Right;
                            }
                        }
                    }
                }
                return;
            }

            if (comparacion > 0)
            {
                RemoveAt(dato, currentRoot.Left, currentRoot);                  //que busque en el hijo izquierdo
            }
            else if (comparacion < 0)
            {
                RemoveAt(dato, currentRoot.Right, currentRoot);
            }
        }
Пример #7
0
 private Nodo<T> Root;

        public void CreateTree(string newkey, T valor)
        {
            if (Root == null) //Si el arbol esta vacio lo crea
     {
         //Recibe un nuevo nodo, tipo Nodo, que contiene la informacion 
         Nodo<T> newnode = new Nodo<T>();
                newnode.Informacion = valor; //Se le asigna la informacion
                newnode.nombre = newkey;
                Root = newnode; //La raiz es igual al primer nodo
     }
            else
     {
                AddNode(valor, newkey, Root); //Si ya existe una raiz, se va a agregar nodo
     }
        }

        private void AddNode(T valor, string newkey, Nodo<T> nodo)
Пример #8
0
     {
                AddNode(valor, newkey, Root); //Si ya existe una raiz, se va a agregar nodo
     }
        }

        private void AddNode(T valor, string newkey, Nodo<T> nodo)
 {

            Nodo<T> nodoadd = nodo;
            if ((String.Compare(newkey, nodoadd.nombre, StringComparison.CurrentCultureIgnoreCase) == 1))//Si el nombre del nodo alfabeticamente es menor a la raiz, o al valor que recursivamente haya tomado, lo agrega a la iz)
     {
                if (nodoadd.Izquierda == null) //Si esta vacio a la izquierda lo agrega
         {
                    Nodo<T> nuevoNodo = new Nodo<T>();
                    nuevoNodo.Informacion = valor;
                    nuevoNodo.nombre = newkey;
                    nodoadd.Izquierda = nuevoNodo;
                }
                else
                {
                    AddNode(valor, newkey, nodoadd.Izquierda);//Si hay un nodo a la izquierda regresa a la funcion 
         }
            }
            else
            {
                if (nodoadd.Derecha == null)
                {
                    Nodo<T> nuevoNodo = new Nodo<T>();
                    nuevoNodo.Informacion = valor;
                    nuevoNodo.nombre = newkey;
                    nodoadd.Derecha = nuevoNodo;
                }
                else
                {
                    AddNode(valor, newkey, nodo.Derecha);
                }
            }
        }
Пример #9
0
 public arbol()
 {
     raiz = null;
 }