benkas Posted May 14, 2008 at 07:15 PM Report Share #185121 Posted May 14, 2008 at 07:15 PM Alguem me sabe dizer o que está de errado nesta função? é a função de pesquisa de uma rbt, quando faço pesquisar um valor, devolve sempre a raiz da rbt, mesmo que o valor nao exista na rbt. struct pesquisa *pesquisa_valor(struct rbt *f, char *v){ char val[strlen(v)]; strcpy(val,v); if (f->pes==NULL) return NULL; else if (!strcmp(val,(f->pes->chave))==0) return f->pes; else if (!strcmp(val,(f->pes->chave))<0) return pesquisa_valor(f->fe,v); else if (!strcmp(val,(f->pes->chave))>0) return pesquisa_valor(f->fd,v); } onde a struct da rbt é: struct rbt{ struct rbt *pai, *fe, *fd; enum Cor cor; struct pesquisa *pes; }; e a da pesquisa é; struct pesquisa{ char *chave; long endereco; }; Link to comment Share on other sites More sharing options...
TheDark Posted May 15, 2008 at 07:10 PM Report Share #185350 Posted May 15, 2008 at 07:10 PM Quando acrescentas um nó estás a inicializar pai, fe e fd a NULL? Desaparecido. Link to comment Share on other sites More sharing options...
benkas Posted May 16, 2008 at 11:25 AM Author Report Share #185452 Posted May 16, 2008 at 11:25 AM Nao; apenas tou a inialiçar a pesquisa.Vou esprimentar a inicializar esses tb. Obrigado pela dica Link to comment Share on other sites More sharing options...
benkas Posted May 16, 2008 at 12:35 PM Author Report Share #185468 Posted May 16, 2008 at 12:35 PM continua com o mesmo problema... Link to comment Share on other sites More sharing options...
Baderous Posted May 16, 2008 at 01:05 PM Report Share #185473 Posted May 16, 2008 at 01:05 PM Porque é que estás a meter um ! antes de cada strcmp? Link to comment Share on other sites More sharing options...
benkas Posted May 16, 2008 at 03:33 PM Author Report Share #185498 Posted May 16, 2008 at 03:33 PM Porque sem o ! da erro ao compilar. na função main() uso tambem o ! para comparar as opcções e está a funcionar perfeitamente Link to comment Share on other sites More sharing options...
Baderous Posted May 16, 2008 at 03:47 PM Report Share #185499 Posted May 16, 2008 at 03:47 PM if (!strcmp(val,(f->pes->chave))==0) O que estás aqui a dizer é o mesmo que: if (strcmp(val,(f->pes->chave))!=0) Como é que dá erro ao compilar? Estás a fazer #include <string.h>? Link to comment Share on other sites More sharing options...
benkas Posted May 16, 2008 at 03:57 PM Author Report Share #185501 Posted May 16, 2008 at 03:57 PM estou, se nao estivesse dava erro ao compilar Link to comment Share on other sites More sharing options...
TheDark Posted May 17, 2008 at 04:51 AM Report Share #185600 Posted May 17, 2008 at 04:51 AM Não tinha reparado no ! que o Baderous indicou. Assim faz sentido que te esteja a devolver a raiz da árvore sempre que procures por um valor diferente do que está na raiz. Já agora, se experimentares procurar pelo valor que tens na raiz da árvore, provavelmente vai devolver o 1º filho direito, porque strcmp do 1º filho com o seu próprio valor retorna 0; !0 = 1; 1>0, logo entra no 3º if. Entra de novo na função e compara; strcmp dá diferente de 0; !(valor diferente de 0) = 0; 0 == 0 entra no 1º if e retorna o filho actual (que é o direito, pela iteração anterior). Qual é exactamente o erro que dá quando retiras o '!' ? Desaparecido. Link to comment Share on other sites More sharing options...
benkas Posted May 17, 2008 at 02:01 PM Author Report Share #185649 Posted May 17, 2008 at 02:01 PM se meto o valor da raiz devolve o valor da raiz Link to comment Share on other sites More sharing options...
TheDark Posted May 17, 2008 at 06:03 PM Report Share #185687 Posted May 17, 2008 at 06:03 PM (Ainda bem que disse "provavelmente" 😄 ) Mas não respondeste à pergunta sobre o erro. Desaparecido. Link to comment Share on other sites More sharing options...
benkas Posted May 20, 2008 at 02:29 PM Author Report Share #186276 Posted May 20, 2008 at 02:29 PM o problema nao tava na funcao pesquisa. quando tava a inserir uma chave inseria a chave com o \n Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now