Menu
Coddy logo textTech

Insert

Lección 10 de 13 del curso Árbol Binario - Serie de Estructuras de Datos #3 de Coddy.

challenge icon

Desafío

Fácil

Añade a BinaryTree un método insert que reciba un entero y lo inserte en un árbol binario de búsqueda. (Los valores menores o iguales al nodo actual van al subárbol izquierdo; los valores mayores van al subárbol derecho).

Pruébalo tú mismo

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binarytree.h"

int main() {
    BinaryTree bt;
    BinaryTree_init(&bt);
    char line[256];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "insert") == 0) {
            char* arg = strtok(NULL, " \t");
            if (arg) BinaryTree_insert(&bt, atoi(arg));
        }
        if (strcmp(cmd, "inOrderPrint") == 0) {
            BinaryTree_inOrderPrint(&bt);
        }
    }
    return 0;
}

Todas las lecciones de Árbol Binario - Serie de Estructuras de Datos #3