Black Tiger III Posted January 5, 2008 at 03:07 PM Report Share #158047 Posted January 5, 2008 at 03:07 PM Boas tardes. O problema é o seguinte. Eu não tenho grande experiencia em C, com árvores binárias. E como tenho um trabalho em que sou obrigado a usar, surgiu-me um problemazinho. Criei código para que ele insira elementos numa árvore, o problema, é que ele não me guarda a cabeça da árvore digamos assim. Estou com um problema qualquer com os apontadores que não são o meu forte. Tentei também fazer recursivamente, mas também não me dá. Agradecia a quem pudesse ver o código e dar-me uma ajudinha. 😉 #include<stdio.h> #include<stdlib.h> #include<string.h> #include "arvore.h" Arvore * inserePr(int valor) { Arvore *tree; if((tree=(Arvore *)malloc(sizeof(Arvore)))==NULL) { perror("ERROR: Deu NULL\n"); exit(EXIT_FAILURE); } tree->valor=valor; tree->right=NULL; tree->left=NULL; return tree; } void acrescenta(Arvore *tree) { int valor/*,i=1*/; char buffer; puts("Introduza valor para adicionar á árvore."); scanf("%d",&valor); buffer=getchar(); Arvore *aux; aux=(Arvore *)malloc(sizeof(Arvore)); aux=tree; imprime(aux); aux=acrescenta1(tree, valor); acrescenta(aux); return; } /*Acrescentar elemento á árvore, está a funcionar, exceptuando que perde a cabeça da árvore*/ Arvore *acrescenta1(Arvore *tree, int valor) { Arvore *aux, *aux2; aux=aux2=(Arvore *)malloc(sizeof(Arvore)); aux=tree; puts("Chegou antes do if"); if(aux->valor<valor && aux->right==NULL) { aux2=inserePr(valor); aux->right=aux2; } else if(valor<=aux->valor && aux->left==NULL) { aux2=inserePr(valor); aux->left=aux2; } else { if(aux->valor<=valor && aux->right!=NULL) { puts("Chegou ao right"); aux=acrescenta1(aux->right, valor); } else { if(valor<aux->valor && aux->left!=NULL) { puts("Chegou ao left"); aux=acrescenta1(aux->left, valor); } } } return aux; //acrescenta(aux); } /*Usar apontadores*/ /*Arvore * acrescenta1(Arvore **praiz, int valor) { if(*praiz==NULL) {*praiz=inserePr(valor); return *praiz;} else if(((*praiz)->valor)>valor) acrescenta1(&(*praiz)->left, valor); else if(((*praiz)->valor)<valor) acrescenta1(&(*praiz)->right, valor); else return *praiz; }*/ /*função que imprime a árvore existente*/ void imprime(Arvore *tree) { if(tree!=NULL) { printf("<%d>",tree->valor); imprime(tree->left); imprime(tree->right); } return; } /*Começar o programa*/ int main() { int valor; char buffer; puts("Introduza valor para adicionar á árvore."); scanf("%d",&valor); buffer=getchar(); Arvore *tree; tree=(Arvore *)malloc(sizeof(Arvore)); tree=inserePr(valor); acrescenta(tree); return 1; } typedef struct AVL { int valor; struct AVL *right; struct AVL *left; }Arvore; Arvore *acrescenta1(Arvore *tree, int valor); void acrescenta(Arvore *tree); void imprime(Arvore *tree); Órale MI RAZA, MY BLOOD HOLMES BELONGS TO ME ÉSE... EENNNGGGGEEEENNNNNNNHHHHAAAARRRRRIIIIIIIIIIAAAAAAAINFORMATICASISTEMAS.... Link to comment Share on other sites More sharing options...
Rui Carlos Posted January 5, 2008 at 03:32 PM Report Share #158052 Posted January 5, 2008 at 03:32 PM Sugeria-te que criasses um módulo separado, onde tivesses apenas a funções que manipulam árvores. E estás a complicar de mais o código. O algoritmo de inserção resume-se a isto: insert(tree, new) if tree=null then tree=malloc tree->v=new tree->r=null tree->l=null else if tree->v > new then tree->l=insert(tree->l, new) else tree->r=insert(tree->r, new) return tree O problema no teu código deve estar aqui: aux=acrescenta1(aux->right, valor); Não seria aux->right=acrescenta1(aux->right, valor);? Rui Carlos Gonçalves Link to comment Share on other sites More sharing options...
Black Tiger III Posted January 5, 2008 at 03:35 PM Author Report Share #158053 Posted January 5, 2008 at 03:35 PM GRANDA RUI CARLOS... Bem se diz que uns olhos limpos, conseguem ver o que alguem que está á procura á horas não consegue... Nem tinha reparado. Que erro estupido :$ :$ :$ :$ mt obrigado Órale MI RAZA, MY BLOOD HOLMES BELONGS TO ME ÉSE... EENNNGGGGEEEENNNNNNNHHHHAAAARRRRRIIIIIIIIIIAAAAAAAINFORMATICASISTEMAS.... Link to comment Share on other sites More sharing options...
MX+ Posted January 9, 2008 at 04:53 PM Report Share #158899 Posted January 9, 2008 at 04:53 PM XD E concordo com o Rui Carlos. Modula isso. Porque tás a repetir código e tá a ficar confuso. Cumprimentos Link to comment Share on other sites More sharing options...
Black Tiger III Posted January 9, 2008 at 10:38 PM Author Report Share #159001 Posted January 9, 2008 at 10:38 PM Ah kd ao modular,eu tenho isto tudo separado, apenas juntei, e criei esta main para poderem compilar e dizer-me kual era o problema 🙂 Abraços e Obrigado Órale MI RAZA, MY BLOOD HOLMES BELONGS TO ME ÉSE... EENNNGGGGEEEENNNNNNNHHHHAAAARRRRRIIIIIIIIIIAAAAAAAINFORMATICASISTEMAS.... 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