AVL adicionarAVL(AVL *raiz, int year, int nraut)
{ 
    if(*raiz == NULL) 
    { 
        *raiz = (AVL)malloc(sizeof(NodoAVL));
        (*raiz)->ano = year;
        (*raiz)->lnaut = criaLNaut();
         inserirAut((*raiz)->lnaut, nraut);   
        (*raiz)->esq = (*raiz)->dir = NULL;
    } 
    else if(year < (*raiz)->ano) 
    { 
        (*raiz)->esq = adicionarAVL(&((*raiz)->esq), year, nraut); 
        (*raiz) = balancearAVL(raiz);  
    } 
    else if(year > (*raiz)->ano) 
    { 
        (*raiz)->dir = adicionarAVL(&((*raiz)->dir), year, nraut); 
        (*raiz) = balancearAVL(raiz); 
    } 
    else if(year == (*raiz)->ano)
    { 
        inserirAut((*raiz)->lnaut, nraut);
    } 
    return *raiz; 
} 
Exemple #2
0
void inserir(struct Arvore *t, struct No *novo)
{
    struct No *pai=NULL, *filho=t->raiz;
    if(t->raiz == NULL)
    {
        t->raiz = novo;
    }
    else
    {
        while(filho!=NULL)
        {
            pai = filho;
            if(filho->chave > novo->chave)
            {
                filho = filho->esquerda;
            }
            else
            {
                filho = filho->direita;
            }
        }
        if(pai->chave > novo->chave)
        {
            pai->esquerda = novo;
        }
        else
        {
            pai->direita = novo;
        }
        novo->pai = pai;
        balancearAVL(novo->pai, t);
    }
}
Exemple #3
0
void balancearAVL(struct No *r, struct Arvore *t)
{
    int fatorR = fatorBalanceamento(r);
    int fatorE = fatorBalanceamento(r->esquerda);
    int fatorD = fatorBalanceamento(r->direita);

    if(fatorR == -2)
    {
        if(fatorD == 1)
        {
            rotacaoDireita(r->direita, t);
        }
        rotacaoEsquerda(r, t);
    }
    else if(fatorR == 2)
    {
        if(fatorE == -1)
        {
            rotacaoEsquerda(r->esquerda, t);
        }
        rotacaoDireita(r, t);
    }

    if(r->pai != NULL)
    {
        balancearAVL(r->pai, t);
    }
}
Exemple #4
0
void removera(struct Arvore *t, int chave)
{
    struct No *pai=NULL, *filho=t->raiz, *subs_pai=NULL, *subs;
    if(t->raiz!=NULL)
    {
        while(filho!=NULL && filho->chave != chave)
        {
            pai = filho;
            
            if(filho->chave > chave)
            {
                filho = filho->esquerda;
            }
            
            else
            {
                filho = filho->direita;
            }
        }
        if(filho!=NULL)
        {
            if(filho->esquerda == NULL && filho->direita == NULL)
            {
                if(pai!=NULL)
                {
                    if(pai->esquerda == filho)
                    {
                        pai->esquerda = NULL;
                    }
                    else
                    {
                        pai->direita = NULL;
                    }
                }
                else
                {
                    t->raiz = NULL;
                }
            }
            else if(filho->esquerda != NULL && filho->direita != NULL)
            {
                subs = filho->esquerda;
                while(subs!=NULL)
                {
                    subs_pai = subs;
                    subs = subs->direita;
                }
                subs = (struct No*) malloc(sizeof(struct No));
                subs->chave = subs_pai->chave;
                subs->esquerda = subs->direita = NULL;
                removera(t, subs_pai->chave);
                if(pai != NULL)
                {
                    if(pai->esquerda == filho)
                    {
                        pai->esquerda = subs;
                    }
                    else
                    {
                        pai->direita = subs;
                    }
                }
                else
                {
                    t->raiz = subs;
                }
                subs->direita = filho->direita;
                if(filho->direita != NULL)
                    filho->direita->pai = subs;
                subs->esquerda = filho->esquerda;
                if(filho->esquerda != NULL)
                    filho->esquerda->pai = subs;
                subs->pai = pai;
            }
            else
            {
                if(pai != NULL)
                {
                    if(pai->esquerda == filho)
                    {
                        if(filho->esquerda != NULL)
                        {
                            pai->esquerda = filho->esquerda;
                            filho->esquerda->pai = pai;
                        }
                        else
                        {
                            pai->esquerda = filho->direita;
                            filho->direita->pai = pai;
                        }
                    }
                    else
                    {
                        if(filho->esquerda != NULL)
                        {
                            pai->direita = filho->esquerda;
                            filho->esquerda->pai = pai;
                        }
                        else
                        {
                            pai->direita = filho->direita;
                            filho->direita->pai = pai;
                        }
                    }
                }
                else
                {
                    if(filho->esquerda != NULL)
                    {
                        t->raiz = filho->esquerda;
                        filho->esquerda->pai = NULL;
                    }
                    else
                    {
                        t->raiz = filho->direita;
                        filho->direita->pai = NULL;
                    }
                }
            }
            balancearAVL(filho->pai,t);
            free(filho);
        }
    }
}