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)); } } }
private void rotation(AVLNode <T> root, bool direction) { bool son = false; //al ser falso es porque es hijo derecho AVLNode <T> aux; AVLNode <T> parent = root.getParent(); if (root != this.root) { if (parent.getLeft() == root) { son = true; } } if (direction) //si es verdadero será rotación izquierda { aux = root.getLeft(); // AVLNode <T> rightAux = aux.getRight(); root.setParent(aux); aux.setRight(root);// if (rightAux != null) { rightAux.setParent(root); } root.setLeft(rightAux);// aux.setParent(parent); if (root == this.root) { this.root = root.getParent(); this.root.setParent(null); } } else //rotación derecha { aux = root.getRight();// AVLNode <T> leftAux = aux.getLeft(); root.setParent(aux); aux.setLeft(root);// if (leftAux != null) { leftAux.setParent(root); } root.setRight(leftAux);// aux.setParent(parent); if (root == this.root) { this.root = root.getParent(); this.root.setParent(null); } } if (son) { parent.setLeft(aux); } else { parent.setRight(aux); } updateHeight(root); updateHeight(aux); }