예제 #1
0
파일: abp.c 프로젝트: pbmartins/AlgC
unsigned int ABPSize (PtABPNode proot)  /* cálculo do número de elementos recursiva - recursive number of nodes */                                     
{
	if (proot == NULL) { Error = ABP_EMPTY; return 0; } /* arvore vazia - empty tree */
	else
	{
		Error = OK;
		return 1 + ABPSize (proot->PtLeft) + ABPSize (proot->PtRight);
	}
}
예제 #2
0
파일: abp.c 프로젝트: pbmartins/AlgC
PtABPNode ABPKMin (PtABPNode proot, unsigned int pk)
{
	if (proot == NULL) { Error = ABP_EMPTY; return NULL; }
	/* K inválido ou arvore com menos elementos do que K - invalid k or tree with less elemenst */
	if (pk < 1 || ABPSize (proot) < pk) { Error = NO_ELEM; return NULL; }
	else { Error = OK; return KMinRec (proot, pk); }
}
예제 #3
0
파일: abp.c 프로젝트: pbmartins/AlgC
/*******************************************************************************
  Função recursiva que pesquisa o K-menor elemento da arvore.
  
  Recursive function that selects the K-minimum element of the tree.
*******************************************************************************/
static PtABPNode KMinRec (PtABPNode proot, unsigned int pk)
{ 
	unsigned int NLeft = ABPSize (proot->PtLeft);

	if (pk == NLeft+1) return proot;
	else	if (pk <= NLeft) return KMinRec (proot->PtLeft, pk);
			else return KMinRec (proot->PtRight, pk-NLeft-1);
}
예제 #4
0
파일: abp.c 프로젝트: pbmartins/AlgC
PtABPNode ABPBalance (PtABPNode proot)
{
	int *List; PtABPNode NewABP = NULL;
	unsigned int Count = 0, n = ABPSize (proot);	/* número de nos - number of nodes */

	if (proot == NULL) { Error = ABP_EMPTY; return NULL; }	/* a arvore está vazia - empyt tree */	
				/* criar a sequência - creating the array */
	if ((List = (int *) calloc (n, sizeof (int))) == NULL) { Error = NO_MEM ; return NULL; }	

    Error = OK;
	ListInOrder (proot, List, &Count);	/* preencher a sequência - filling the array */
	Balance (&NewABP, List, 0, n-1);	/* balancear a arvore - balancing the tree */
	free (List);	/* libertar a sequência - releasing the array */

	return NewABP;
}
예제 #5
0
파일: abp.c 프로젝트: pbmartins/AlgC
void ABPStoreFile (PtABPNode proot, char *nomef)
{
    FILE *PtF; unsigned int NElem;

    /* abertura com validacao do ficheiro para escrita - opening the text file for writing */
    if ((PtF = fopen (nomef, "w")) == NULL) { Error = NO_FILE; return ; }

    NElem = ABPSize (proot);
    /* escrita do número de elementos da arvore no ficheiro - writing the tree's size */
    fprintf (PtF, "%u\n", NElem);

    /* escrita dos elementos da arvore no ficheiro - writing the tree's elements */
    StoreFilePreOrder (proot, PtF);

    fclose (PtF);  /* fecho do ficheiro - closing the text file */
    Error = OK;
}
예제 #6
0
int main (void)
{
	PtABPNode abp1, abp2, abp3; int st; char filename[21];

	system ("clear");
	printf ("\nLer arvore binaria de pesquisa do ficheiro\n");
	do
	{
  		printf ("Nome do ficheiro -> ");
		st = scanf ("%20[^\n]", filename);
		scanf ("%*[^\n]"); scanf ("%*c");
	} while (st == 0);

	abp1 = ABPCreateFile (filename);

	if (ABPEmpty (abp1)) printf ("\nArvore vazia!\n");

	printf ("\nNumero de nos da arvore = %d\n", ABPSize (abp1));

	printf ("\nAltura da arvore = %d\n", ABPHeight (abp1));

	printf ("\nArvore listada em pre-ordem (versao recursiva)\n");
	ABPPreOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em ordem (versao recursiva)\n");
	ABPInOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em pos-ordem (versao recursiva)\n");
	ABPPostOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em pre-ordem (versao repetitiva)\n");
	ABPPreOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada em ordem (versao repetitiva)\n");
	ABPInOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada em pos-ordem (versao repetitiva)\n");
	ABPPostOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada por niveis\n");
	ABPByLevel (abp1);
	printf ("\n");

	printf ("\nArvore listada hierarquicamente\n");
	ABPDisplay (abp1);
	printf ("\n");

	printf ("\nBalanceamento da arvore\n");
	abp2 = ABPBalance (abp1);

	printf ("\nNumero de nos da arvore = %d\n", ABPSize (abp2));

	printf ("\nAltura da arvore = %d\n", ABPHeight (abp2));

	printf ("\nArvore listada hierarquicamente\n");
	ABPDisplay (abp2);
	printf ("\n");
	
	printf ("\nCopia da arvore\n");
	abp3 = ABPCopy (abp1);

	printf ("\nNumero de nos da arvore = %d\n", ABPSize (abp3));

	printf ("\nAltura da arvore = %d\n", ABPHeight (abp3));

	printf ("\nArvore listada em ordem (versao recursiva)\n");
	ABPInOrderRec (abp3);
	printf ("\n");

	printf ("\nDestruir as arvores e terminar o programa\n\n");
	ABPDestroy (&abp1);
	ABPDestroy (&abp2);
	ABPDestroy (&abp3);

	return 0;
}
예제 #7
0
int main (void)
{
	PtABPNode abp1, abp2, abp3; int st; char filename[21];

	system ("clear");
	printf ("\nLer arvore binaria de pesquisa do ficheiro - Read binary search tree from a text file\n");
	do
	{
  		printf ("Nome do ficheiro (Filename) -> ");
		st = scanf ("%20[^\n]", filename);
		scanf ("%*[^\n]"); scanf ("%*c");
	} while (st == 0);

	abp1 = ABPCreateFile (filename);

	if (ABPEmpty (abp1)) printf ("\nArvore vazia! Empty tree!\n");

	printf ("\nNumero de nos da arvore (node number) = %d\n", ABPSize (abp1));

	printf ("\nAltura da arvore (tree height) = %d\n", ABPHeight (abp1));

	printf ("\nArvore listada em pre-ordem (versao recursiva)\n");
	printf ("Tree listed using a pre-order traversal (recursive version)\n");
	ABPPreOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em ordem (versao recursiva)\n");
	printf ("Tree listed using a in-order traversal (recursive version)\n");
	ABPInOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em pos-ordem (versao recursiva)\n");
	printf ("Tree listed using a post-order traversal (recursive version)\n");
	ABPPostOrderRec (abp1);
	printf ("\n");

	printf ("\nArvore listada em pre-ordem (versao repetitiva)\n");
	printf ("Tree listed using a pre-order traversal (repetitive version)\n");
	ABPPreOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada em ordem (versao repetitiva)\n");
	printf ("Tree listed using a in-order traversal (repetitive version)\n");
	ABPInOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada em pos-ordem (versao repetitiva)\n");
	printf ("Tree listed using a post-order traversal (repetitive version)\n");
	ABPPostOrderRep (abp1);
	printf ("\n");

	printf ("\nArvore listada por niveis - Tree listed using a traversal by level\n");
	ABPByLevel (abp1);
	printf ("\n");

	printf ("\nArvore listada hierarquicamente - Tree listed hierarchically\n");
	ABPDisplay (abp1);
	printf ("\n");

	printf ("\nBalanceamento da arvore Balancing the tree\n");
	abp2 = ABPBalance (abp1);

	printf ("\nNumero de nos da arvore (node number) = %d\n", ABPSize (abp2));

	printf ("\nAltura da arvore (tree height) = %d\n", ABPHeight (abp2));

	printf ("\nArvore listada hierarquicamente - Tree listed hierarchically\n");
	ABPDisplay (abp2);
	printf ("\n");
	
	printf ("\nCopia da arvore - Copy the tree\n");
	abp3 = ABPCopy (abp1);

	printf ("\nNumero de nos da arvore (node number) = %d\n", ABPSize (abp3));

	printf ("\nAltura da arvore (tree height)= %d\n", ABPHeight (abp3));

	printf ("\nArvore listada hierarquicamente - Tree listed hierarchically\n");
	ABPDisplay (abp3);
	printf ("\n");

	printf ("\nArvore listada em ordem (versao recursiva)\n");
	printf ("Tree listed using a in-order traversal (recursive version)\n");
	ABPInOrderRec (abp3);
	printf ("\n");

	printf ("\nDestruir as arvores - Releasing the trees\n\n");
	
	ABPDestroy (&abp1);
	ABPDestroy (&abp2);
	ABPDestroy (&abp3);

	return 0;
}
예제 #8
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;
}