Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Online C compiler to run C program online
- #include <stdio.h>
- #include <stdlib.h>
- #define true 1
- typedef struct strct_node node;
- typedef node tree;
- struct strct_node {
- int info;
- node *dir;
- node *esq;
- };
- struct caminho {
- int tamanho;
- node **lista;
- };
- // inicializa uma árvore de modo que fique evidente que esta árvore está vazia.
- void inicializar(tree **arv) {
- *arv = malloc(sizeof (tree));
- (*arv)->dir = (node *) -1;
- (*arv)->esq = (node *) -1;
- }
- node *criar_nodo(int conteudo) {
- node *nodo = malloc(sizeof (nodo));
- nodo->esq = NULL;
- nodo->dir = NULL;
- nodo->info = conteudo;
- }
- struct caminho caminhar(tree *arv) {
- static int layer = 0;
- static int qnt_nodos = 0;
- static node **vetor = NULL;
- node *walker = arv;
- if (vetor == NULL || !layer)
- vetor = malloc(1);
- // RECURSÃO -----------------------------------------------------
- layer++;
- if (walker->esq != NULL) {
- caminhar(walker->esq);
- }
- qnt_nodos++;
- vetor = realloc(vetor, qnt_nodos * (sizeof (node *)));
- vetor[qnt_nodos - 1] = walker;
- if (walker->dir != NULL) {
- caminhar(walker->dir);
- }
- layer--;
- // --------------------------------------------------------------
- if (!layer) {
- struct caminho retorno;
- retorno.tamanho = qnt_nodos;
- retorno.lista = vetor;
- qnt_nodos = 0;
- return retorno;
- }
- }
- void imprimir(tree *arvore) {
- struct caminho trilha = caminhar(arvore);
- printf("%i", trilha.lista[0]->info);
- for (int i = 1; i < trilha.tamanho; i++)
- printf(" - %i", trilha.lista[i]->info);
- puts("");
- }
- void inserir(int valor, tree *arv) {
- // árvore vazia
- if (arv->dir == (node *) -1 && arv->esq == (node *) -1) {
- arv->info = valor;
- arv->esq = NULL;
- arv->dir = NULL;
- return;
- }
- node *walker = arv;
- while (true) {
- if (walker->info > valor) {
- if (walker->esq == NULL) {
- walker->esq = criar_nodo(valor);
- return;
- }
- walker = walker->esq;
- }
- if (walker->info <= valor) {
- if (walker->dir == NULL) {
- walker->dir = criar_nodo(valor);
- return;
- }
- walker = walker->dir;
- }
- }
- }
- int comparar_arvores(tree *arv1, tree *arv2) {
- struct caminho trilha1 = caminhar(arv1);
- struct caminho trilha2 = caminhar(arv2);
- int teste1, teste2;
- if (trilha1.tamanho != trilha2.tamanho)
- return 0;
- for (int i = 0; i < trilha1.tamanho; i++) {
- teste1 = trilha1.lista[i]->info;
- teste2 = trilha2.lista[i]->info;
- if (trilha1.lista[i]->info != trilha2.lista[i]->info)
- return 0;
- }
- return 1;
- }
- // verifica se a árvore segue, de fato, a estrutura de uma árvore binária.
- int check_arvore_crescente(tree *arv) {
- struct caminho trilha = caminhar(arv);
- for (int i = 1; i < trilha.tamanho; i++) {
- if (trilha.lista[i - 1]->info > trilha.lista[i]->info)
- return 0;
- }
- return 1;
- }
- void imprimir_arvore_por_altura(tree *arv) {
- static int layer = 0;
- node *walker = arv;
- // árvore vazia
- if (walker == NULL || (arv->dir == (node *) -1 && arv->esq == (node *) -1))
- puts("Árvore vazia, nada a exibir.");
- if (!layer)
- printf("Nodos e seus níveis: ");
- // RECURSÃO -----------------------------------------------------
- layer++;
- if (walker->esq != NULL)
- imprimir_arvore_por_altura(walker->esq);
- printf("\t%i (niv: %i)", walker->info, layer);
- if (walker->dir != NULL)
- imprimir_arvore_por_altura(walker->dir);
- layer--;
- // --------------------------------------------------------------
- if (!layer)
- puts("");
- }
- node *encontrar_nodo(int valor, tree *arv) {
- node *walker = arv;
- // árvore vazia
- if (walker == NULL || (arv->dir == (node *) -1 && arv->esq == (node *) -1))
- return NULL;
- // BUSCA
- while (true) {
- if (walker->info > valor) {
- if (walker->esq == NULL)
- return NULL;
- walker = walker->esq;
- }
- else if (walker->info < valor) {
- if (walker->dir == NULL)
- return NULL;
- walker = walker->dir;
- }
- else
- return walker;
- }
- }
- void obter_caminho(tree *arv, int v1, int v2) {
- struct caminho trilha = caminhar(arv);
- int *percurso = malloc(1);
- int compr = 0;
- node *tracker = encontrar_nodo(v1, arv);
- while (true) {
- if (tracker->info > v2) {
- if (tracker->esq == NULL) {
- puts("Não há caminho entre estes dois valores.");
- return;
- }
- compr++;
- percurso = realloc(percurso, compr * sizeof (int));
- percurso[compr - 1] = tracker->info;
- tracker = tracker->esq;
- }
- if (tracker->info < v2) {
- if (tracker->dir == NULL) {
- puts("Não há caminho entre estes dois valores.");
- return;
- }
- compr++;
- percurso = realloc(percurso, compr * sizeof (int));
- percurso[compr - 1] = tracker->info;
- tracker = tracker->dir;
- }
- if (tracker->info == v2) {
- printf("Caminho de %i para %i:\n", v1, v2);
- for (int i = 0; i < compr; i++)
- printf("%i -> ", percurso[i]);
- printf("%i", v2);
- return;
- }
- }
- }
- int main() {
- tree *arvore;
- inicializar(&arvore);
- inserir(5, arvore);
- inserir(11, arvore);
- inserir(3, arvore);
- inserir(2, arvore);
- inserir(13, arvore);
- inserir(7, arvore);
- inserir(12, arvore);
- inserir(14, arvore);
- inserir(23, arvore);
- inserir(6, arvore);
- imprimir(arvore);
- tree *arvore2;
- inicializar(&arvore2);
- inserir(5, arvore2);
- inserir(12, arvore2);
- inserir(3, arvore2);
- inserir(2, arvore2);
- inserir(13, arvore2);
- inserir(7, arvore2);
- tree *arvore3;
- inicializar(&arvore3);
- inserir(5, arvore3);
- inserir(12, arvore3);
- inserir(3, arvore3);
- inserir(2, arvore3);
- inserir(13, arvore3);
- inserir(7, arvore3);
- imprimir(arvore2);
- imprimir(arvore3);
- if (comparar_arvores(arvore, arvore2)) puts("iguais");
- else puts("diferentes");
- if (comparar_arvores(arvore3, arvore2)) puts("iguais");
- else puts("diferentes");
- if (check_arvore_crescente(arvore)) puts("Funcionou");
- else puts("Não funcionou");
- node *busca = encontrar_nodo(7, arvore);
- printf("busca = %i\n", busca->info);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement