Esempio n. 1
0
        private void addElement(AVLNode <T> root, T element)
        {
            if (compareElements(element, root.getElement()) < 0)//x es menor que y
            {
                if (root.getLeft() == null)
                {
                    root.setLeft(new AVLNode <T>(element, root, null, null));
                    numberOfTreeNodes++;
                }
                else
                {
                    addElement(root.getLeft(), element);
                }
            }
            else if (compareElements(element, root.getElement()) > 0)//x es mayor que y
            {
                if (root.getRight() == null)
                {
                    root.setRight(new AVLNode <T>(element, root, null, null));
                    numberOfTreeNodes++;
                }
                else
                {
                    addElement(root.getRight(), element);
                }
            }

            balance(root);
            updateHeight(root);
        }
Esempio n. 2
0
 public void preOrder(AVLNode <T> root)
 {
     if (root != null)
     {
         Console.WriteLine(root.getElement());
         preOrder(root.getLeft());
         preOrder(root.getRight());
     }
 }
Esempio n. 3
0
        public T remove(AVLNode <T> root, K key)
        {
            if (root == null)
            {
                return(default(T));
            }
            else if (compareKeys(root.getElement(), key) == 0)
            {
                if (numberOfChildren(root) == 0)//borrar nodo sin hijos
                {
                    T           aux    = root.getElement();
                    AVLNode <T> parent = root.getParent();
                    if (root == this.root)
                    {
                        this.root = null;
                    }
                    else
                    {
                        if (parent.getLeft() == root)
                        {
                            parent.setLeft(null);
                        }
                        else
                        {
                            parent.setRight(null);
                        }
                        root = null;

                        //equilibrar el árbol
                        updateHeight(parent);
                        balance(parent);
                    }
                    numberOfTreeNodes--;
                    return(aux);
                }//borrar nodo sin hijos
                else if (numberOfChildren(root) == 1)//borrar nodo con 1 hijo
                {
                    T aux = root.getElement();
                    if (root == this.root) //si es la raiz
                    {
                        if (root.getLeft() != null)
                        {
                            this.root = root.getLeft();
                        }
                        else
                        {
                            this.root = root.getRight();
                        }
                        this.root.setParent(null);
                    }
                    else
                    {
                        AVLNode <T> parent = root.getParent();
                        AVLNode <T> son;
                        if (parent.getLeft() == root)
                        {
                            if (root.getLeft() != null)
                            {
                                son = root.getLeft();
                                son.setParent(parent);
                                parent.setLeft(son);
                            }
                            else
                            {
                                son = root.getRight();
                                son.setParent(parent);
                                parent.setLeft(son);
                            }
                        }
                        else
                        {
                            if (root.getLeft() != null)
                            {
                                son = root.getLeft();
                                son.setParent(parent);
                                parent.setRight(son);
                            }
                            else
                            {
                                son = root.getRight();
                                son.setParent(parent);
                                parent.setRight(son);
                            }
                        }

                        updateHeight(parent);
                        balance(parent);
                    }
                    numberOfTreeNodes--;
                    return(aux);
                }//borrar nodo con 1 hijo
                else
                {//El que sustituirá será el más derecho de los izquierdos
                    AVLNode <T> next = root.getLeft();
                    T           aux  = root.getElement();

                    while (next.getRight() != null)
                    {
                        next = next.getRight();
                    }
                    root.setElement(next.getElement());

                    if (next != root.getLeft())
                    {
                        AVLNode <T> father = next.getParent();
                        father.setRight(null);
                        updateHeight(father);
                        balance(father);
                    }
                    else
                    {
                        if (next.getLeft() != null)
                        {
                            root.setLeft(next.getLeft());
                            next.getLeft().setParent(root);
                        }
                        else
                        {
                            root.setLeft(null);
                        }

                        updateHeight(root);
                        balance(root);
                    }

                    numberOfTreeNodes--;
                    return(aux);
                }
            }
            else
            {
                if (compareKeys(root.getElement(), key) < 0)
                {
                    return(remove(root.getLeft(), key));
                }
                else
                {
                    return(remove(root.getRight(), key));
                }
            }
        }