Error inserting data in binary tree
Posted
by
chepe263
on Stack Overflow
See other posts from Stack Overflow
or by chepe263
Published on 2012-03-29T22:50:48Z
Indexed on
2012/03/29
23:29 UTC
Read the original article
Hit count: 211
c++
|binary-tree
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?
© Stack Overflow or respective owner