tengo un problema con un arbol en C
Publicado: 15 May 2010, 18:00
hola, pues tengo que hacer un programa e implementar unas funciones...
y bueno todo funciona, pero cuando tengo que borrar todo el arbol, no se que pasa, explota como si no viera un limite
por favor una ayudita
la función problema tiene el nombre
/////////////////////PROBLEMA/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void destroy_nodes(node_t *node)
....
gracias
y bueno todo funciona, pero cuando tengo que borrar todo el arbol, no se que pasa, explota como si no viera un limite
por favor una ayudita
Código: Seleccionar todo
#include <duma.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node_s
{
char songtitle[256],interpreter[256];
struct node_s *parent,*left,*right;
} node_t;
node_t *create_node(char songtitle[],char interpreter[]);
node_t *insert_node(node_t *rootnode, char songtitle[], char interpreter[]);
void insert_help(node_t *parent, node_t **node_ptr, node_t *new_node);
void tree_print(node_t *node, long level);
void destroy_nodes(node_t *node);
node_t *search_node(node_t *node, char songtitle[]);
long count_nodes(node_t *rootnode);
long count_nodes_with_interpreter(node_t *rootnode, char interpreter[]);
int main()
{
node_t *playlist;
node_t *isthier=0;
//Datos para alimentar el programa
playlist=insert_node(playlist,"m","1");
playlist=insert_node(playlist,"b","2");
playlist=insert_node(playlist,"a","3");
playlist=insert_node(playlist,"d","4");
playlist=insert_node(playlist,"p","1");
playlist=insert_node(playlist,"o","3");
playlist=insert_node(playlist,"s","4");
// el imprime los datos asi volteando la pantalla de izquierda a derecha
tree_print(playlist,0);
// la variable isthier es solo para ver si encontro o no lo que buscaba
isthier = search_node(playlist,"b");
if(isthier) printf("Voila");
else printf("NO NO");
printf("\nNodes im Tree: %ld\n", count_nodes(playlist));
printf("\nHit im Tree: %ld\n", count_nodes_with_interpreter(playlist,"4"));
getchar();
// hasta aca funciona bien tooooodo funciona da numero de nodos y la cantidad de canciones por artista
// al llamar la siguiente funcion EXPLOTA AAAAAAAAAAAAAAAA.
destroy_nodes(playlist);
printf("Hello world!\n");
return 0;
}
node_t *create_node(char songtitle[],char interpreter[])
{
node_t *new_node=0;
if((new_node=malloc(sizeof(node_t))))
{
strcpy(new_node->songtitle,songtitle);
strcpy(new_node->interpreter,interpreter);
new_node->parent=0;
new_node->left=0;
new_node->right=0;
return new_node;
}
else
{
printf("no memory");
return 0;
}
}
node_t *insert_node(node_t *rootnode, char songtitle[], char interpreter[])
{
node_t *new_node=0;
if(new_node=create_node(songtitle,interpreter))
{
insert_help(0,&rootnode,new_node);
return rootnode;
}
else
{
printf("No memory!");
return 0;
}
}
void insert_help(node_t *parent, node_t **node_ptr, node_t *new_node)
{
node_t *node = *node_ptr;
if (node == 0)
{
new_node->parent = parent;
*node_ptr = new_node;
}
else
{
if(strncmp(new_node->songtitle,node->songtitle,255)<=0)
insert_help(node, &node->left, new_node);
else
insert_help(node, &node->right, new_node);
}
}
void tree_print(node_t *node, long level)
{
long i;
if (node->left) tree_print(node->left, level + 5);
for (i = 0; i < level; ++i) printf(" ");
printf("%s\n",node->songtitle);
if (node->right) tree_print(node->right, level + 5);
}
/////////////////////PROBLEMA/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void destroy_nodes(node_t *node)
{
if(node->left)
destroy_nodes(node->left);
if(node->right)
destroy_nodes(node->right);
free(node);
}
node_t *search_node ( node_t *rootnode , char songtitle[]){
if(rootnode==0)
return 0;
node_t *tmp=0;
static node_t *blatt=0;
if(rootnode->left){
tmp=search_node(rootnode->left, songtitle);
}
if(rootnode->right){
tmp=search_node(rootnode->right, songtitle);
}
if(strcasecmp(rootnode->songtitle, songtitle)==0)
blatt=rootnode;
return blatt;
}
long count_nodes(node_t *rootnode)
{
long counter=0;
if(rootnode->left)
counter+=count_nodes(rootnode->left);
if(rootnode->right)
counter+=count_nodes(rootnode->right);
counter++;
return counter;
}
long count_nodes_with_interpreter(node_t *rootnode, char interpreter[])
{
long counter=0;
if(rootnode->left)
counter+=count_nodes_with_interpreter(rootnode->left,interpreter);
if(rootnode->right)
counter+=count_nodes_with_interpreter(rootnode->right,interpreter);
if(strcmp(rootnode->interpreter,interpreter)==0)
counter++;
return counter;
}
la función problema tiene el nombre
/////////////////////PROBLEMA/////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void destroy_nodes(node_t *node)
....
gracias