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