Skip to content

Commit 3b30c6b

Browse files
committedMay 21, 2021
levelSearchv2 is not passing at case 3.. idk why
1 parent 483fdbc commit 3b30c6b

File tree

3 files changed

+155
-147
lines changed

3 files changed

+155
-147
lines changed
 

‎binarySearchTree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//https://thehuxley.com/problem/547?quizId=6236
1+
//https://thehuxley.com/problem/546?quizId=6236
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>

‎heightTree.c

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//https://thehuxley.com/problem/546?quizId=6236
1+
//https://www.thehuxley.com/problem/547
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
@@ -59,35 +59,35 @@ int isEmpty(binaryTree* bt)
5959
{
6060
return (bt == NULL);
6161
}
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[]
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[]
7171

72-
num[*count] = bt->data;
73-
*count += 1;
72+
// num[*count] = bt->data;
73+
// *count += 1;
7474

75-
preOrder(bt->left,num,count,out);
76-
preOrder(bt->right,num,count,out);
75+
// preOrder(bt->left,num,count,out);
76+
// preOrder(bt->right,num,count,out);
7777

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-
}
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+
// }
9191
binaryTree* search(binaryTree* root, int target)
9292
{
9393
if (root == NULL || root->data == target)
@@ -145,12 +145,12 @@ int main()
145145
i++;
146146
}
147147

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);
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);
154154

155155
binaryTree* targetFound = search(bT,numberToBeFound);
156156
if(targetFound == NULL) printf("NAO ESTA NA ARVORE\n-1\n");

‎levelSearchv2.c

+121-113
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4+
//https://thehuxley.com/problem/949/oracle?quizId=6236
45
/*
56
6
67
10 2 1
@@ -10,7 +11,7 @@
1011
23 -1 -1
1112
51 -1 -1
1213
*/
13-
#define DEBUG if(0)
14+
#define DEBUG if(1)
1415
#define MAX_QUEUE 10000
1516
typedef struct _node
1617
{
@@ -21,11 +22,16 @@ typedef struct _node
2122
struct _node* right;
2223
}binaryTree;
2324

25+
typedef struct node
26+
{
27+
int item;
28+
struct node* next;
29+
}NODE;
2430
typedef struct _queue
2531
{
2632
//fazer estrutura em queue para criar lista encadeada somente para qeue e que funcione para visited[i]
27-
binaryTree* head;
28-
binaryTree* tail;
33+
NODE* head;
34+
NODE* tail;
2935
}Queue;
3036
Queue* initQ()
3137
{
@@ -34,20 +40,11 @@ Queue* initQ()
3440
new_q->head = NULL;
3541
return new_q;
3642
}
37-
Queue* isEmptyQ(Queue* q)
43+
44+
int isEmptyQ(Queue* q)
3845
{
39-
return (q == NULL);
46+
return (q->head == NULL);
4047
}
41-
// Queue* enQueue (Queue* q, binaryTree* bt)
42-
// {
43-
// binaryTree* newQ = (binaryTree*) malloc (sizeof(binaryTree));
44-
// newQ ->
45-
// if (isEmptyQ(q))
46-
// {
47-
// newQ->
48-
// }
49-
50-
// }
5148
int max (int a, int b)
5249
{
5350
return (a>b) ? a : b;
@@ -59,34 +56,62 @@ int height (binaryTree* bt)
5956
return 1 + max(height(bt->left),height(bt->right));
6057
}
6158
}
59+
NODE* createNode(int origin) //creates a "node" with value
60+
{
61+
NODE* adj = (NODE*) malloc (sizeof(NODE));
62+
adj->item = origin;
63+
adj->next = NULL;
64+
return adj;
65+
}
66+
int isEmptyList(NODE *head)
67+
{
68+
return (head == NULL);
69+
}
70+
void printLinkedList(NODE *head)
71+
{
72+
NODE* tmp = head;
73+
if(tmp == NULL) printf(" hea d=== null\t");
74+
while (tmp != NULL)
75+
{
76+
printf("[%d]-> ", tmp->item);
77+
tmp = tmp->next;
78+
}
79+
DEBUG printf(" fim da lista.\n");
80+
return;
81+
}
82+
NODE* addNode (NODE* head, int value)
83+
{ NODE* new = createNode(value);
84+
if(head == NULL)
85+
{
86+
head = new;
87+
//printf("new creado [%d]\n",new->item);
88+
}
89+
else
90+
{
91+
NODE* tmp = head;
92+
new->next = tmp;
93+
head = new;
94+
}
95+
return head;
96+
}
6297
int isEmpty(binaryTree* bt)
6398
{
6499
return (bt == NULL);
65100
}
66-
void preOrder(binaryTree* bt, int heights[][2], int* count)
101+
void preOrder(binaryTree* bt, NODE* height[])
67102
{
68103
if (!(isEmpty(bt)))
69104
{
70105
int level = bt->height;
71106
int vall = bt->value;
72107
int tmp;
73-
printf(" ( %d[%d] ",bt->value,bt->height);
74-
if(vall > heights[level][1]) //maior do que o maior
75-
{
76-
heights[level][0] = vall;
77-
printf("{h[%d][0] = %d}\n",level,vall);
78-
}
79-
else if (vall < heights[level][2]) //menor do que o menor
80-
{
81-
heights[level][1] = vall;
82-
printf("{h[%d][1] = %d}\n",level,vall);
83-
84-
}
85-
preOrder(bt->left,heights,count);
86-
preOrder(bt->right,heights,count);
87-
//printf(") ");
108+
DEBUG printf("%d[%d]\t",bt->value,bt->height);
109+
height[level] = addNode(height[level],vall);
110+
DEBUG printLinkedList(height[level]);
111+
preOrder(bt->left,height);
112+
preOrder(bt->right,height);
113+
//printf(")");
88114
//printf(") ");
89-
90115
}
91116
else
92117
{
@@ -102,29 +127,51 @@ binaryTree* createBinTree(int nodeNum, int value)
102127
new->height = 0;
103128
return new;
104129
}
105-
binaryTree* add(int nodeNum, int values[], int leftNumbers[], int rightNumbers[],binaryTree* bt)
130+
binaryTree* add(int nodeNum, int values[], int leftNumbers[], int rightNumbers[],binaryTree* bt,int NumOfNodes)
106131
{
107-
printf("\n---------- nó de numero #%d ---------\n",nodeNum);
132+
DEBUG printf("\n---------- nó de numero #%d ---------\n",nodeNum);
108133
binaryTree* newNode;
109-
if (nodeNum == -1)
134+
if (nodeNum == -1 || nodeNum > NumOfNodes-1)
110135
{
111136
newNode = NULL;
112137
}
113-
else
138+
else
114139
{
115140
if(nodeNum != -1)
116141
{
117142
newNode = createBinTree(nodeNum,values[nodeNum]);
118-
printf("criado com sucesso nodeNum = [%d]\n", nodeNum);
143+
DEBUG printf("criado com sucesso nodeNum = [%d]\n", nodeNum);
119144
}
120-
printf("indo para left\n");
121-
newNode->left = add(leftNumbers[nodeNum],values,leftNumbers,rightNumbers,newNode->left);
122-
printf("indo para right\n");
123-
newNode->right = add(rightNumbers[nodeNum],values,leftNumbers,rightNumbers,newNode->right);
145+
DEBUG printf("indo para left\n");
146+
newNode->left = add(leftNumbers[nodeNum],values,leftNumbers,rightNumbers,newNode->left,NumOfNodes);
147+
DEBUG printf("indo para right\n");
148+
newNode->right = add(rightNumbers[nodeNum],values,leftNumbers,rightNumbers,newNode->right,NumOfNodes);
124149
newNode->height = height(newNode);
125150
}
126151
return newNode;
127152
}
153+
int deQueue(Queue* q)
154+
{
155+
int deQueued = q->head->item;
156+
NODE* tmp = q->head->next;
157+
free(q->head);
158+
q->head = tmp;
159+
return deQueued;
160+
}
161+
void enQueue(Queue* q, int origin)
162+
{
163+
NODE* toBeQueued = createNode(origin);
164+
if (isEmptyQ(q))
165+
{
166+
q->head = toBeQueued;
167+
q->tail = toBeQueued;
168+
}
169+
else
170+
{
171+
q->tail->next = toBeQueued;
172+
q->tail = toBeQueued;
173+
}
174+
}
128175
binaryTree* iniT(){return NULL;}
129176
binaryTree* heighter(binaryTree* root, int height, int max, int min)
130177
{
@@ -133,66 +180,28 @@ binaryTree* heighter(binaryTree* root, int height, int max, int min)
133180
printf("Nivel 1 : Maior = %d, Menor = %d", max, min);
134181
}
135182
}
136-
// void bfs(graph* graph,int origin, int destination, int count)
137-
// {
138-
// for (int i = 0; i < MAX_SIZE; i++)
139-
// {
140-
// graph->visited[i] = 0;
141-
// }
142-
143-
// adj_list* tmp = graph->vertices[origin];
144-
// Queue* Queue = initQ();
145-
// int dequeued;
146-
// graph->visited[origin] = 1;
147-
// //DEBUG printf("\tgraph->visited[%d] = %d\n",origin,graph->visited[origin]);
148-
149-
// enQueue(Queue,origin);
150-
// printf("Iniciando busca em largura a partir de %d\n",origin);
151-
// int lasts[MAX_SIZE];
152-
// int found = 0;
153-
// while (!isEmptyQ(Queue))
154-
// {
155-
// //DEBUG printf("Fila não está vazia ainda.\n");
156-
// dequeued = deQueue(Queue);
157-
// tmp = graph->vertices[dequeued];
158-
// while (tmp != NULL)
159-
// {
160-
// // if (tmp->item < tmp->next->item && tmp->next != NULL)
161-
// //{
162-
// //tmp = tmp->next;
163-
// /* code that orders the reading */
164-
// //}
165-
166-
167-
168-
169-
170-
171-
172-
// //DEBUG printf("Visitando [%d]\n",tmp->item);
173-
// lasts[count] = tmp->item;
174-
// if(tmp->item == destination)
175-
// {
176-
// found = 1;
177-
// count++;
178-
// DEBUG printf("ENCONTRADO depois de %d passagens, ultimo foi [%d]\n",count,lasts[count-1]);
179-
// break;
180-
// }
181-
// printf("Iniciando busca em largura a partir de %d\n",tmp->item);
182-
// if(!graph->visited[tmp->item])
183-
// {
184-
// // DEBUG printf("\tgraph->visited[%d] = %d\n",tmp->item,graph->visited[tmp->item]);
185-
// graph->visited[tmp->item] = 1;
186-
// enQueue(Queue,tmp->item);
187-
// }
188-
// tmp = tmp->next;
189-
// count++;
190-
191-
// }
192-
// }
193-
// DEBUG printf("FIM DA BUSCA!\n");
194-
195-
// }
183+
void altureiro(int level, NODE* list, int maxH)
184+
{
185+
int trueLevel = maxH-level;
186+
int max, min;
187+
max = list->item;
188+
min = list->item;
189+
NODE* tmp = list;
190+
while (tmp != NULL)
191+
{
192+
if (tmp->item > max)
193+
{
194+
max = tmp->item;
195+
}
196+
if (tmp->item < min)
197+
{
198+
min = tmp->item;
199+
}
200+
tmp = tmp->next;
201+
}
202+
printf("Nivel %d: Maior = %d, Menor = %d\n",trueLevel,max,min);
203+
204+
}
196205
int main()
197206
{
198207
int numberOfNodes;
@@ -208,20 +217,19 @@ int main()
208217
}
209218
//every index will be associated with a node. it'll be the node number.
210219
binaryTree* bT = iniT();
211-
bT = add(0,nodeValue,leftNodeNumber,rightNodeNumber,bT);
220+
bT = add(0,nodeValue,leftNodeNumber,rightNodeNumber,bT,numberOfNodes);
212221
int count = 0;
213-
int heights[height(bT)+1][2];//heights[nivel] = {max,min};
214-
preOrder(bT,heights,&count);
215-
for (int i = 0; i < height(bT)+1; i++)
222+
int max_h = height(bT)+1;
223+
NODE* new[max_h];
224+
for (int i = 0; i < max_h; i++)
216225
{
217-
for (int j = 0; j < 2; j++)
218-
{
219-
printf("h[%d][%d] = [%d]\n",i,j,heights[i][j]);
220-
}
221-
226+
new[i] = NULL;
227+
}
228+
preOrder(bT,new);
229+
for (int i = max_h-1; i>= 0; i--)
230+
{
231+
DEBUG printf("indo para o nivel [%d]\n",i);
232+
altureiro(i,new[i],max_h);//passo o nível, passo os valores dos nós do nivel i
222233
}
223-
224-
printf("\n");
225-
//searcher(bT);
226234
return 0;
227235
}

0 commit comments

Comments
 (0)
Please sign in to comment.