Mostrando entradas con la etiqueta arboles binarios. Mostrar todas las entradas
Mostrando entradas con la etiqueta arboles binarios. Mostrar todas las entradas

miércoles, 15 de abril de 2020

Graphviz. Simple solution for recreating graphics

This is the first article that I publish in English, the main reason to do it is because all the documentation related to context maps that I have found so far, are related in English and obviously my way to contribute to the extensive Internet community, is to do it in a universal language, at least for this article.

Well, today I want to talk about two utilities that have helped me a lot, one of them is graphviz.org. Graphviz, which has versions for Windows, Mac or Linux (in my case I have tried in Linux and Mac OS), is a powerful Open Source tool that allows to easily generate almost any graphic, cool stuff like this:



Or simpler things than recreating a binary tree that I explained in the previous section, https://www.larebelion.com/2020/04/arboles-binarios.html

Imagine that in code C, you want to represent the resulting binary tree, with few lines of code you could get something like this:


Let me a simple C function to add to the previous tree and generate this:

static void _generating_graphviz_details(const tree_t node, const void *extra)
{
 FILE *f = (FILE *) extra;

 fprintf(f, " node_%p [label=\"{%p | content: %d | { <izqda> %p | <dcha> %p}\"];\n",
  node, node, node->content, node->left, node->right
 );
 if (node->left != NULL) {
  fprintf(f, " node_%p:left -> node_%p;\n", node, node->left);
 }
 if (node->right != NULL) {
  fprintf(f, " node_%p:dcha -> node_%p;\n", node, node->dcha);
 }
 fprintf(f, "f");
}

Okay, hopefully this tiny contribution will help you in the dynamic generation of your graphics.

domingo, 12 de abril de 2020

Árboles binarios

Ejercicio sencillo para representar un Arbol Binario, funciones básicas:

  • Crear nuevo árbol
  • Insertar hojas
  • Destruir Árbol
  • Imprimir en (Preorden, Postorden e Inorden)
  • Localizar nodos menores a un número, mostrarlos y contarlos.





#include <stdio.h>
#include <stdlib.h>

typedef struct nodo
{
    struct nodo *hizqdo;
    struct nodo *hdrcho;
    int contenido;
} nodo;

typedef struct nodo *arbol;

arbol nuevo_arbol (int contenido);
void destruir_arbol (arbol a);
void insertar_arbol (arbol nuevo, arbol *a);
void encontrar_menores (arbol a, int comparador, int *contador);
void imprimir (arbol a, int modo);

int main (void)
{
    int contenidos[] = {1, 20, 43, 12, 45, 21, 75, 26, 16, 74, 74, 36, 24, 2, 29, 97, -1};
    int *c = contenidos;
    arbol a = NULL;
    int contador = 0;
    while (*c >= 0) {
        arbol nuevo = nuevo_arbol(*c);
        insertar_arbol (nuevo, &a);
        c++;
    }

    printf ("\nPreorden:\n");
    imprimir(a, 1);
    printf ("\nPostorden:\n");
    imprimir(a, 2);
    printf ("\nInorden:\n");
    imprimir(a, 3);

    printf ("\nEncontrar menores de 50:\n");
    encontrar_menores(a, 50, &contador);
    printf ("\nContador: %d", contador);

    destruir_arbol(a);
    return EXIT_SUCCESS;
}

arbol nuevo_arbol (int contenido)
{
    arbol ret;
    if ((ret = calloc(1,sizeof(nodo))) == NULL) {
        return NULL;
    }

    ret->contenido = contenido;

    return ret;
}

void destruir_arbol (arbol a)
{
    if (a == NULL) {
        return;
    }
    destruir_arbol(a->hizqdo);
    destruir_arbol(a->hdrcho);
    free(a);
}

void insertar_arbol (arbol nuevo, arbol *a)
{
    if (*a == NULL) {
        *a = nuevo;
        return;
    }

    if (nuevo->contenido < (*a)->contenido) {
        insertar_arbol(nuevo, &((*a)->hizqdo));
    } else if (nuevo->contenido > (*a)->contenido) {
        insertar_arbol(nuevo, &((*a)->hdrcho));
    }
}

void encontrar_menores (arbol a, int comparador, int *contador)
{
    if (a == NULL) {
        return;
    }

    if (a->contenido < comparador) {
        printf ("%d | ", a->contenido);  
        ++(*contador);
    } 
    encontrar_menores (a->hizqdo, comparador, contador);
    encontrar_menores (a->hdrcho, comparador, contador);
}

void imprimir (arbol a, int modo) 
{
    if (a == NULL) {
        return;
    }

    if (modo == 1) { // Preorden
    printf ("%d | ", a->contenido);
    imprimir(a->hizqdo, 1);
    imprimir(a->hdrcho, 1);
    } else if (modo == 2){ // Postorden
        imprimir(a->hizqdo, 2);
        imprimir(a->hdrcho, 2);
        printf ("%d | ", a->contenido);
        } else if (modo == 3) {// Inorden
            imprimir(a->hizqdo, 3);
            printf ("%d | ", a->contenido);
            imprimir(a->hdrcho, 3);
            }
}

lunes, 3 de junio de 2019

Ejemplo completo de Matrices en C

A continuación, os pongo el ejemplo que se me ha ocurrido más completo sobre gestión de Matrices en C que reúne los ejemplos que he ido poniendo anteriormente.




#include <stdio.h>;
#include <stdlib.h>;


void Ingresa_Matriz (int m[3][3], int f, int c);
void Utiliza_Matriz (int m[3][3], int f, int c);
void Visualiza_Matriz (int m[3][3], int f, int c);
void Rota_Matriz_D (int m[3][3], int f, int c);
void Rota_Matriz_I (int m[3][3], int f, int c);
void Multiplica_Matriz (int m[3][3], int f, int c, int cf, int *m0, int *m1, int *m2);

int main () {
    char ingresa;
    int m[3][3], cf = 0, direccion = 0, f = 0, c = 0;
    int m0, m1, m2;
    printf ("Deseas ingresar Matriz o utiliza la maqueta &lt;I/M&gt;: ");
    scanf ("%c",&amp;ingresa);
    if (ingresa == 'i') {
        Ingresa_Matriz(m, f, c);
    } else {
        Utiliza_Matriz(m, f, c);
    }
    Visualiza_Matriz(m, f, c);
    printf ("\nDeseas rotar la matriz a la derecha &lt;1&gt; o a la izquierda &lt;0&gt;: ");
    scanf ("%d",&amp;direccion);
    if (direccion == 1) {
        Rota_Matriz_D (m, f, c);
    } else {
        Rota_Matriz_I (m, f, c);
    }
    Visualiza_Matriz(m, f, c);
    printf ("\n¿Quieres multiplicar las filas (1) o las columnas (0)?: ");
    scanf ("%d",&amp;cf);
    Multiplica_Matriz(m, f, c, cf, &amp;m0, &amp;m1, &amp;m2);
    if (cf == 1) {
        printf ("\nMultiplicacion Fila 0: %d",m0);
        printf ("\nMultiplicacion Fila 1: %d",m1);
        printf ("\nMultiplicacion Fila 2: %d",m2);
    } else {
        printf ("\nMultiplicacion Columna 0: %d",m0);
        printf ("\nMultiplicacion Columna 1: %d",m1);
        printf ("\nMultiplicacion Columna 2: %d",m2);
    }
    return 0;
}

void Ingresa_Matriz (int m[3][3], int f, int c) {
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            printf ("\nIntroduce posicion [%d][%d]: ",f,c);
            scanf ("%d",&amp;m[f][c]);
        }
    }
}

void Utiliza_Matriz (int m[3][3], int f, int c) {
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            m[f][c]=rand()%50+1;
        }
    }
}

void Visualiza_Matriz (int m[3][3], int f, int c) {
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            printf ("[%d] ",m[f][c]);
        } printf ("\n");
    }
}
void Rota_Matriz_D (int m[3][3], int f, int c) {
    int tmp[3][3];
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
                if (c == 2) {
                    tmp[f][0]=m[f][c];
                } else {
                    tmp[f][c+1]=m[f][c];
                }
            }
        }
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            m[f][c]=tmp[f][c];
        }
    }
}

void Rota_Matriz_I (int m[3][3], int f, int c) {
    int tmp[3][3];
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            if (c == 0) {
                tmp[f][2]=m[f][c];
            } else {
                tmp[f][c-1]=m[f][c];
            }
        }
    }
    
    for (f=0;f&lt;3;++f) {
        for (c=0;c&lt;3;++c) {
            m[f][c]=tmp[f][c];
        }
    }
}

void Multiplica_Matriz (int m[3][3], int f, int c, int cf, int *m0, int *m1, int *m2) {
    // 1 Multiplica filas
    // 0 Multiplica columnas
    int c0[3], c1[3], c2[3];
    
    if (cf == 0) {
        printf ("\nMultiplicando columnas...\n\n");
        for (f=0;f&lt;3;++f) {
            for (c=0;c&lt;3;++c) {
                if (c == 0)
                    c0[f]=m[f][c];
                if (c == 1)
                    c1[f]=m[f][c];
                if (c == 2)
                    c2[f]=m[f][c];
            }
        }
    } else {
        printf ("\nMultiplicando filas...\n\n");
        for (f=0;f&lt;3;++f) {
            for (c=0;c&lt;3;++c) {
                if (f == 0)
                    c0[c]=m[f][c];
                if (f == 1)
                    c1[c]=m[f][c];
                if (f == 2)
                    c2[c]=m[f][c];
            }
        }
    }
    *m0=c0[0]*c0[1]*c0[2];
    *m1=c1[0]*c1[1]*c1[2];
    *m2=c2[0]*c2[1]*c2[2];
}