Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active July 11, 2023 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/e4f608f8c8f7ca79f240a1c13a588eef to your computer and use it in GitHub Desktop.
Save parzibyte/e4f608f8c8f7ca79f240a1c13a588eef to your computer and use it in GitHub Desktop.
/**
* Árboles binarios en C
* Operaciones de:
* Inserción
* Recorrido inorden, postorden y preorden
* Uso de malloc
*
* @author parzibyte
* @see https://parzibyte.me/blog
* */
#include <stdio.h>
#include <stdlib.h>
struct Nodo {
int dato;
struct Nodo *izquierda;
struct Nodo *derecha;
};
struct Nodo *nuevoNodo(int dato) {
// Solicitar memoria
size_t tamanioNodo = sizeof(struct Nodo);
struct Nodo *nodo = (struct Nodo *) malloc(tamanioNodo);
// Asignar el dato e iniciar hojas
nodo->dato = dato;
nodo->izquierda = nodo->derecha = NULL;
return nodo;
}
void insertar(struct Nodo *nodo, int dato) {
// ¿Izquierda o derecha?
// Si es mayor va a la derecha
if (dato > nodo->dato) {
// Tienes espacio a la derecha?
if (nodo->derecha == NULL) {
nodo->derecha = nuevoNodo(dato);
} else {
// Si la derecha ya está ocupada, recursividad ;)
insertar(nodo->derecha, dato);
}
} else {
// Si no, a la izquierda
if (nodo->izquierda == NULL) {
nodo->izquierda = nuevoNodo(dato);
} else {
// Si la izquierda ya está ocupada, recursividad ;)
insertar(nodo->izquierda, dato);
}
}
}
void preorden(struct Nodo *nodo) {
if (nodo != NULL) {
printf("%d,", nodo->dato);
preorden(nodo->izquierda);
preorden(nodo->derecha);
}
}
void inorden(struct Nodo *nodo) {
if (nodo != NULL) {
inorden(nodo->izquierda);
printf("%d,", nodo->dato);
inorden(nodo->derecha);
}
}
void postorden(struct Nodo *nodo) {
if (nodo != NULL) {
postorden(nodo->izquierda);
postorden(nodo->derecha);
printf("%d,", nodo->dato);
}
}
int main(void) {
struct Nodo *raiz = nuevoNodo(28);
insertar(raiz, 11);
insertar(raiz, 96);
insertar(raiz, 21);
insertar(raiz, 6);
insertar(raiz, 97);
insertar(raiz, 1);
insertar(raiz, 30);
insertar(raiz, 10);
insertar(raiz, 2);
printf("\nImprimiendo preorden\n");
preorden(raiz);
printf("\nImprimiendo inorden\n");
inorden(raiz);
printf("\nImprimiendo postorden\n");
postorden(raiz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment