BinaryTree Class
Leçon 5 sur 13 du cours Arbre binaire - Structures de données n°3 de Coddy.
Défi
FacileÉcrivez une classe BinaryTree avec un constructeur (initialise la racine à null/None), getRoot, et setRoot.
Essayez vous-même
#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, "setRoot") == 0) {
char* arg = strtok(NULL, " \t");
Node* c = Node_new();
Node_setValue(c, atoi(arg));
BinaryTree_setRoot(&bt, c);
}
if (strcmp(cmd, "getRootValue") == 0) {
printf("%d\n", Node_getValue(BinaryTree_getRoot(&bt)));
}
}
return 0;
}
Toutes les leçons de Arbre binaire - Structures de données n°3
2Binary Tree Project
Node ClassNode with Sons