#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Estrutura para o nó da árvore binária
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Funções para inserção da árvore binária
struct Node* newNode(int data) {
struct Node
* node
= (struct Node
*)malloc(sizeof(struct Node
)); node->data = data;
node->left = node->right = NULL;
return node;
}
struct Node* insert(struct Node* node, int data) {
if (node == NULL) return newNode(data);
if (data < node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
// Função de ordenação: Selection Sort
void selectionSort(int arr[], int n, int* comparisons) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
(*comparisons)++;
if (arr[j] < arr[min_idx])
min_idx = j;
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Função para gerar um vetor de inteiros únicos aleatórios
void generateRandomArray(int arr[], int size) {
int i, num, exists;
for (i = 0; i < size; i++) {
do {
exists = 0;
for (int j = 0; j < i; j++) {
if (arr[j] == num) {
exists = 1;
break;
}
}
} while (exists);
arr[i] = num;
}
}
// Função para imprimir a árvore binária em ordem
void printInOrder(struct Node* node) {
if (node != NULL) {
printInOrder(node->left); // Visita o filho à esquerda
printf("%d ", node
->data
); // Imprime o valor do nó printInOrder(node->right); // Visita o filho à direita
}
}
// Função para imprimir a árvore binária em pré-ordem
void printPreOrder(struct Node* node) {
if (node != NULL) {
printf("%d ", node
->data
); // Imprime o valor do nó printPreOrder(node->left); // Visita o filho à esquerda
printPreOrder(node->right); // Visita o filho à direita
}
}
int main() {
// Parte 1: Exibir nomes e matrículas
printf("Endryo Tavares - 202051054689\n"); printf("Pedro Guilherme - 202308418858\n"); printf("Pedro Vanderlei - 202402821865\n");
// Parte 2: Geração e Ordenação de Vetor
int arr[100];
int comparisons = 0;
generateRandomArray(arr, 100);
// Calcula o tempo de execução
clock_t start, end;
// Realizando Selection Sort
printf("\nUtilizando Selection Sort para ordenação...\n"); selectionSort(arr, 100, &comparisons);
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
// Parte 3: Apresentar resultados
for (int i = 0; i < 100; i++) {
}
printf("\nComparações realizadas: %d\n", comparisons
); printf("Tempo de execução: %f segundos\n", time_spent
);
// Parte 4: Construção da Árvore Binária
struct Node* root = NULL;
for (int i = 0; i < 100; i++) {
root = insert(root, arr[i]);
}
// Parte 5: Impressão da árvore binária
printf("\nÁrvore Binária em ordem: "); printInOrder(root);
// Parte 6: Impressão da árvore binária em pré-ordem
printf("Árvore Binária em pré-ordem: "); printPreOrder(root);
// Finalização
printf("\n\nAgradecemos a presença de todos!\n");
return 0;
}
CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiNpbmNsdWRlIDx0aW1lLmg+CgovLyBFc3RydXR1cmEgcGFyYSBvIG7DsyBkYSDDoXJ2b3JlIGJpbsOhcmlhCnN0cnVjdCBOb2RlIHsKICAgIGludCBkYXRhOwogICAgc3RydWN0IE5vZGUqIGxlZnQ7CiAgICBzdHJ1Y3QgTm9kZSogcmlnaHQ7Cn07CgovLyBGdW7Dp8O1ZXMgcGFyYSBpbnNlcsOnw6NvIGRhIMOhcnZvcmUgYmluw6FyaWEKc3RydWN0IE5vZGUqIG5ld05vZGUoaW50IGRhdGEpIHsKICAgIHN0cnVjdCBOb2RlKiBub2RlID0gKHN0cnVjdCBOb2RlKiltYWxsb2Moc2l6ZW9mKHN0cnVjdCBOb2RlKSk7CiAgICBub2RlLT5kYXRhID0gZGF0YTsKICAgIG5vZGUtPmxlZnQgPSBub2RlLT5yaWdodCA9IE5VTEw7CiAgICByZXR1cm4gbm9kZTsKfQoKc3RydWN0IE5vZGUqIGluc2VydChzdHJ1Y3QgTm9kZSogbm9kZSwgaW50IGRhdGEpIHsKICAgIGlmIChub2RlID09IE5VTEwpIHJldHVybiBuZXdOb2RlKGRhdGEpOwogICAgaWYgKGRhdGEgPCBub2RlLT5kYXRhKQogICAgICAgIG5vZGUtPmxlZnQgPSBpbnNlcnQobm9kZS0+bGVmdCwgZGF0YSk7CiAgICBlbHNlCiAgICAgICAgbm9kZS0+cmlnaHQgPSBpbnNlcnQobm9kZS0+cmlnaHQsIGRhdGEpOwogICAgcmV0dXJuIG5vZGU7Cn0KCi8vIEZ1bsOnw6NvIGRlIG9yZGVuYcOnw6NvOiBTZWxlY3Rpb24gU29ydAp2b2lkIHNlbGVjdGlvblNvcnQoaW50IGFycltdLCBpbnQgbiwgaW50KiBjb21wYXJpc29ucykgewogICAgaW50IGksIGosIG1pbl9pZHgsIHRlbXA7CiAgICBmb3IgKGkgPSAwOyBpIDwgbiAtIDE7IGkrKykgewogICAgICAgIG1pbl9pZHggPSBpOwogICAgICAgIGZvciAoaiA9IGkgKyAxOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgICgqY29tcGFyaXNvbnMpKys7CiAgICAgICAgICAgIGlmIChhcnJbal0gPCBhcnJbbWluX2lkeF0pCiAgICAgICAgICAgICAgICBtaW5faWR4ID0gajsKICAgICAgICB9CiAgICAgICAgdGVtcCA9IGFyclttaW5faWR4XTsKICAgICAgICBhcnJbbWluX2lkeF0gPSBhcnJbaV07CiAgICAgICAgYXJyW2ldID0gdGVtcDsKICAgIH0KfQoKLy8gRnVuw6fDo28gcGFyYSBnZXJhciB1bSB2ZXRvciBkZSBpbnRlaXJvcyDDum5pY29zIGFsZWF0w7NyaW9zCnZvaWQgZ2VuZXJhdGVSYW5kb21BcnJheShpbnQgYXJyW10sIGludCBzaXplKSB7CiAgICBpbnQgaSwgbnVtLCBleGlzdHM7CiAgICBmb3IgKGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgZG8gewogICAgICAgICAgICBleGlzdHMgPSAwOwogICAgICAgICAgICBudW0gPSByYW5kKCkgJSAxMDAwICsgMTsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBpOyBqKyspIHsKICAgICAgICAgICAgICAgIGlmIChhcnJbal0gPT0gbnVtKSB7CiAgICAgIC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