c - Binary Search Tree Madness -


i have write implementation of binary search tree can handle library's stock. reads text file books , add books tree in alphabetic order. have been fighting insertar() function code days , can't make work properly, receives pointer root of tree along data related book. if root null, inits node values entered in function , asings memory direction null node. problem is, doing locally , in end doesnt assigns it. can me correct specific function please?

functions , structs:

nodoarbol: node arbolbin: binary tree, has pointer root node , int number of elements initnodo: inits node, returns pointer node raiz: returns pointer root of binary tree clear,clear_aux: clears tree ingresar: insert() function , source of problem imprimir: rints elements of 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; } 

tnodoarbol *root;  insertar(root,a,b,2,1000,2014);  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);/*nodo pointer,node->der  illeagl*/         }         else if (result<0){             insertar (nodo->izq, nombre,autor,stock,valor,anno);/*the same error */         }     } } ----------------------------------------------------------------------------------------- declaration pointer, want through insertar()  change root, need use  insertar(&root,a,b,2,1000,2014), because root in insertar() not root in main() ,they have same value,we copy value of root(main)  root(insertar). --------------------------------------------------------------------------------------- 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);         }     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -