You are on page 1of 6

Superior de Tecnologia em Análise e Desenvolvimento de Sistemas EAD

NOME: RICARDO MONTEIRO DOS ANJOS


Matrícula: 1220401488

DISCIPLINA: ESTRUTURA DE DADOS


PROF: LUCIANO PINNA

AVA2

Niterói - RJ
2023
Segue abaixo o programa para manipular uma arvore binária como solicitado pela
empresa.

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insertNode(struct Node* root, int data) {


if (root == NULL)
return createNode(data);

if (data < root->data)


root->left = insertNode(root->left, data);
else if (data > root->data)
root->right = insertNode(root->right, data);

return root;
}

struct Node* findMinNode(struct Node* root) {


while (root->left != NULL)
root = root->left;
return root;
}

struct Node* deleteNode(struct Node* root, int data) {


if (root == NULL)
return root;

if (data < root->data)


root->left = deleteNode(root->left, data);
else if (data > root->data)
root->right = deleteNode(root->right, data);
else {

if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}

struct Node* temp = findMinNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);


}

return root;
}

void preOrderTraversal(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}

void inOrderTraversal(struct Node* root) {


if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}

void postOrderTraversal(struct Node* root) {


if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node* root = NULL;
int choice, value;

do {
// Menu de opções
printf("\n* * * MENU DE OPCOES * * *\n");
printf("1. Incluir no\n");
printf("2. Remover no\n");
printf("3. Buscar pre-ordem\n");
printf("4. Buscar em ordem\n");
printf("5. Buscar pos-ordem\n");
printf("0. Encerrar\n");
printf("Opcao: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Digite o valor a ser incluido: ");
scanf("%d", &value);
root = insertNode(root, value);
break;
case 2:
printf("Digite o valor a ser removido: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 3:
printf("Busca pre-ordem: ");
preOrderTraversal(root);
printf("\n");
break;
case 4:
printf("Busca em ordem: ");
inOrderTraversal(root);
printf("\n");
break;
case 5:
printf("Busca pos-ordem: ");
postOrderTraversal(root);
printf("\n");
break;
case 0:
printf("Encerrando o programa. \n");
break;
default:
printf("Opcao invalida. Tente novamente.\n");
}
} while (choice != 0);

return 0;
}
Referência:

BORIN, Vinicius Pozzobon. Estrutura de dados. 1. ed. São Paulo: Contentus, 2020.

E-book. Disponível em: https://plataforma.bvirtual.com.br. Acesso em: 15 nov. 2023.


KOFFMAN, Elliot B.; WOLFGANG, Paul A T. Objetos, Abstração, Estrutura de

Dados e Projeto Usando C++. São Paulo: Grupo GEN, 2008. E-book. ISBN 978-85-

216-2780-7. Disponível em: https://integrada.minhabiblioteca.com.br/#/books/978-

85-216-2780-7/. Acesso em: 10 nov. 2023.

You might also like