the code is here: http://pastebin.com/9vswg7b0
here is an possible input:
Inserir Jose 30264221 15
Inserir Carlos 304 1
Inserir Maria 887745 7
Inserir Paulo -147 -8
Inserir Isabel 7845 38
Inserir Ana 4578 5
Inserir Danilo 5474 4
Inserir Jose 3641 36
Inserir Pedro 1234 1
Remover 4
Remover 1
Remover 12
Buscar 14
Buscar 5
Imprimir in
Fim
Inserir Tiago 1245 2
Remover 2
Imprimir pre
Fim
the output should be:
Conjunto #1
Cliente Jose cadastrado
Cliente Carlos ca开发者_StackOverflowdastrado
Cliente Maria cadastrado
Cliente Paulo cadastrado
Cliente Isabel cadastrado
Cliente Ana cadastrado
Cliente Danilo cadastrado
Cliente Jose cadastrado
Cliente com a chave 1 ja existe
Cliente Danilo removido
Cliente Carlos removido
Cliente com a chave 12 nao existe
Cliente nao encontrado
Nome: Ana, Telefone 4578
Visita in
Altura=0, Nome: Paulo, Telefone -147
Altura=1, Nome: Ana, Telefone 4578
Altura=0, Nome: Maria, Telefone 887745
Altura=2, Nome: Jose, Telefone 30264221
Altura=0, Nome: Jose, Telefone 3641
Altura=1, Nome: Isabel, Telefone 7845
Conjunto #2
Cliente Tiago cadastrado
Cliente Tiago removido
Visita pre
Banco de Dados vazio
The program is not working correctly, it ends in some sort of infinite loop. Whats wrong? I've puted some printf's for debbuging.
The main problem is that you are freeing the nodes right after inserting them into the tree...
arvore = insere(arvore, aux);
fprintf(fout, "Cliente %s cadastrado\n", aux->nome);
//printf("Cliente %s cadastrado\n", aux->nome);
//printf("%s\n", arvore->nome);
free(aux->nome);
free(aux);
To have a tree, you have to have memory for the nodes and that memory must persist. The infinite loop is caused by reusing old nodes that were incorrectly freed and eventually you get a node that points to itself and the traversal loop spins.
Update: ok, I found some more things...
For one thing, your remove function tried to free the node twice, once within the function and then again in main()
.
In another case, your code may have been OK but I didn't understand it, so I replaced the code in remover() that's called at the end of the conditionals when there is both a left and right link. See the snippet below. You should review this as your version may have been more correct.
} else {
p = raiz->esq;
p2 = raiz->dir;
free(raiz);
return insere(p, p2);
}
You will need to forward-declare insere()
or reverse its order with remover()
.
And finally, although the sample output clearly indicates that you will be asked to remove a key that doesn't exist, in fact you did not check for a NULL return from buscar()
which bombs out the program. The code I added doesn't precisely create the specified output but I'm sure you can fix that up:
} else if (strcmp(str, "Remover") == 0) {
fscanf(fin, "%d", &chave);
//printf("O Programa vai remover o no de chave %d\n", chave);
aux = buscar(arvore, chave);
char *s = aux == NULL ? "no such key" : aux->nome;
fprintf(fout, "Cliente %s removido\n", s);
//printf("Cliente %s removido\n", aux->nome);
arvore = remover(arvore, chave);
Update 2: my version of tree.c is here at pastebin.
精彩评论