Exemplo n.º 1
0
int InsereAVL(struct No ** noRaiz, struct Item I)
{
	int resultado;

	if (*noRaiz == NULL)
	{

		struct No * NovoNo = CriaNo(I);

		if (NovoNo == NULL)
		{
			return 0;
		}

		*noRaiz = NovoNo;
		return 1;
	}

	struct No * noAtual = * noRaiz;

	if (ChaveMenor(GetChave(I), GetChave(noAtual->I)))
	{
		if ((resultado = InsereAVL(&(noAtual->esquerda), I)) == 1)
		{
			if (FatorDeBalanceamentoDoNo(noAtual) >= 2)
			{
				if ( ChaveMenor(GetChave(I), GetChave((*noRaiz)->esquerda->I)))
				{
					RotacaoSimplesADireita(noRaiz);
				}
				else
				{
					RotacaoDuplaADireita(noRaiz);
				}
			}
		}
	}
	else
	{
		if ((resultado = InsereAVL(&(noAtual->direita), I)) == 1)
		{
			if (FatorDeBalanceamentoDoNo(noAtual) >= 2)
			{
				if ( ChaveMenor(GetChave( (*noRaiz)->direita->I ), GetChave(I)))
				{
					RotacaoSimplesAEsquerda(noRaiz);
				}
				else
				{
					RotacaoDuplaAEsquerda(noRaiz);
				}
			}
		}

	}

	noAtual->altura = maior( RecuperaAlturaNo(noAtual->esquerda), RecuperaAlturaNo(noAtual->direita) ) + 1;

	return resultado;
}
//Insere um no em uma sub-arvore.
NoRN* InsereNo(NoRN* raiz, NoRN* pai, int valor){
	if(raiz == NULL){
		
		raiz = CriaNo(pai, valor);
		TornePai(raiz,pai);
		return Balanceamento(raiz);		
	}
	if(raiz->valor > valor){
		return InsereNo(raiz->filho_esq, raiz, valor);
	}
	if(raiz->valor < valor){
		return InsereNo(raiz->filho_dir, raiz, valor);
	}
	return Balanceamento(raiz);
}
Exemplo n.º 3
0
struct No * InserirRecursivo(struct No * anda, struct Item I)
{
	if(anda == NULL)
	{
		anda =  CriaNo(I);
	}
	else if(ChaveMenor(GetChave(I), GetChave(anda->I)))
	{
		anda->esquerda = InserirRecursivo(anda->esquerda, I);
	}
	else
	{
		anda->direita = InserirRecursivo(anda->direita, I);
	}
	return anda;
}
Exemplo n.º 4
0
struct No * InserirRecursivo(struct No * anda, struct Item I)
{
	if(anda == NULL)
	{
		anda =  CriaNo(I);
	}
	else if(ChaveMenor(GetChave(I), GetChave(anda->I)))
	{		
		anda->esquerda = InserirRecursivo(anda->esquerda, I);
		anda->altura = 1 + maior( RecuperaAlturaNo(anda->esquerda), RecuperaAlturaNo(anda->direita) );
	}
	else
	{
		anda->direita = InserirRecursivo(anda->direita, I);
		anda->altura = maior( RecuperaAlturaNo(anda->esquerda), RecuperaAlturaNo(anda->direita) ) + 1;
	}
	return anda;
}
Exemplo n.º 5
0
int Buscar(struct Dicionario * D, Chave c)
{
	struct Item I;
	SetChave(&I, c);
	return BuscarRecursivo(&D->raiz, c, CriaNo(I));
}