Created
March 18, 2019 22:33
-
-
Save brun0xff/35bdb52ad951aa1f26f4a445d4cae316 to your computer and use it in GitHub Desktop.
árvore binária de busca e algumas funções básicas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct tipoNo { | |
int valor; | |
char op; | |
struct tipoNo *esq; | |
struct tipoNo *dir; | |
} tipoNo; | |
tipoNo *criaArvore (); | |
void insereArvore (tipoNo **a, int valor); | |
int menorArvore (tipoNo *a); | |
int maiorArvore (tipoNo *a); | |
int tamArvore (tipoNo *a); | |
int somaArvore (tipoNo *a); | |
int somaFolhasArvore (tipoNo *a); | |
int alturaArvore (tipoNo *a); | |
tipoNo *buscaArvore (tipoNo *a, int valor); | |
void removeArvore (tipoNo **a, int valor); | |
tipoNo *criaArvore () { | |
return NULL; | |
} | |
int main (void) { | |
tipoNo *arvore; | |
tipoNo *no; | |
arvore = criaArvore(); | |
insereArvore (&arvore, 12); | |
insereArvore (&arvore, 6); | |
insereArvore (&arvore, 18); | |
insereArvore (&arvore, 10); | |
insereArvore (&arvore, 15); | |
insereArvore (&arvore, 0); | |
insereArvore (&arvore, 30); | |
printf ("Menor elemento da árvore: %d\n", menorArvore(arvore)); | |
printf ("Maior elemento da árvore: %d\n", maiorArvore(arvore)); | |
printf ("Número de nós da árvore: %d\n", tamArvore(arvore)); | |
printf ("Soma dos nós da árvore: %d\n", somaArvore(arvore)); | |
printf ("Soma dos nós folha da árvore: %d\n", somaFolhasArvore(arvore)); | |
printf ("Altura da árvore: %d\n", alturaArvore(arvore)); | |
no = buscaArvore(arvore, 200); | |
if (no) { | |
printf ("Valor 20: %d\n", no->valor); | |
} else { | |
printf ("Valor não encontrado\n"); | |
} | |
return 0; | |
} | |
void insereArvore (tipoNo **a, int valor) { | |
if (*a == NULL) { | |
*a = (tipoNo *) malloc (sizeof(tipoNo)); | |
(*a)->valor = valor; | |
(*a)->esq = NULL; | |
(*a)->dir = NULL; | |
} | |
else if (valor < (*a)->valor) insereArvore(&(*a)->esq, valor); | |
else insereArvore(&(*a)->dir, valor); | |
} | |
int menorArvore (tipoNo *a) { | |
if (a==NULL) return -1; | |
if (a->esq!=NULL) return menorArvore(a->esq); | |
else return a->valor; | |
} | |
int maiorArvore (tipoNo *a) { | |
if (a==NULL) return -1; | |
if (a->dir!=NULL) return maiorArvore(a->dir); | |
else return a->valor; | |
} | |
int tamArvore (tipoNo *a) { | |
if (a==NULL) return 0; | |
else return 1 + tamArvore (a->esq) + tamArvore (a->dir); | |
} | |
int somaArvore (tipoNo *a) { | |
if (a==NULL) return 0; | |
else return a->valor + somaArvore (a->esq) + somaArvore (a->dir); | |
} | |
int somaFolhasArvore (tipoNo *a) { | |
if (a==NULL) return 0; | |
if ((a->esq==NULL) && (a->dir==NULL)) return a->valor; | |
else return somaFolhasArvore (a->esq) + somaFolhasArvore (a->dir); | |
} | |
int alturaArvore (tipoNo *a) { | |
int ae, ad; | |
if (a==NULL) return -1; | |
ae = alturaArvore (a->esq); | |
ad = alturaArvore (a->dir); | |
if (ae > ad) return 1 + ae; | |
else return 1 + ad; | |
} | |
tipoNo *buscaArvore (tipoNo *a, int valor) { | |
if (a==NULL) return NULL; | |
else if (valor < a->valor) return buscaArvore(a->esq,valor); | |
else if (valor > a->valor) return buscaArvore(a->dir,valor); | |
else return a; | |
} | |
void removeArvore (tipoNo **a, int valor) { | |
tipoNo *aux; | |
if ((*a)==NULL) return; | |
if (valor < (*a)->valor) removeArvore (&(*a)->esq, valor); | |
else if (valor > (*a)->valor) removeArvore (&(*a)->dir, valor); | |
else { | |
if (((*a)->dir==NULL) && ((*a)->esq==NULL)) { | |
(*a) = NULL; | |
} | |
else if ((*a)->esq==NULL) { | |
(*a) = (*a)->dir; | |
} | |
else if ((*a)->dir==NULL) { | |
(*a) = (*a)->esq; | |
} | |
else { | |
aux = (*a)->dir; | |
while (aux->esq) aux = aux->esq; | |
(*a)->valor = aux->valor; | |
aux->valor = valor; | |
removeArvore (&(*a)->dir, valor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment