Node Class
Lección 3 de 13 del curso Árbol Binario - Serie de Estructuras de Datos #3 de Coddy.
Comencemos por construir un Node — el bloque de construcción básico de un árbol binario.
Desafío
FácilEscribe una clase Node que tenga:
- Constructor que no recibe ninguna entrada e inicializa el valor del nodo a
0. - Método
getValueque no recibe ninguna entrada y devuelve el valor del nodo. - Método
setValueque recibe un entero y establece el valor del nodo con él.
Pruébalo tú mismo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
int main() {
Node n;
Node_init(&n);
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, "setValue") == 0) {
char* arg = strtok(NULL, " \t");
if (arg) setValue(&n, atoi(arg));
}
if (strcmp(cmd, "getValue") == 0) {
printf("%d\n", getValue(&n));
}
}
return 0;
}
Todas las lecciones de Árbol Binario - Serie de Estructuras de Datos #3
2Binary Tree Project
Node ClassNode with Sons