Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<stdbool.h> typedef enum { FALSE ,TRUE } bool; struct no { int info; int fb; struct no *esq; struct no *dir; }; struct no *inserir (int , struct no *, int *); struct no* buscar(struct no *,int); struct no* buscar(struct no *ptr,int info) { if(ptr!=NULL) if(info < ptr->info) ptr=buscar(ptr->esq,info); else if( info > ptr->info) ptr=buscar(ptr->dir,info); return(ptr); } struct no *inserir (int info, struct no *pptr, int *ht_inc) { struct no *aptr; struct no *bptr; if(pptr==NULL) { pptr = (struct no *) malloc(sizeof(struct no)); pptr->info = info; pptr->esq = NULL; pptr->dir = NULL; pptr->fb = 0; *ht_inc = TRUE; return (pptr); } if(info < pptr->info) { pptr->esq = inserir(info, pptr->esq, ht_inc); if(*ht_inc==TRUE) { switch(pptr->fb) { case -1: pptr->fb = 0; *ht_inc = FALSE; break; case 0: pptr->fb = 1; break; case 1: aptr = pptr->esq; if(aptr->fb == 1) { printf("Rotação para Esquerda\n"); pptr->esq= aptr->dir; aptr->dir = pptr; pptr->fb = 0; aptr->fb=0; pptr = aptr; } else { printf("Rotação para Direita\n"); bptr = aptr->dir; aptr->dir = bptr->esq; bptr->esq = aptr; pptr->esq = bptr->dir; bptr->dir = pptr; if(bptr->fb == 1 ) pptr->fb = -1; else pptr->fb = 0; if(bptr->fb == -1) aptr->fb = 1; else aptr->fb = 0; bptr->fb=0; pptr=bptr; } *ht_inc = FALSE; } } } if(info > pptr->info) { pptr->dir = inserir(info, pptr->dir, ht_inc); if(*ht_inc==TRUE) { switch(pptr->fb) { case 1: pptr->fb = 0; *ht_inc = FALSE; break; case 0: pptr->fb= -1; break; case -1: aptr = pptr->dir; if(aptr->fb == -1) { printf("Rotação para Direitan"); pptr->dir= aptr->esq; aptr->esq = pptr; pptr->fb = 0; aptr->fb=0; pptr = aptr; } else { printf("Rotação para Esquerda\n"); bptr = aptr->esq; aptr->esq = bptr->dir; bptr->dir = aptr; pptr->dir = bptr->esq; bptr->esq = pptr; if(bptr->fb == -1) pptr->fb = 1; else pptr->fb = 0; if(bptr->fb == 1) aptr->fb = -1; else aptr->fb = 0; bptr->fb=0; pptr = bptr; } *ht_inc = FALSE; } } } return(pptr); } display(struct no *ptr,int level) { int i; if ( ptr!=NULL ) { display(ptr->dir, level+1); printf("\n"); for (i = 0; i < level; i++) printf(" "); printf("%d", ptr->info); display(ptr->esq, level+1); } } numeros(struct no *ptr) { if(ptr!=NULL) { numeros(ptr->esq); printf("%d ",ptr->info); numeros(ptr->dir); } } main() { int ht_inc; int info ; int escolha; struct no *raiz = (struct no *)malloc(sizeof(struct no)); raiz = NULL; while(1) { printf("1.Arvore AVL\n\n"); printf("1.Inserir um numero\n"); printf("2.Mostrar Arvore\n"); printf("3.Sair do Programa\n"); printf("Digite um Número: "); scanf("%d",&escolha); switch(escolha) { case 1: printf("Digite o Numero a ser inserido : "); scanf("%d", &info); if( buscar(raiz,info) == NULL ) raiz = inserir(info, raiz, &ht_inc); else printf("Valor duplicado\n\n"); break; case 2: if(raiz==NULL) { printf("A Arvore esta vazia\n\n"); continue; } printf("AVL :\n"); display(raiz, 1); printf("\n\n"); printf("Numeros: "); numeros(raiz); printf("\n"); break; case 3: system("exit"); default: printf("Digite um numero válido\n"); } } }
Compartilhar