Jump to content

[C]-Inserção de elemento em árvore


Black Tiger III

Recommended Posts

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

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);?

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.