Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <math.h>
  4. # include <string.h>
  5.  
  6. //Estrutura da lista que contem as chaves de cada noh da arvore
  7. struct noh {
  8. char info; //Chave
  9. struct noh *prox; //Ponteiro para próxima chave
  10. };
  11.  
  12. //Estrutura da arvore B:
  13. struct arv {
  14. int folha; //Indica se o noh eh folha ou nao
  15. int n; //Numero atual de chaves no noh
  16. struct noh *chave; //Ponteiro para lista que armazena as chaves
  17. struct arv *filhos; //Ponteiros para os filhos
  18. };
  19.  
  20. typedef struct noh *PONT_NOH;
  21. typedef struct arv *PONT_ARV;
  22.  
  23. //Prototipos das funcoes
  24. FILE *abre_arquivo (); //Abre o arquivo
  25. PONT_ARV pesquisa_BTree (char, PONT_ARV arv);
  26. int insere_BTree (char, PONT_ARV *, int);
  27.  
  28. //==============================================================================
  29.  
  30.  
  31.  
  32. //==============================================================================
  33. int main()
  34. {
  35. int M, i=0, pos; //Auxiliares ]
  36.  
  37. FILE *arq = abre_arquivo();
  38. char caracter;
  39. PONT_ARV raiz = malloc(sizeof(struct arv));
  40. PONT_NOH filhos[M];
  41.  
  42. system ("color 5f");
  43. printf("\n\n\n =-=-= B - T R E E =-=-=\n\n"); //Imprimindo menu
  44.  
  45. i = 0;
  46. pos = 0;
  47. /*do
  48. { //Le caracter de um arquivo
  49. caracter = getc(arq);
  50. if (caracter != ',') {
  51. printf ("%c %d", caracter, i);
  52. //raiz->info[pos] = caracter;
  53. i++;
  54. }
  55. } while(caracter != EOF && i < 4); */
  56. do {
  57. caracter = getc(arq);
  58. raiz->chave->info = caracter;
  59. i++;
  60. } while (caracter != EOF && i<4);
  61. fclose(arq);
  62. printf ("\n\n\tEntre com o numero de chaves que tera cada noh: ");
  63. scanf ("%d", &M);
  64. for (i=0; i<M; i++)
  65. filhos[i] = NULL;
  66. while (raiz->chave)
  67. {
  68. printf("\n\nno da arvore: %c", raiz->chave->info);
  69. raiz->chave = raiz->chave->prox;
  70. }
  71. printf ("\n\n");
  72. system ("pause");
  73.  
  74. }
  75. //==============================================================================
  76.  
  77.  
  78.  
  79. //==============================================================================
  80.  
  81. //Funcao que abre um arquivo
  82. FILE *abre_arquivo () {
  83. FILE *arq;
  84. if(!(arq = fopen("teste.txt","r"))) //Abrindo arquivo
  85. {
  86. printf("\n\n\tErro ao abrir arquivo!!!\n\n");
  87. system ("pause");
  88. exit(1);
  89. }
  90. return arq;
  91. }
  92. //==============================================================================
  93.  
  94.  
  95.  
  96. //==============================================================================
  97.  
  98. //Funcao que busca um numero na arvore B
  99. PONT_ARV pesquisa_BTree(char chave, PONT_ARV arv) {
  100. int i=0;
  101. if (arv) {
  102. for (i = 1; ((i <= arv->n) && (arv->chave->info < chave)); i++)
  103. arv->chave = arv->chave->prox;
  104. if (i > (arv->n) || (arv->chave->info > chave))
  105. return pesquisa_BTree(chave, arv->filhos);
  106. else
  107. return arv;
  108. }
  109. else
  110. return NULL;
  111. }
  112. //==============================================================================
  113.  
  114.  
  115.  
  116. //==============================================================================
  117.  
  118. //Funcao que insere elementos em uma arvore B
  119. int insere_BTree (char chave, PONT_ARV *b, int M) {
  120. int i, aux;
  121. PONT_ARV temp;
  122.  
  123. if (!b) {
  124. b = malloc (sizeof(struct arv));
  125. (*b)->chave = malloc(sizeof(struct noh) * M);
  126. (*b)->folha = 1;
  127. (*b)->n = 1;
  128. (*b)->chave->info = chave;
  129. (*b)->filhos = malloc(sizeof(struct arv) * (M+1));
  130. return 1;
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment