Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. typedef struct binary_tree{
  8.     int item;
  9.     struct binary_tree *left;
  10.     struct binary_tree *right;
  11. }binary_tree;
  12.  
  13. binary_tree *create_binary_tree(int item, binary_tree *left, binary_tree *right) {
  14.     binary_tree *new_binary_tree = (binary_tree*) malloc(sizeof(binary_tree));
  15.     new_binary_tree->item = item;
  16.     new_binary_tree->left = left;
  17.     new_binary_tree->right = right;
  18.     return new_binary_tree;
  19. }
  20.  
  21. void search(binary_tree *arvore, int item, int *prof, int *achou)
  22. {
  23.     if (arvore == NULL || arvore->item == item) {
  24.         if(arvore->item == item) {
  25.             // printf("skdha\n");
  26.             *achou = 1;
  27.             *prof += 1;
  28.         }
  29.         return;
  30.     } else if (arvore->item > item) { // se o item que estou procurando for menor que o que estou, devo ir para esquerda.
  31.         // printf("Profundidade: %d", *prof);
  32.         *prof += 1;
  33.         search(arvore->left, item, prof, achou);
  34.     } else {
  35.         *prof += 1;
  36.         search(arvore->right, item, prof, achou);
  37.     }
  38. }
  39.  
  40. binary_tree *loop(binary_tree *arvore, int *i, char str[], int tam) {
  41.     if(str[*i] == '('){
  42.         if(str[*i + 1] == ')') {
  43.             *i += 2;
  44.             return NULL;
  45.         } else {
  46.             (*i)++;
  47.             //printf("*i = %d\n", *i);
  48.             int value = 0, j = 0;
  49.             while(str[*i + j] != '(') {
  50.                 j++; // vai guardar a quantidade de numeros que vao ser encontrados.
  51.             }
  52.             //printf("char_1: %c\n", str[*i]);
  53.             int a = j;
  54.             j = 0;
  55.             while(j != a) {
  56.                 //printf("char: %c\n", str[*i]);
  57.                 int b = str[*i] - '0'; // transforma um char em inteiro.
  58.                 value += pow(10,a - j - 1) * b;
  59.                 //printf("expressao: %lf, j = %d, a = %d, str = %d\n", pow(10,a - j - 1) * b, j, a, b);
  60.                 j++;
  61.                 (*i)++;
  62.             }
  63.             //printf("x = %d e *i = %d\n", x, *i);
  64.             arvore = create_binary_tree(value, NULL, NULL);
  65.             arvore->left = loop(arvore->left, i, str, tam);
  66.             arvore->right = loop(arvore->right, i, str, tam);
  67.         }
  68.     }
  69.     (*i)++;
  70.     return arvore;
  71. }
  72.  
  73. int main() {
  74.     binary_tree *arvore = NULL;
  75.     char str[99999];
  76.     scanf("%[^\n]s", str);
  77.     int tam = strlen(str);
  78.     int quero, prof = -1, achou = 0;
  79.     scanf("%d", &quero);
  80.     int i = 0;
  81.     arvore = loop(arvore, &i, str, tam);
  82.     search(arvore, quero, &prof, &achou);
  83.     if(achou != 0) {
  84.         printf("ESTA NA ARVORE\n%d\n", prof);
  85.     } else printf("NAO ESTA NA ARVORE\n%d\n", prof);
  86.  
  87.     // printf("%s\n", str);
  88.     // create_binary_tree(a, )
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement