Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct _Nodo{
- char nome[101];
- float nota_P1;
- struct _Nodo *esq, *dir;
- }Nodo;
- void cria_arvore(Nodo **arvore){
- *arvore = NULL;
- };
- /* Retorna 1 se foi possivel inserir. Caso contrario 0. */
- int insere_raiz(Nodo **arvore, char *nome_aluno, float nota){
- if(*arvore == NULL){
- Nodo *novo = (Nodo *) malloc(sizeof(Nodo));
- strcpy(novo->nome, nome_aluno);
- novo->nota_P1 = nota;
- novo->esq = novo->dir = NULL;
- *arvore = novo;
- return 1;
- }
- return 0;
- }
- int insere_dir(Nodo **arvore, char *pai, char *nome_aluno, float nota){
- if(*arvore == NULL)
- return 0;
- else if(strcmp((*arvore)->nome, pai) == 0){
- if((*arvore)->dir == NULL){
- Nodo *novo = (Nodo *) malloc(sizeof(Nodo));
- strcpy(novo->nome, nome_aluno);
- novo->nota_P1 = nota;
- novo->esq = novo->dir = NULL;
- (*arvore)->dir = novo;
- return 1;
- }else
- return 0;
- }
- return insere_dir(&(*arvore)->esq, pai, nome_aluno, nota) || insere_dir(&(*arvore)->dir, pai, nome_aluno, nota);
- }
- int main(){
- Nodo *arvore;
- cria_arvore(&arvore);
- if(insere_raiz(&arvore, "Matheus", 6.5) == 1)
- printf("Inserido com sucesso!\n");
- else
- printf("Erro: árvore ja possui raiz!\n");
- if(insere_dir(&arvore, "Matheus", "João", 9.5) == 1)
- printf("Inserido com sucesso!\n");
- else
- printf("Erro: árvore ja possui raiz!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment