Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <stdlib.h>
- # include <math.h>
- # include <string.h>
- //Estrutura da lista que contem as chaves de cada noh da arvore
- struct noh {
- char info; //Chave
- struct noh *prox; //Ponteiro para próxima chave
- };
- //Estrutura da arvore B:
- struct arv {
- int folha; //Indica se o noh eh folha ou nao
- int n; //Numero atual de chaves no noh
- struct noh *chave; //Ponteiro para lista que armazena as chaves
- struct arv *filhos; //Ponteiros para os filhos
- };
- typedef struct noh *PONT_NOH;
- typedef struct arv *PONT_ARV;
- //Prototipos das funcoes
- FILE *abre_arquivo (); //Abre o arquivo
- PONT_ARV pesquisa_BTree (char, PONT_ARV arv);
- int insere_BTree (char, PONT_ARV *, int);
- //==============================================================================
- //==============================================================================
- int main()
- {
- int M, i=0, pos; //Auxiliares ]
- FILE *arq = abre_arquivo();
- char caracter;
- PONT_ARV raiz = malloc(sizeof(struct arv));
- PONT_NOH filhos[M];
- system ("color 5f");
- printf("\n\n\n =-=-= B - T R E E =-=-=\n\n"); //Imprimindo menu
- i = 0;
- pos = 0;
- /*do
- { //Le caracter de um arquivo
- caracter = getc(arq);
- if (caracter != ',') {
- printf ("%c %d", caracter, i);
- //raiz->info[pos] = caracter;
- i++;
- }
- } while(caracter != EOF && i < 4); */
- do {
- caracter = getc(arq);
- raiz->chave->info = caracter;
- i++;
- } while (caracter != EOF && i<4);
- fclose(arq);
- printf ("\n\n\tEntre com o numero de chaves que tera cada noh: ");
- scanf ("%d", &M);
- for (i=0; i<M; i++)
- filhos[i] = NULL;
- while (raiz->chave)
- {
- printf("\n\nno da arvore: %c", raiz->chave->info);
- raiz->chave = raiz->chave->prox;
- }
- printf ("\n\n");
- system ("pause");
- }
- //==============================================================================
- //==============================================================================
- //Funcao que abre um arquivo
- FILE *abre_arquivo () {
- FILE *arq;
- if(!(arq = fopen("teste.txt","r"))) //Abrindo arquivo
- {
- printf("\n\n\tErro ao abrir arquivo!!!\n\n");
- system ("pause");
- exit(1);
- }
- return arq;
- }
- //==============================================================================
- //==============================================================================
- //Funcao que busca um numero na arvore B
- PONT_ARV pesquisa_BTree(char chave, PONT_ARV arv) {
- int i=0;
- if (arv) {
- for (i = 1; ((i <= arv->n) && (arv->chave->info < chave)); i++)
- arv->chave = arv->chave->prox;
- if (i > (arv->n) || (arv->chave->info > chave))
- return pesquisa_BTree(chave, arv->filhos);
- else
- return arv;
- }
- else
- return NULL;
- }
- //==============================================================================
- //==============================================================================
- //Funcao que insere elementos em uma arvore B
- int insere_BTree (char chave, PONT_ARV *b, int M) {
- int i, aux;
- PONT_ARV temp;
- if (!b) {
- b = malloc (sizeof(struct arv));
- (*b)->chave = malloc(sizeof(struct noh) * M);
- (*b)->folha = 1;
- (*b)->n = 1;
- (*b)->chave->info = chave;
- (*b)->filhos = malloc(sizeof(struct arv) * (M+1));
- return 1;
- }
- }
Add Comment
Please, Sign In to add comment