#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef int (*_funCmp_t)(void *a, void *b);
typedef void (*_funPrint_t)(void *a);
///Arbol
typedef struct _nodo{
void *valor;
struct _nodo *iz;
struct _nodo *der;
}nodo_t;
///Estructura administrativa
typedef struct {
int sizeTipo; //Tamaño del dato a almacenar
nodo_t *raiz;
_funCmp_t funCmp; //Funcion de comparacion
_funPrint_t funPrint; //Funcion de impresion
}arbol_t;
int comparaEnteros(void *a, void *b){
return *((int *)a) - *((int *)b);
}
void imprimeEnteros(void *a){
}
arbol_t *arbolCreate(int sizeTipo, _funCmp_t funCmp, _funPrint_t funPrint){
arbol_t
*arbol
= (arbol_t
*)malloc(sizeof(arbol_t
));
arbol->sizeTipo = sizeTipo;
arbol->funCmp = funCmp;
arbol->funPrint = funPrint;
arbol->raiz = NULL;
return arbol;
}
nodo_t *creaNodo(arbol_t *arbol, void *valor, nodo_t *der, nodo_t *iz){
nodo_t
*nodo
= (nodo_t
*)malloc(sizeof(nodo_t
));
nodo->valor = valor;
nodo->der = der;
nodo->iz = iz;
return nodo;
}
nodo_t *arbolInsert(arbol_t *arbol, nodo_t *nodo, void *aIngresar){
if(!nodo)
return creaNodo(arbol, aIngresar, NULL, NULL);
if(arbol->funCmp(nodo->valor, aIngresar) > 0)
nodo->iz = arbolInsert(arbol, nodo->iz, aIngresar);
else if(arbol->funCmp(nodo->valor, aIngresar) < 0)
nodo->der = arbolInsert(arbol, nodo->der, aIngresar);
return nodo;
}
void printArbol(arbol_t *arbol, nodo_t *nodo){ //Preorden
if(nodo)
arbol->funPrint(nodo->valor);
if(nodo->iz)
printArbol(arbol, nodo->iz);
if(nodo->der)
printArbol(arbol, nodo->der);
}
int main(void){
arbol_t *arbol = arbolCreate(sizeof(int),comparaEnteros, imprimeEnteros);
int i = 5;
arbol->raiz = arbolInsert(arbol, arbol->raiz, (void *)&i);
i =4;
arbol->raiz = arbolInsert(arbol, arbol->raiz, (void *)&i);
i =3;
arbol->raiz = arbolInsert(arbol, arbol->raiz, (void *)&i);
i = 7;
arbol->raiz = arbolInsert(arbol, arbol->raiz, (void *)&i);
printArbol(arbol, arbol->raiz);
return 0;
}