#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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCi8vIEVzdHJ1dHVyYSBwYXJhIG8gbsOzIGRhIMOhcnZvcmUgYmluw6FyaWEKc3RydWN0IE5vZGUgewogICAgaW50IGRhdGE7CiAgICBzdHJ1Y3QgTm9kZSogbGVmdDsKICAgIHN0cnVjdCBOb2RlKiByaWdodDsKfTsKCi8vIEZ1bsOnw7VlcyBwYXJhIGluc2Vyw6fDo28gZGEgw6Fydm9yZSBiaW7DoXJpYQpzdHJ1Y3QgTm9kZSogbmV3Tm9kZShpbnQgZGF0YSkgewogICAgc3RydWN0IE5vZGUqIG5vZGUgPSAoc3RydWN0IE5vZGUqKW1hbGxvYyhzaXplb2Yoc3RydWN0IE5vZGUpKTsKICAgIG5vZGUtPmRhdGEgPSBkYXRhOwogICAgbm9kZS0+bGVmdCA9IG5vZGUtPnJpZ2h0ID0gTlVMTDsKICAgIHJldHVybiBub2RlOwp9CgpzdHJ1Y3QgTm9kZSogaW5zZXJ0KHN0cnVjdCBOb2RlKiBub2RlLCBpbnQgZGF0YSkgewogICAgaWYgKG5vZGUgPT0gTlVMTCkgcmV0dXJuIG5ld05vZGUoZGF0YSk7CiAgICBpZiAoZGF0YSA8IG5vZGUtPmRhdGEpCiAgICAgICAgbm9kZS0+bGVmdCA9IGluc2VydChub2RlLT5sZWZ0LCBkYXRhKTsKICAgIGVsc2UKICAgICAgICBub2RlLT5yaWdodCA9IGluc2VydChub2RlLT5yaWdodCwgZGF0YSk7CiAgICByZXR1cm4gbm9kZTsKfQoKLy8gRnVuw6fDo28gZGUgb3JkZW5hw6fDo286IFNlbGVjdGlvbiBTb3J0CnZvaWQgc2VsZWN0aW9uU29ydChpbnQgYXJyW10sIGludCBuLCBpbnQqIGNvbXBhcmlzb25zKSB7CiAgICBpbnQgaSwgaiwgbWluX2lkeCwgdGVtcDsKICAgIGZvciAoaSA9IDA7IGkgPCBuIC0gMTsgaSsrKSB7CiAgICAgICAgbWluX2lkeCA9IGk7CiAgICAgICAgZm9yIChqID0gaSArIDE7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgKCpjb21wYXJpc29ucykrKzsKICAgICAgICAgICAgaWYgKGFycltqXSA8IGFyclttaW5faWR4XSkKICAgICAgICAgICAgICAgIG1pbl9pZHggPSBqOwogICAgICAgIH0KICAgICAgICB0ZW1wID0gYXJyW21pbl9pZHhdOwogICAgICAgIGFyclttaW5faWR4XSA9IGFycltpXTsKICAgICAgICBhcnJbaV0gPSB0ZW1wOwogICAgfQp9CgovLyBGdW7Dp8OjbyBwYXJhIGdlcmFyIHVtIHZldG9yIGRlIGludGVpcm9zIMO6bmljb3MgYWxlYXTDs3Jpb3MKdm9pZCBnZW5lcmF0ZVJhbmRvbUFycmF5KGludCBhcnJbXSwgaW50IHNpemUpIHsKICAgIGludCBpLCBudW0sIGV4aXN0czsKICAgIGZvciAoaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBkbyB7CiAgICAgICAgICAgIGV4aXN0cyA9IDA7CiAgICAgICAgICAgIG51bSA9IHJhbmQoKSAlIDEwMDAgKyAxOwogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGk7IGorKykgewogICAgICAgICAgICAgICAgaWYgKGFycltqXSA9PSBudW0pIHsKICAgICAgICA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