i've been having problems deleting a node from this code, if i insert the number 12 and i try to delete it, it won't delete it, i tried debugging and it seems when it tries to delete, it goes the wrong part of the tree. But if i try delete a node that it's already inserted in main it will delete it, or i insert number 21 it can delete it, sorry if it's in spanish, if you request it i can translate it to english. The problem it's inside struct nodo* borrar_nodo(struct nodo* raiz, int llave, it just returns and doesn't find 12 or other numers i put in the tree. I tried the code from other threads with similar problems so i dont know where i'm wrong.
#include <bits/stdc++.h>
using namespace std;
struct nodo {
int llave;
struct nodo *izquierda, *derecha;
};
{
struct nodo* temp = new nodo;
temp->llave = llave;
temp->izquierda = temp->derecha = NULL;
return temp;
};
void Inorden(struct nodo* temp)
{
if (!temp)
return;
Inorden(temp->izquierda);
cout << temp->llave << " ";
Inorden(temp->derecha);
}
void preorden(struct nodo* temp)
{
if (!temp)
return;
cout << temp->llave << " ";
Inorden(temp->izquierda);
Inorden(temp->derecha);
}
void postorden(struct nodo* temp)
{
if (!temp)
return;
Inorden(temp->izquierda);
Inorden(temp->derecha);
cout << temp->llave << " ";
}
void insertar(nodo* temp, int llave)
{
queue<nodo*> fila;
fila.push(temp);
while (!fila.empty()) {
nodo* temp = fila.front();
fila.pop();
if (!temp->izquierda) {
temp->izquierda = NodoNuevo(llave);
break;
} else
fila.push(temp->izquierda);
if (!temp->derecha) {
temp->derecha = NodoNuevo(llave);
break;
} else
fila.push(temp->derecha);
}
}
struct nodo * valorMinimo(struct nodo* nodo)
{
struct nodo* actual = nodo;
while (actual && actual->izquierda != NULL)
actual = actual->izquierda;
return actual;
}
struct nodo* borrar_nodo(struct nodo* raiz, int llave)
{
if (raiz == NULL) return raiz;
else if (llave < raiz->llave)
raiz->izquierda = borrar_nodo(raiz->izquierda, llave);
else if (llave > raiz->llave)
raiz->derecha = borrar_nodo(raiz->derecha, llave);
else
{
if (raiz->izquierda == NULL)
{
struct nodo *temp = raiz->derecha;
free(raiz);
return temp;
}
else if (raiz->derecha == NULL)
{
struct nodo *temp = raiz->izquierda;
free(raiz);
return temp;
}
struct nodo* temp = valorMinimo(raiz->derecha);
raiz->llave = temp->llave;
raiz->derecha = borrar_nodo
(raiz->derecha, temp->llave);
}
return raiz;
}
int main()
{
int opcion;
int ins, borrar;
struct nodo* raiz = NodoNuevo(14);
raiz->izquierda = NodoNuevo(4);
raiz->izquierda->izquierda = NodoNuevo(3);
raiz->izquierda->derecha = NodoNuevo(9);
raiz->izquierda->derecha->izquierda = NodoNuevo(7);
raiz->derecha = NodoNuevo(15);
do
{
cout << "\nMenu de Opciones" << endl;
cout << "1. Crear tu nodo" << endl;
cout << "2. Eliminar nodo" << endl;
cout << "3. Mostrar nodos en: PREORDEN, INORDEN, POSTORDEN" << endl;
cout << "4. SALIR" << endl;
cin >> opcion;
switch (opcion) {
case 1:
cout << "\nQue nodo (edad) desea introducir al arbol: \n";
cin >> ins;
insertar(raiz, ins);
cout <<"\nNodo: " << ins << " insertado en el arbol";
break;
case 2: cout << "\nQue nodo (edad) desea eliminar de la siguiente lista: \n";
Inorden(raiz);
cout << "\n";
cin >> borrar;
borrar_nodo(raiz, borrar);
break;
case 3: cout << "Recorrido en inorden : \n";
Inorden(raiz);
cout << "\nRecorrido en preorden: \n";
preorden(raiz);
cout << "\nRecorrido en postorden : \n";
postorden(raiz);
break;
}
}while (opcion != 5);
}