|
| 1 | +//https://thehuxley.com/problem/546?quizId=6236 |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <math.h> |
| 6 | +#include <ctype.h> |
| 7 | +#define DEBUG if(0) |
| 8 | +typedef struct Node |
| 9 | +{ |
| 10 | + int data; |
| 11 | + int height; |
| 12 | + struct Node* left; |
| 13 | + struct Node* right; |
| 14 | +}binaryTree; |
| 15 | + |
| 16 | +binaryTree* iniT() |
| 17 | +{ |
| 18 | + return NULL; |
| 19 | +} |
| 20 | +binaryTree* createBinTree(int item, binaryTree* left, binaryTree* right) |
| 21 | +{ |
| 22 | + binaryTree* new = (binaryTree*) malloc(sizeof(binaryTree)); |
| 23 | + new->data = item; |
| 24 | + new->left = left; |
| 25 | + new->right = right; |
| 26 | + new->height = 0; |
| 27 | + return new; |
| 28 | +} |
| 29 | +int max (int a, int b) |
| 30 | +{ |
| 31 | + return (a>b) ? a : b; |
| 32 | +} |
| 33 | +int height (binaryTree* bt) |
| 34 | +{ |
| 35 | + if (bt == NULL) return -1; |
| 36 | + else{ |
| 37 | + return 1 + max(height(bt->left),height(bt->right)); |
| 38 | + } |
| 39 | +} |
| 40 | +binaryTree* add(int item, binaryTree* bt) |
| 41 | +{ |
| 42 | + if (bt == NULL) |
| 43 | + { |
| 44 | + bt = createBinTree(item,NULL,NULL); |
| 45 | + } |
| 46 | + else if (item < bt->data) //11>2 |
| 47 | + { |
| 48 | + bt->left = add(item, bt->left); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + bt->right = add(item, bt->right); |
| 53 | + } |
| 54 | + |
| 55 | + bt->height = height(bt); |
| 56 | + return bt; |
| 57 | +} |
| 58 | +int isEmpty(binaryTree* bt) |
| 59 | +{ |
| 60 | + return (bt == NULL); |
| 61 | +} |
| 62 | +void preOrder(binaryTree* bt,int num[], int *count, char out[]) |
| 63 | +{ |
| 64 | + char buffer[30]; |
| 65 | + if (!(isEmpty(bt))) |
| 66 | + { |
| 67 | + //sprintf gets what would be printed |
| 68 | + int n = sprintf(buffer, "(%d", bt->data); |
| 69 | + strcat(out,buffer); |
| 70 | + //adds bt->data to num[] |
| 71 | + |
| 72 | + num[*count] = bt->data; |
| 73 | + *count += 1; |
| 74 | + |
| 75 | + preOrder(bt->left,num,count,out); |
| 76 | + preOrder(bt->right,num,count,out); |
| 77 | + |
| 78 | + //cleans buffer and adds ) to out[] |
| 79 | + memset(buffer,0,strlen(buffer)); |
| 80 | + sprintf(buffer,")"); |
| 81 | + strcat(out,buffer); |
| 82 | + memset(buffer,0,strlen(buffer)); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + //cleans buffer and adds () to out[] |
| 87 | + sprintf(buffer,"()"); |
| 88 | + strcat(out,buffer); |
| 89 | + } |
| 90 | +} |
| 91 | +binaryTree* search(binaryTree* root, int target) |
| 92 | +{ |
| 93 | + if (root == NULL || root->data == target) |
| 94 | + { |
| 95 | + DEBUG printf("-- %d --\n",root->data); |
| 96 | + return root; |
| 97 | + } |
| 98 | + else if (root->data > target) |
| 99 | + { |
| 100 | + return search(root->left,target); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + return search(root->right,target); |
| 105 | + } |
| 106 | +} |
| 107 | +int main() |
| 108 | +{ |
| 109 | + //gets the input: numbers and symbols |
| 110 | + char input[10000]; |
| 111 | + int numberToBeFound; |
| 112 | + int t = 0; |
| 113 | + while (scanf("%c",&input[t]) != EOF) |
| 114 | + { |
| 115 | + if(input[t] == ' ') continue; |
| 116 | + if(input[t] == '\n') {input[t] = '\0'; break;} |
| 117 | + t++; |
| 118 | + } |
| 119 | + scanf("%d",&numberToBeFound); |
| 120 | + DEBUG printf("{{%s}}\n",input); |
| 121 | + DEBUG printf("a procura de [%d]\n",numberToBeFound); |
| 122 | + |
| 123 | + //gets only the inputTree from the input |
| 124 | + int len = strlen(input); //lenght of the input |
| 125 | + int numOfValues = 0; //number of ints in the input |
| 126 | + int inputTree[len]; //the tree from the input in the int form |
| 127 | + char *ptr = input; //creates a auxiliary ptr to transverse the string |
| 128 | + while (*ptr) { |
| 129 | + if (isdigit(*ptr)) { //if string + x is digit |
| 130 | + long val = strtol(ptr, &ptr, 10); //transforms the characters from string into base10 ints. |
| 131 | + DEBUG printf("%ld\n", val); |
| 132 | + inputTree[numOfValues] = val; |
| 133 | + numOfValues++; |
| 134 | + } else { |
| 135 | + ptr++; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + //creates a binaryTree from scratch using the input from user |
| 140 | + int i = 0; //uses i for creating the tree |
| 141 | + binaryTree* bT = iniT(); //initiliazes the binaryTree that will be right constructed. |
| 142 | + while (i < numOfValues) |
| 143 | + { |
| 144 | + bT = add(inputTree[i],bT); |
| 145 | + i++; |
| 146 | + } |
| 147 | + |
| 148 | + //gets an array formed by the rightTree in the preOrder variation |
| 149 | + int rightTree[10000]; //its the tree! (the right one :D in the int format) |
| 150 | + int ptr2 = 0; //auxiliary pointer to initialize preOrder |
| 151 | + char output[10000]; //its the tree! (the right one :D in the char format) |
| 152 | + preOrder(bT,rightTree,&ptr2,output); |
| 153 | + DEBUG printf("[%s]\n",output); |
| 154 | + |
| 155 | + binaryTree* targetFound = search(bT,numberToBeFound); |
| 156 | + if(targetFound == NULL) printf("NAO ESTA NA ARVORE\n-1\n"); |
| 157 | + else{ |
| 158 | + printf("ESTA NA ARVORE\n"); |
| 159 | + printf("%d\n",height(bT) - height(targetFound)); |
| 160 | + } |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +/* |
| 166 | + //gets an array formed by the rightTree in the preOrder variation |
| 167 | + int rightTree[10000]; //its the tree! (the right one :D in the int format) |
| 168 | + int ptr2 = 0; //auxiliary pointer to initialize preOrder |
| 169 | + char output[10000]; //its the tree! (the right one :D in the char format) |
| 170 | + preOrder(bT,rightTree,&ptr2,output); |
| 171 | + int flag = 0; |
| 172 | +
|
| 173 | + //compares both trees, if they are equal, it is valid. |
| 174 | + for (int i = 0; i < numOfValues; i++) //checks for the preOrder value print order |
| 175 | + { |
| 176 | + if(rightTree[i] != inputTree[i]) |
| 177 | + { |
| 178 | + flag += 1; |
| 179 | + break; |
| 180 | + } |
| 181 | + } |
| 182 | + int flag2 = strcmp(input,output); //checks for the preOrder print with the parenthesis as another reference. |
| 183 | + if (flag != 0 || flag2 != 0) |
| 184 | + { |
| 185 | + printf("FALSO\n"); |
| 186 | + return 0; |
| 187 | + } |
| 188 | + printf("VERDADEIRO\n"); |
| 189 | +*/ |
| 190 | + |
0 commit comments