I have to write an implementation of a binary search tree that can handle a library's stock. It reads a text file with all the books and add the books to the tree in alphabetic order. I have been fighting with the Insertar() function code for DAYS and i can't make it work properly, it basically receives a pointer for the root of the tree along all the data related to the book. If the root is NULL, then it inits a node with all the values entered in the function and asings the memory direction to the null node. The problem is, its doing it locally and in the end it doesnt assigns it. Can somebody help me correct that specific function please?
Functions and Structs:
nodoArbol: The node
ArbolBin: Binary Tree, it has a pointer to a root node and an int with the number of elements
InitNodo: Inits the node, returns pointer to node
Raiz: Returns a pointer to the root of a Binary Tree
Clear,Clear_Aux: Clears a Tree
Ingresar: Insert() function and the source of the problem
Imprimir: rints the elements of a node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nodoArbol {
char nombre[51],autor[51];
int valor,stock,anno;
struct nodoArbol *der;
struct nodoArbol *izq;
} tNodoArbol;
typedef struct {
tNodoArbol *root;
int n;
} ArbolBin;
tNodoArbol* InitNodo(char *nombre,char *autor, int stock, int valor, int anno){
tNodoArbol *p;
p= (tNodoArbol*)malloc(sizeof(tNodoArbol));
strcpy(p->nombre, nombre);
strcpy(p->autor, autor);
p->stock = stock;
p->anno = anno;
p->valor = valor;
p->izq = NULL;
p->der = NULL;
return p;
}
tNodoArbol* Raiz(ArbolBin p){
return (&p)-> root;
}
void Init(ArbolBin *p){
p->root = NULL;
p->n = 0;
}
void clear_aux(tNodoArbol *nodo){
if (nodo == NULL){
return;
}
clear_aux(nodo->izq);
clear_aux(nodo->der);
free((void *) nodo);
}
void Clear(ArbolBin *p){
clear_aux(p->root);
p->root = NULL;
p->n = 0;
}
void Insertar (tNodoArbol *nodo, char *nombre,char *autor, int stock, int valor, int anno){
if (nodo == NULL){
nodo = (InitNodo(nombre,autor,stock,valor,anno));
}
else{
int result;
result = strcmp(nodo->nombre,nombre);
if (result>0){
Insertar (nodo->der, nombre,autor,stock,valor,anno);
}
else if (result<0){
Insertar (nodo->izq, nombre,autor,stock,valor,anno);
}
}
}
void Imprimir(tNodoArbol *nodo){
printf("Nombre:%s \n",nodo->nombre);
printf("Autor:%s \n",nodo->autor);
printf("Stock:%d \n",nodo->stock);
printf("Valor:%d \n",nodo->valor);
printf("anno:%d \n",nodo->anno);
}
int main(){
char a[50]= "holi",b[50] ="asdasdasd";
ArbolBin Tree;
tNodoArbol *Root;
Init(&Tree);
Root = Raiz(Tree);
Insertar(Root,a,b,2,1000,2014);
Imprimir(Root);
return 0;
}