Estructura de Datos
Enviado por Dietzzel • 20 de Enero de 2021 • Apuntes • 8.225 Palabras (33 Páginas) • 61 Visitas
public class ABB {
NodoABB root;
/***************************************************************************/
/******************************* INSERTAR **********************************/
public void insertar(int key) {
root = insertarR(key, root);
}
private NodoABB insertarR(int key, NodoABB nodo) {
if (nodo == null) {
return new NodoABB(key);
}
//Navega por el arbol dependiendo del valor de key
if (key < nodo.key) {
nodo.leftChild = insertarR(key, nodo.leftChild);
} else if(key > nodo.key){
nodo.rightChild = insertarR(key, nodo.rightChild);
}
return nodo;
}
/***************************************************************************/
/******************************* ELIMINAR **********************************/
public void eliminar(int key) {
root = borrar(root, key);
}
private NodoABB borrar(NodoABB nodo, int key) {
if (nodo == null){
System.out.println("Llave no encontrada");
return nodo;
}
if (key < nodo.key){
nodo.leftChild = borrar(nodo.leftChild, key);
}else if (key > nodo.key){
nodo.rightChild = borrar(nodo.rightChild, key);
}else {
if (nodo.leftChild == null && nodo.rightChild == null) {
nodo = null;
} else if (nodo.rightChild == null) {
NodoABB aux = nodo.leftChild;
while (aux.leftChild != null) {
aux = aux.leftChild;
}
int valor = aux.key;
borrar(root, valor);
nodo.key = valor;
} else {
NodoABB aux = nodo.rightChild;
while (aux.leftChild != null) {
aux = aux.leftChild;
}
int valor = aux.key;
borrar(root, valor);
nodo.key = valor;
}
}
return nodo;
}
/***************************************************************************/
/*************************** MENOR A MAYOR *********************************/
public void mostrarMenorAMayor() {
menorAMayor(root);
}
private void menorAMayor(NodoABB focusNode) {
if (focusNode != null) {
menorAMayor(focusNode.leftChild);
System.out.print(focusNode);
menorAMayor(focusNode.rightChild);
}
}
/***************************************************************************/
/*************************** MAYOR A MENOR *********************************/
public void mostrarMayorAMenor() {
mayorAMenor(root);
}
private void mayorAMenor(NodoABB focusNode) {
if (focusNode != null) {
mayorAMenor(focusNode.rightChild);
System.out.print(focusNode);
mayorAMenor(focusNode.leftChild);
}
}
/***************************************************************************/
/******************************* POST ORDER ********************************/
public void mostrarPostOrder() {
postOrder(root);
}
public void postOrder(NodoABB focusNode) {
if (focusNode != null) {
postOrder(focusNode.leftChild);
...