Amigos sou novato no forúm, já procurei algums exemplos aqui no forum porém não tive muito sucesso. a pergunta é Atividade 3 Escreva um programa que leia várias informações de Alunos para uma lista encadeada. Em seguida crie uma árvore binária de busca a partir da lista. O programa deve permitir que se faça consultas às informações da lista a partir da busca realizada na árvore. tentei usar este código como base porém não está correto, alguém pode me ajudar? obrigado pela atenção, Abraço #include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void binsearchtree(struct node **,int);
void delatgivendata(struct node **,int);
inorder(struct node *);
preorder(struct node *);
postorder(struct node *);
void main()
{
int data,choice;
struct node *p;
p=NULL;
clrscr();
while(1)
{
printf("\n1.Add node to the binary search tree");
printf("\n2.Deletion at given data:");
printf("\n3.Exit");
printf("\nEnter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the data");
scanf("\n%d",&data);
binsearchtree(&p,data);
printf("\n The inorder traversals are:");
printf("\n");
inorder(p);
printf("\n The preorder traversals are:");
printf("\n");
preorder(p);
printf("\nThe post order traversals are:");
printf("\n");
postorder(p);
break;
case 2: printf("\n Please enter the data where you want deleted");
scanf("%d",&data);
delatgivendata(&p);
printf("\nThe inorder traversals are :");
inorder(p);
break;
case 3:exit(0);
}
}
}
void binsearchtree(struct node **q,int data)
{
struct node *temp,*r;
r=*q;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=data;
temp->left=NULL;
temp->right=NULL;
if(r==NULL)
{
*q=temp;
(*q)->left=temp->left;
(*q)->right=temp->right;
}
else
{
if(r->data==data)
printf("\nInvalid input\n");
while(1)
{
if(r->data>data)
{
if(r->left==NULL)
{
r->left=temp;
break;
}
r=r->left;
}
else
{
if(r->right==NULL)
{
r->right=temp;
break;
}
r=r->right;
}
}
}
}
void delatgivendata(struct node **q,int data)
{
struct node *r;
r=*q;
}
inorder(struct node *q)
{
struct node *r;
r=q;
if(r==NULL)
return 0;
else
{
inorder(r->left);
printf("%d\t",r->data);
inorder(r->right);
}
}
preorder(struct node *q)
{
struct node *r;
r=q;
if(r==NULL)
return 0;
else
{
printf("%d\t",r->data);
preorder(r->left);
preorder(r->right);
}
}
postorder(struct node *q)
{
struct node *r;
r=q;
if(r==NULL)
return 0;
else
{
postorder(r->left);
postorder(r->right);
printf("%d\t",r->data);
}
}