Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- typedef struct binary_tree{
- int item;
- struct binary_tree *left;
- struct binary_tree *right;
- }binary_tree;
- binary_tree *create_binary_tree(int item, binary_tree *left, binary_tree *right) {
- binary_tree *new_binary_tree = (binary_tree*) malloc(sizeof(binary_tree));
- new_binary_tree->item = item;
- new_binary_tree->left = left;
- new_binary_tree->right = right;
- return new_binary_tree;
- }
- binary_tree* add(binary_tree *arvore, int item) {
- if (arvore == NULL) { // caso a arvore seja nula, é preciso criar ela.
- arvore = create_binary_tree(item, NULL, NULL);
- } else if (arvore->item > item) { // se o item que eu quero adicionar for menor, preciso adiciona-lo a esquerda.
- arvore->left = add(arvore->left, item);
- } else {
- arvore->right = add(arvore->right, item);
- }
- return arvore;
- }
- void print_pre_order(binary_tree *arvore) {
- if (arvore != NULL) {
- printf("%d", arvore->item);
- print_pre_order(arvore->left); // in_order
- print_pre_order(arvore->right); // pos_order
- }
- }
- void altura(binary_tree *arvore, int a, int *h){
- if(arvore != NULL) {
- if(arvore->left == NULL || arvore->right == NULL) a--;
- altura(arvore->left, a + 1, h);
- altura(arvore->right, a + 1, h);
- }
- else {
- if(*h < a) *h = a;
- return;
- }
- }
- int binary_search(binary_tree *arvore, int item, int *prof) {
- *prof += 1;
- if(arvore == NULL) {
- // *prof += 1;
- return 0;
- } else if (arvore->item == item) {
- // printf("skdha\n");
- return 1;
- } else if (arvore->item > item) { // se o item que estou procurando for menor que o que estou, devo ir para esquerda.
- return binary_search(arvore->left, item, prof);
- } else {
- return binary_search(arvore->right, item, prof);
- }
- }
- void search_complet(binary_tree *arvore, int item, int *nivel, int *cont) {
- if(arvore == NULL) return;
- else {
- printf("arvore -> item: %d\n", arvore->item);
- if(arvore->item == item) {
- *cont = *nivel;
- }
- *nivel += 1;
- search_complet(arvore->left, item, nivel, cont);
- // *nivel += 1;
- search_complet(arvore->right, item, nivel, cont);
- }
- }
- binary_tree *loop(binary_tree *arvore, int *i, char str[], int tam) {
- if(str[*i] == '('){
- if(str[*i + 1] == ')') {
- *i += 2;
- return NULL;
- } else {
- (*i)++;
- //printf("*i = %d\n", *i);
- int value = 0, j = 0;
- while(str[*i + j] != '(') {
- j++; // vai guardar a quantidade de numeros que vao ser encontrados.
- }
- //printf("char_1: %c\n", str[*i]);
- int a = j;
- j = 0;
- while(j != a) {
- //printf("char: %c\n", str[*i]);
- int b = str[*i] - '0'; // transforma um char em inteiro.
- value += pow(10,a - j - 1) * b;
- //printf("expressao: %lf, j = %d, a = %d, str = %d\n", pow(10,a - j - 1) * b, j, a, b);
- j++;
- (*i)++;
- }
- //printf("x = %d e *i = %d\n", x, *i);
- arvore = create_binary_tree(value, NULL, NULL);
- arvore->left = loop(arvore->left, i, str, tam);
- arvore->right = loop(arvore->right, i, str, tam);
- }
- }
- (*i)++;
- return arvore;
- }
- int main() {
- binary_tree *arvore = NULL;
- char str[99999];
- scanf("%[^\n]s", str);
- int tam = strlen(str);
- int quero, prof = -1;
- scanf("%d", &quero);
- // printf("QUERO %d", quero);
- int i = 0;
- arvore = loop(arvore, &i, str, tam);
- // int a = binary_search(arvore, quero, &prof);
- // if(a != 0) {
- // printf("ESTA NA ARVORE\n%d\n", prof);
- // } else {
- // printf("NAO ESTA NA ARVORE\n-1\n");
- // }
- // int h = 0;
- // altura(arvore,1, &h);
- // printf("altura da arvore: %d\n", h);
- // print_pre_order(arvore);
- int cont = -1, nivel = 0;
- search_complet(arvore, quero, &nivel, &cont);
- if(cont != -1) printf("Achou.\nProfundidade: %d\n", cont);
- else printf("NOPS\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement