BinaryTree Class
Lektion 5 von 13 im Kurs Binärbaum - Datenstrukturen-Serie #3 von Coddy.
Aufgabe
EinfachSchreibe eine Klasse BinaryTree mit einem Konstruktor (initialisiert die Wurzel auf null/None), getRoot und setRoot.
Probier es selbst
#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;
}
Alle Lektionen in Binärbaum - Datenstrukturen-Serie #3
2Binary Tree Project
Node ClassNode with Sons