Você está na página 1de 13

#include <stdio.

h>
#include <stdlib.h>
#include <windows.h>
#include <locale.h>

///ARVORE Arvore
typedef struct arvore
{
int info;
int fb;
struct arvore *esq;
struct arvore *dir;
} Arvore;

///PROT�TIPOS ABB
Arvore *Ler(FILE *p);
void EmOrdem(Arvore *a);
void Largura (Arvore *a);
void Notacao (Arvore *a);
int Existe (Arvore *a, int x);
void MenorMaior (Arvore *a, int x, int y);
int Entre (Arvore *a, int x, int y);
int Folha(Arvore *a);
void MaiorIgual(Arvore *a, int x);
int Altura(Arvore *a);
void imprimir_nivel(Arvore *a, int cont, int nivel);
int NivelNo (Arvore *a, int x);

///PROT�TIPOS AVL
Arvore * CalculaFB(Arvore *a);
Arvore * RotationRightSimples (Arvore *a);
Arvore * RotationLeftSimples (Arvore *a);
Arvore * RotationRight (Arvore *a);
Arvore * RotationLeft (Arvore *a);
Arvore * RotationRightDupla (Arvore *a);
Arvore * RotationLeftDupla (Arvore *a);
Arvore * Inserir (Arvore * a, int x, int *hmudou);
Arvore * Remover (Arvore *r, int x, int *hMudou);

void Printar () {
printf("BEM VINDO AO TRABALHO DE AVL DE LEO SANCHES E DANIEL HENRY\n");
Sleep(50);
printf("Selecione uma op��o abaixo:\n\n");
Sleep(50);
printf("0) Sair\n");
Sleep(50);
printf("1) Ler uma arvore de um arquivo, o nome do arquivo deve ser fornecido
pelo usuario\n");
Sleep(50);
printf("2) Inserir novo n�\n");
Sleep(50);
printf("3) Remover n�\n");
Sleep(50);
printf("4) Imprimir em largura\n");
Sleep(50);
printf("5) Imprimir em nota��o\n");
Sleep(50);
printf("6) Imprimir valores menores que x ou maiores que y(x<y)\n");
Sleep(50);
printf("7) Contar o n�mero de elementos entre x e y\n");
Sleep(50);
printf("8) Imprimir as folhas de valor maior ou igual a x\n");
Sleep(50);
printf("9) Verificar se no existe na arvore\n");
Sleep(50);
}

int main()
{

setlocale(LC_ALL,"");
Arvore *a;
int menu = -1;
int alce1, alce2, alce3;
int *hmudou = 0;

while (menu != 0)
{
system("CLS");
Printar();
setbuf(stdin, NULL);
scanf("%d", &menu);
if (menu == 1)
{
char arqnome[100];
printf("//com a extens�o\n");
printf("Insira o nome do arquivo: ");
scanf("%s", arqnome);
FILE *p = fopen(arqnome, "r");

if (p == NULL)
printf("ERRO! O arquivo n�o foi aberto!\n");
else
{
printf("O arquivo foi aberto com sucesso!\n\n");
a = Ler(p);
printf("Arvore:\n");
Largura(a);
}
setbuf(stdin, NULL);
getchar();
}
if (menu == 2)
{
printf("Que valor deseja inserir? ");
scanf("%d", &alce1);
a = Inserir(a, alce1, &hmudou);
printf("Inserido e balanceado :3\n");
setbuf(stdin, NULL);
getchar();
}
if (menu == 3)
{
printf("Qual valor deseja remover? ");
scanf("%d", &alce1);
if (Existe(a, alce1) == 1)
{
a = Remover(a, alce1, &hmudou);
printf("Removido e balanceado:3\n");
}
else
{
printf("Valor inexistente na arvore :/\n\n\n");
}
setbuf(stdin, NULL);
getchar();
}
if (menu == 4)
{
system("CLS");
Largura(a);
setbuf(stdin, NULL);
getchar();
}
if (menu == 5)
{
system("CLS");
Notacao(a);
setbuf(stdin, NULL);
getchar();
}
if (menu == 6)
{
system("CLS");
printf("Digite um valor para x: ");
scanf("%d", &alce1);
alce2 = alce1;
while (alce2<=alce1)
{
printf("(x<y)\n");
printf("Digite um valor para y: ");
scanf("%d", &alce2);
}
MenorMaior(a, alce1, alce2);
setbuf(stdin, NULL);
getchar();
}
if (menu == 7)
{
system("CLS");
printf("Digite um valor para x: ");
scanf("%d", &alce1);
alce2 = alce1;
while (alce2<=alce1)
{
printf("(x<y)\n");
printf("Digite um valor para y: ");
scanf("%d", &alce2);
}
printf("Existem %d elementos entre x e y", Entre(a, alce1, alce2));
setbuf(stdin, NULL);
getchar();
}
if (menu == 8)
{
printf("Digite um valor para x: ");
scanf("%d", &alce1);
MaiorIgual(a, alce1);
setbuf(stdin, NULL);
getchar();
}
if (menu == 9)
{
printf("Qual valor deseja verificar? ");
scanf("%d", &alce1);
if (Existe(a, alce1) == 1)
{
printf("Existe :3\n");
}
else
{
printf("Valor inexistente na arvore :/\n\n\n");
}
setbuf(stdin, NULL);
getchar();
}
if (menu == 11)
{
printf("FUN��O SECRETA: N�VEL DO N�!\n");
printf("Qual no deseja verificar? ");
scanf("%d", &alce1);
if (Existe(a, alce1) == 1)
{
printf("O n�vel do n� � %d", NivelNo(a, alce1));
}
else
{
printf("Valor inexistente na arvore :/\n\n\n");
}
setbuf(stdin, NULL);
getchar();
}
}
LiberaArvore(a);
}

Arvore *Ler(FILE *p)


{
char ch;
int x;
fscanf(p, "%c", &ch);
fscanf(p, "%d", &x);
if (x == -1)
{
fscanf(p, "%c", &ch);
return NULL;
}
else
{
Arvore *no = (Arvore *)malloc(sizeof(Arvore));
no->info = x;
no->esq = Ler(p);
no->dir = Ler(p);
fscanf(p, "%c", &ch);
return no;
}
}

void EmOrdem (Arvore *a)


{
if(a != NULL)
{
Largura(a->esq);
printf("%d \n", a->info);
Largura(a->dir);
}
}

void Largura (Arvore *a)


{
if (a != NULL)
{
int i;
for (i = 1; i <= Altura(a); i++)
{
imprimir_nivel(a,1,i);
printf("\n");
}
}
}

void imprimir_nivel(Arvore *a, int cont, int nivel)


{
if (a != NULL)
{
if (cont == nivel)
{
printf("%d ",a->info);
}
else
{
imprimir_nivel(a->esq, cont + 1, nivel);
imprimir_nivel(a->dir, cont + 1, nivel);
}
}
}

void Notacao (Arvore *a)


{
if(a!=NULL)
{
printf("(");
printf("%d", a->info);
Notacao(a->esq);
Notacao(a->dir);
printf(")");
}
else
printf("(-1)");
}

int Existe (Arvore *a, int x)


{
if (a==NULL)
{
return 0;
}
else
{
if (a->info == x)
{
return 1;
}
else
{
if (a->info < x)
{
return Existe(a->dir, x);
}
else
{
return Existe(a->esq, x);
}
}
}
}

void MenorMaior (Arvore *a, int x, int y)


{
if (a != NULL)
{

///CASO A CASO S� PRA N�O ME PERDER


if (a->info < x)
{
printf("%d ", a->info);
MenorMaior(a->esq, x, y);
}
if (a->info >= x || a->info <= y)
{
MenorMaior(a->esq, x, y);
MenorMaior(a->dir, x, y);
}
if (a->info > y)
{
printf("%d ", a->info);
MenorMaior(a->dir, x, y);
}

}
}

int Entre (Arvore *a, int x, int y)


{
if (a != NULL)
{

///CASO A CASO PRA EU N�O ME PERDER


if (a->info < x)
{
return Entre(a->dir, x, y);
}
if (a->info == x)
{
return Entre(a->dir, x, y) + 1;
}
if (a->info > x && a->info < y)
{
return Entre(a->dir, x, y) + Entre(a->esq, x, y) + 1;
}
if (a->info == y)
{
return Entre(a->esq, x, y) + 1;
}
if (a->info > y)
{
return Entre(a->esq, x, y);
}
}
else
{
return 0;
}
}

int Folha (Arvore *a)


{
if (a != NULL)
{
if (a->dir == NULL && a->esq == NULL)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}

void MaiorIgual(Arvore *a, int x)


{
if (a != NULL)
{
if (a->info >= x)
{
if (Folha(a) == 1)
{
printf("%d ", a->info);
}
MaiorIgual(a->dir, x);
MaiorIgual(a->esq, x);
}
if (a->info < x)
{
MaiorIgual(a->esq, x);
}
}
}

int Altura (Arvore *a)


{
if (a==NULL)
{
return 0;
}
else
{
int he, hd;
he = Altura(a->esq);
hd = Altura(a->dir);
if (he>hd)
return he + 1;
else
return hd + 1;
}
}

void LiberaArvore (Arvore *a)


{
if (a != NULL)
{
LiberaArvore(a->esq);
LiberaArvore(a->dir);
free(a);
}
}

int NivelNo (Arvore *a, int x)


{
if (a != NULL)
{
if (a->info == x)
{
return 0;
}
else
{
if (a->info > x)
{
return NivelNo(a->dir, x) + 1;
}
else
{
return NivelNo(a->esq, x) + 1;
}
}
}
}

///A PARTIR DE AGORA AS FUN��ES DA AVL


Arvore * CalculaFB(Arvore *a)
{
if(a != NULL)
{
int he, hd;
he = Altura(a->esq);
hd = Altura(a->dir);
a->fb = hd-he;
CalculaFB(a->esq);
CalculaFB(a->dir);
return a;
}
else
{
return NULL;
}
}

Arvore * RotationRightSimples (Arvore *a)


{
Arvore *alce1;
Arvore *alce2;
alce1 = a->esq->dir;
a->esq->dir = a;

a->fb = -1-a->esq->fb;
a->esq->fb = a->esq->fb+1;

alce2 = a->esq;
a->esq = alce1;
a = alce2;
return a;
}

Arvore * RotationLeftSimples (Arvore *a)


{
Arvore *alce1;
Arvore *alce2;
alce1 = a->dir->esq;
a->dir->esq = a;

a->fb = 1-a->dir->fb;
a->dir->fb = a->dir->fb-1;

alce2 = a->dir;
a->dir = alce1;
a = alce2;
return a;
}

Arvore * RotationLeft (Arvore*a)


{
///FB = 2
if (a->dir->fb >= 0)
{
a = RotationLeftSimples(a);
}
else
{
a = RotationLeftDupla(a);
}
return a;
}

Arvore * RotationRight (Arvore*a)


{
///FB = -2
if (a->esq->fb >= 0)
{
a = RotationRightSimples(a);
}
else
{
a = RotationRightDupla(a);
}
return a;
}

Arvore * RotationRightDupla (Arvore *a)


{
a->esq = RotationLeft(a->esq);
a = RotationRight(a);
return a;
}
Arvore * RotationLeftDupla (Arvore *a)
{
a->dir = RotationRight(a->dir);
a = RotationLeft(a);
return a;
}

Arvore *Inserir(Arvore *r, int x, int *hMudou) {


///Momento de inserir
if (r == NULL) {
r = (Arvore*) malloc (sizeof(Arvore));
r->info = x;
r->esq = NULL;
r->dir = NULL;
r->fb = 0;
*hMudou = 1;
}
else {
if (x <= r->info) {
r->esq = Inserir (r->esq,x,hMudou);
if (*hMudou == 1) {
switch (r->fb)
{
case -1:
r = RotationRight(r);
*hMudou = 0;
break;
case 0:
r->fb = -1;
*hMudou = 1;
break;
case +1:
r->fb = 0;
*hMudou = 0;
break;
}
}
}
else {
r->dir = Inserir (r->dir,x,hMudou);
if (*hMudou == 1) {
switch (r->fb)
{
case -1:
r->fb = 0;
*hMudou = 0;
break;
case 0:
r->fb = +1;
*hMudou = 1;
break;
case +1:
r = RotationLeft(r);
*hMudou = 0;
break;
}
}
}
}
return r;
}

Arvore *Remover (Arvore *r, int x, int *hMudou) {


if (r != NULL) {
if (r->info == x) {
//Verificar se � folha
if (r->esq == NULL && r->dir == NULL) {
free (r);
*hMudou = 1;
return NULL;
}
//Verificar se um dos filhos � nulo
else if (r->esq == NULL || r->dir == NULL) {
Arvore *aux;
if (r->esq == NULL)
aux = r->dir;
else
aux = r->esq;

free(r);
*hMudou = 1;
return aux;
}
//Nenhum filho nulo
else {
Arvore *maiorEsq = r->esq;
while (maiorEsq->dir != NULL)
maiorEsq = maiorEsq->dir;

r->info = maiorEsq->info;
r->esq = Remover(r->esq,r->info,hMudou);

if (*hMudou == 1) {
switch(r->fb)
{
case -1:
r->fb = 0;
*hMudou = 1;
break;
case 0:
r->fb = 1;
*hMudou = 0;
break;
case +1:
{
int aux = r->dir->fb;
r = RotationLeft(r);
if (aux == 0)
*hMudou = 0;
else
*hMudou = 1;
break;
}
}
}
}
}
else if (x < r->info) {
r->esq = Remover(r->esq, x, hMudou);

if (*hMudou == 1) {
switch(r->fb)
{
case -1:
r->fb = 0;
*hMudou = 1;
break;
case 0:
r->fb = 1;
*hMudou = 0;
break;
case +1:
{
int aux = r->dir->fb;
r = RotationLeft(r);

if (aux == 0)
*hMudou = 0;
else
*hMudou = 1;
break;
}
}
}
}
else {
r->dir = Remover(r->dir, x, hMudou);

if (*hMudou == 1) {
switch(r->fb)
{
case +1:
r->fb = 0;
*hMudou = 1;
break;
case 0:
r->fb = -1;
*hMudou = 0;
break;
case -1:
{
int aux = r->esq->fb;
r = RotationRight(r);

if (aux == 0)
*hMudou = 0;
else
*hMudou = 1;
break;
}
}
}
}
return r;
}
}

Você também pode gostar