Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 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. binary_tree* add(binary_tree *arvore, int item) {
  22.     if (arvore == NULL) { // caso a arvore seja nula, é preciso criar ela.
  23.         arvore = create_binary_tree(item, NULL, NULL);
  24.     } else if (arvore->item > item) { // se o item que eu quero adicionar for menor, preciso adiciona-lo a esquerda.
  25.         arvore->left = add(arvore->left, item);
  26.     } else {
  27.         arvore->right = add(arvore->right, item);
  28.     }
  29.     return arvore;
  30. }
  31.  
  32. void print_pre_order(binary_tree *arvore) {
  33.     if (arvore != NULL) {
  34.         printf("%d", arvore->item);
  35.         print_pre_order(arvore->left); // in_order
  36.         print_pre_order(arvore->right); // pos_order
  37.     }
  38. }
  39.  
  40. void altura(binary_tree *arvore, int a, int *h){
  41.     if(arvore != NULL) {
  42.         if(arvore->left == NULL || arvore->right == NULL) a--;
  43.         altura(arvore->left, a + 1, h);
  44.         altura(arvore->right, a + 1, h);
  45.     }
  46.     else {
  47.         if(*h < a) *h = a;
  48.         return;
  49.     }
  50. }
  51.  
  52. int binary_search(binary_tree *arvore, int item, int *prof) {
  53.     *prof += 1;
  54.     if(arvore == NULL) {
  55.         // *prof += 1;
  56.         return 0;
  57.     } else if (arvore->item == item) {
  58.         // printf("skdha\n");
  59.         return 1;
  60.     } else if (arvore->item > item) { // se o item que estou procurando for menor que o que estou, devo ir para esquerda.
  61.         return binary_search(arvore->left, item, prof);
  62.     } else {
  63.         return binary_search(arvore->right, item, prof);
  64.     }
  65. }
  66.  
  67. void search_complet(binary_tree *arvore, int item, int *nivel, int *cont) {
  68.     if(arvore == NULL) return;
  69.     else {
  70.         printf("arvore -> item: %d\n", arvore->item);
  71.         if(arvore->item == item) {
  72.             *cont = *nivel;
  73.         }
  74.         *nivel += 1;
  75.         search_complet(arvore->left, item, nivel, cont);
  76.         // *nivel += 1;
  77.         search_complet(arvore->right, item, nivel, cont);
  78.     }
  79. }
  80.  
  81. binary_tree *loop(binary_tree *arvore, int *i, char str[], int tam) {
  82.     if(str[*i] == '('){
  83.         if(str[*i + 1] == ')') {
  84.             *i += 2;
  85.             return NULL;
  86.         } else {
  87.             (*i)++;
  88.             //printf("*i = %d\n", *i);
  89.             int value = 0, j = 0;
  90.             while(str[*i + j] != '(') {
  91.                 j++; // vai guardar a quantidade de numeros que vao ser encontrados.
  92.             }
  93.             //printf("char_1: %c\n", str[*i]);
  94.             int a = j;
  95.             j = 0;
  96.             while(j != a) {
  97.                 //printf("char: %c\n", str[*i]);
  98.                 int b = str[*i] - '0'; // transforma um char em inteiro.
  99.                 value += pow(10,a - j - 1) * b;
  100.                 //printf("expressao: %lf, j = %d, a = %d, str = %d\n", pow(10,a - j - 1) * b, j, a, b);
  101.                 j++;
  102.                 (*i)++;
  103.             }
  104.             //printf("x = %d e *i = %d\n", x, *i);
  105.             arvore = create_binary_tree(value, NULL, NULL);
  106.             arvore->left = loop(arvore->left, i, str, tam);
  107.             arvore->right = loop(arvore->right, i, str, tam);
  108.         }
  109.     }
  110.     (*i)++;
  111.     return arvore;
  112. }
  113.  
  114. int main() {
  115.     binary_tree *arvore = NULL;
  116.  
  117.     char str[99999];
  118.     scanf("%[^\n]s", str);
  119.  
  120.     int tam = strlen(str);
  121.  
  122.     int quero, prof = -1;
  123.     scanf("%d", &quero);
  124.     // printf("QUERO %d", quero);
  125.  
  126.     int i = 0;
  127.     arvore = loop(arvore, &i, str, tam);
  128.  
  129.     // int a = binary_search(arvore, quero, &prof);
  130.     // if(a != 0) {
  131.     //     printf("ESTA NA ARVORE\n%d\n", prof);
  132.     // } else {
  133.     //     printf("NAO ESTA NA ARVORE\n-1\n");
  134.     // }
  135.  
  136.     // int h = 0;
  137.     // altura(arvore,1, &h);
  138.     // printf("altura da arvore: %d\n", h);
  139.  
  140.     // print_pre_order(arvore);
  141.     int cont = -1, nivel = 0;
  142.     search_complet(arvore, quero, &nivel, &cont);
  143.     if(cont != -1) printf("Achou.\nProfundidade: %d\n", cont);
  144.     else printf("NOPS\n");
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement