1

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); 
} 
  • Unrelated to your problem, but please take some time to fix your indentation. Consistent indentation makes the code much easier to read, understand and maintain. Also please take some time to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Also please learn how to create a [mcve], with emphasis on the *minimal* part. – Some programmer dude Aug 15 '20 at 11:21
  • As for your problem, have you tried to perform the operations using a pencil and some paper? Draw small boxes for the nodes, and arrows for the links (pointers). Then erase and redraw as you perform operations. Also have you tried to use a debugger to step through your code statement by statement while monitoring variables and their values? Debugging is an important concept that is really a required skill for all programmers. – Some programmer dude Aug 15 '20 at 11:23
  • Lastly please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask] as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and please learn how to [edit] your questions to improve them. – Some programmer dude Aug 15 '20 at 11:24
  • `#include ` is bad, don't do that. `using namespace std;` is bad too, don't do that. – Eljay Aug 15 '20 at 12:09

1 Answers1

0

Thank you for your recommendation guys, didn't know about bits/stdc and using namespace are bad practice, i'll improve my identation.

I fixed my problem, i used a simple approach to insert the node and data, it servers it purpose, it's for a university assigment about data structure. If there's a student who can speak spanish for future reference i used the book "Programación en Java 2: Algoritmos, Estructura de datos y programación orientada a objetos" from Luis Joyanes Aguilar pages 548-589 it perfectly explains the approach how to insert data in a tree.

 struct nodo* insert(struct nodo* nodo, int llave) 
{ 
    if (nodo == NULL) return NodoNuevo(llave);

    if (llave < nodo->llave)
        nodo->izquierda = insert(nodo->izquierda, llave); 
    else if (llave > nodo->llave)
        nodo->derecha = insert(nodo->derecha, llave); 

    return nodo; 
}