0

I copied this code (in spanish) http://www.elrincondelc.com/nuevorincon/index.php?pag=codigos&id=4 and wrote a new one.

This is my code:

#include <cstdlib>
#include <conio.h>
#include <iostream>

using namespace std;

struct nodoarbol {
    int dato;
    struct nodoarbol *izq;
    struct nodoarbol *der;
};

typedef nodoarbol Nodo;

typedef Nodo *Arbol;

void insertar(Arbol *, int);
void inorden(Arbol);
void postorden(Arbol);
void preorden(Arbol);

void insertar(Arbol *raiz, int nuevo){
    if (*raiz==NULL){
        *raiz = (Nodo *)malloc(sizeof(Nodo));
        if (*raiz != NULL){
            (*raiz)->dato=nuevo;
            (*raiz)->der=NULL;
            (*raiz)->izq=NULL;
        }
        else{
            cout<<"No hay memoria suficiente u ocurrio un error";
        }
    }
    else{
        if (nuevo < (*raiz)->dato)
            insertar( &((*raiz)->izq), nuevo  );
        else if (nuevo > (*raiz)->dato)
            insertar(&((*raiz)->der), nuevo);
    }
}//inseertar

void inorden(Arbol raiz){
    if (raiz != NULL){
        inorden(raiz->izq);
        cout << raiz->dato << " ";
        inorden(raiz->der);
    }
}

void preorden(Arbol raiz){
    if (raiz != NULL){
        cout<< raiz->dato << " ";
        preorden(raiz->izq);
        preorden(raiz->der);
    }
}

void postorden(Arbol raiz){
    if (raiz!=NULL){
        postorden(raiz->izq);
        postorden(raiz->der);
        cout<<raiz->dato<<" ";

    }
}

int main() {
    int i;
    i=0;
    int val;
    Arbol raiz = NULL;
    for (i=0; i<10; i++){
        cout<<"Inserte un numero";
        cin>>val;
        insertar( (raiz), val);
    }
    cout<<"\nPreorden\n";
    preorden(raiz);
    cout<<"\nIneorden\n";
    inorden(raiz);
    cout<<"\nPostorden\n";
    postorden(raiz);
    return 0;
}

I'm using netbeans 7.1.1, mingw32 compiler

This is the output:

make[2]: Leaving directory `/q/netbeans c++/NetBeansProjects/treek'
make[1]: Leaving directory `/q/netbeans c++/NetBeansProjects/treek'
main.cpp: In function 'int main()':
main.cpp:110:30: error: cannot convert 'Arbol {aka nodoarbol*}' to 'Nodo** {aka nodoarbol**}' for argument '1' to 'void insertar(Nodo**, int)'
make[2]: *** [build/Release/MinGW-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 11s)

I don't understand what's wrong since i just copied the code (and rewrite it to my own code). I'm really good in php, asp.net (vb) and other languages but c is a headche for me.

I've been struggling with this problem for about an hour. Could somebody tell me what could it be?

0

2 Answers 2

1
for (i=0; i<10; i++){
        cout<<"Inserte un numero";
        cin>>val;
        insertar( (raiz), val);
    }

Should (at least) be:

for (i=0; i<10; i++){
            cout<<"Inserte un numero";
            cin>>val;
            insertar( &raiz, val);
        }

Also, your insert-function overwrites existing nodes with

if (*raiz != NULL){
(*raiz)->dato=nuevo;
(*raiz)->der=NULL;
(*raiz)->izq=NULL;
}

Update: the above statement is incorrect. The if (*raiz != NULL){} was in a subblock conditional on (*raiz ==NULL).

An attempt (untested!) to remove the recursion.

void insertar(Arbol *raiz, int nuevo){

    while (*raiz) {
       if (nuevo == (*raiz)->dato) return;
       raiz =  (nuevo < (*raiz)->dato) 
               ? &(*raiz)->izq
               : &(*raiz)->der
               ;
        }

   *raiz = (Nodo *)malloc(sizeof(Nodo));
    if (*raiz != NULL){
        (*raiz)->dato=nuevo;
        (*raiz)->der=NULL;
        (*raiz)->izq=NULL;
        }
    else{
        cout<<"No hay memoria suficiente u ocurrio un error";
        }
    }

}//inseertar

Extra comment: the original code looks more like C than C++. (I don't do C++) In C, one would not cast the return from malloc, and preferably not hide pointers behind a typedef. In C++ (so I am told) one should not use malloc().

Sign up to request clarification or add additional context in comments.

Comments

0

main.cpp:110:30: error: cannot convert 'Arbol {aka nodoarbol*}' to 'Nodo** {aka nodoarbol*}' for argument '1' to 'void insertar(Nodo*, int)'

void insertar(Arbol *raiz, int nuevo) { ... }

Arbol raiz = NULL;

Replace:

- insertar( (raiz), val);

with:

+ insertar( &raiz, val);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.