예제 #1
0
파일: abp.c 프로젝트: pbmartins/AlgC
void ABPDeleteRec (PtABPNode *proot, int pelem)	/* remoção recursiva - recursive deletion */
{
	Error = OK;
	if (*proot == NULL) { Error = NO_ELEM; return; }	/* arvore vazia ou elemento inexistente - empty tree or non existing element */

	if ((*proot)->Elem > pelem)
			ABPDeleteRec (&(*proot)->PtLeft, pelem);
	else if ((*proot)->Elem < pelem)
				ABPDeleteRec (&(*proot)->PtRight, pelem);
			else NodeDelete (proot);	/* eliminar o elemento - deleting the element */
}
예제 #2
0
파일: abp.c 프로젝트: pbmartins/AlgC
/*******************************************************************************
  Função que remove efectivamente um no da arvore.
  
  Function that efectively deletes the node.
*******************************************************************************/
static void NodeDelete (PtABPNode *proot)
{
	PtABPNode Node = *proot;

	if ((*proot)->PtLeft == NULL && (*proot)->PtRight == NULL)
			ABPNodeDestroy (proot);	/* eliminar o elemento que é uma folha - deleting a leaf node */
	else if ((*proot)->PtLeft == NULL)	/* com subarvore direita - with right subtree only */
			{
				*proot = (*proot)->PtRight;	/* ligar à direita - connect to the right subtree */
				ABPNodeDestroy (&Node);	/* eliminar o elemento - deleting the node */
			}
			else if ((*proot)->PtRight == NULL)	/* com subarvore esquerda - with left subtree only */
					{
						*proot = (*proot)->PtLeft;	/* ligar à esquerda - connect to the left subtree */
						ABPNodeDestroy (&Node);	/* eliminar o elemento - deleting the node */
					}
					else	/* com subarvores direita e esquerda - with both right and left subtrees */
					{	/* substituir pelo menor elemento da subarvore direita - replacing by the minimum element of the right subtree */
						(*proot)->Elem = FindMin ((*proot)->PtRight);
						/* remover o menor elemento da subarvore direita - deleting the minimum element of the right subtree */
						ABPDeleteRec (&(*proot)->PtRight, (*proot)->Elem);
					}
}
예제 #3
0
파일: simabp.c 프로젝트: DanielaSimoes/algc
int main (void)
{
    PtABPNode abp, node; int value, error, sum; unsigned int count; char op;

    system ("clear");
    abp = ABPCreate ();

    do
    {
        printf ("Inserir-Inserting/Remover-Removing/Terminar-Terminating-> ");
        scanf ("%c", &op);
        scanf ("%*[^\n]"); scanf ("%*c");
        op = toupper (op);
    } while (op != 'I' && op != 'R' && op != 'T');

    while (op != 'T')
    {
        printf ("Valor (Value) -> ");
        scanf ("%d", &value);
        scanf ("%*[^\n]"); scanf ("%*c");

        if (op == 'I')
        {
            ABPInsertRec (&abp, value);
            if ((error = ABPErrorCode ()) != OK) printf ("Erro a inserir (Error in inserting) -> %s\n", ABPErrorMessage ());
        }
        else	if (op == 'R')
        {
            ABPDeleteRec (&abp, value);
            if ((error = ABPErrorCode ()) != OK) printf ("Erro a remover (Error in removing) -> %s\n", ABPErrorMessage ());
        }
        else
        {
            printf ("Erro na operacao escolhida - Error on the chosen operation\n");
            error = 1;
        }

        if (!error)
        {
            printf ("--------------------------------------------------------------------------------------------\n");
            if (ABPEmpty (abp)) printf ("\nArvore vazia! - Empty tree!\n");
            else ABPDisplay (abp);
            printf ("--------------------------------------------------------------------------------------------\n");
        }

        printf ("Inserir-Inserting/Remover-Removing/Terminar-Terminating-> ");
        scanf ("%c", &op);
        scanf ("%*[^\n]"); scanf ("%*c");
        op = toupper (op);
    }

    printf ("--------------------------------------------------------------------------------------------\n");
    if (ABPEmpty (abp)) printf ("Arvore vazia! - Empty tree!\n");
    else
    {
        ABPDisplay (abp);
        printf ("--------------------------------------------------------------------------------------------\n");
        printf ("Numero de nos da arvore (number of nodes) = %d\n", ABPSize (abp));
        printf ("Altura da arvore (tree height) = %d\n", ABPHeight (abp));
    }
    printf ("--------------------------------------------------------------------------------------------\n");

    node = ABPMinRep (abp);
    if (ABPErrorCode ()) printf ("Menor elemento da arvore (minimum element) = %s\n", ABPErrorMessage ());
    else printf ("Menor elemento da arvore (minimum element) = %d\n", ABPElement (node));

    node = ABPMaxRep (abp);
    if (ABPErrorCode ()) printf ("Maior elemento da arvore (maximum element) = %s\n", ABPErrorMessage ());
    else printf ("Maior elemento da arvore (maximum element) = %d\n", ABPElement (node));

    sum = ABPTotalSum (abp);
    if (ABPErrorCode ()) printf ("Soma dos elementos da arvore (Soma of the elements) = %s\n", ABPErrorMessage ());
    else printf ("Soma dos elementos da arvore (sum of the elements) = %d\n", sum);


    count = ABPEvenCount (abp);
    if (ABPErrorCode ()) printf ("Contagem dos elementos pares da arvore (number of even elements) = %s\n", ABPErrorMessage ());
    else printf ("Contagem dos elementos pares da arvore (number of even elements) = %d\n", count);

    sum = ABPMultSum (abp, 5);
    if (ABPErrorCode ()) printf ("Soma dos elementos da arvore multiplos de 5 (sum of elements multiple of 5) = %s\n", ABPErrorMessage ());
    else printf ("Soma dos elementos da arvore multiplos de 5 (sum of elements multiple of 5) = %d\n", sum);

    count = ABPOddCount (abp);
    if (ABPErrorCode ()) printf ("Contagem dos elementos impares da arvore (number of odd elements) = %s\n", ABPErrorMessage ());
    else printf ("Contagem dos elementos impares da arvore (number of odd elements) = %d\n", count);

    printf ("--------------------------------------------------------------------------------------------\n");
    printf ("Arvore listada em ordem (inorder traversal) ");
    ABPInOrderRec (abp);
    printf ("\n");
    printf ("--------------------------------------------------------------------------------------------\n");

    sum = ABPEvenOrderSum (abp);
    if (ABPErrorCode ()) printf ("Soma dos elementos com numero de ordem par da arvore (sum of elements with even order number) = %s\n", ABPErrorMessage ());
    else printf ("Soma dos elementos com numero de ordem par da arvore (sum of elements with even order number) = %d\n", sum);

    printf ("--------------------------------------------------------------------------------------------\n");
    printf ("Elementos impares da arvore por ordem crescente (odd elements by increasing order) ");
    ABPOddPrint (abp);
    printf ("\n");
    printf ("--------------------------------------------------------------------------------------------\n");

    printf ("\n");

    ABPDestroy (&abp);

    return 0;
}