Node with Sons
Coddy의 이진 트리 - 자료구조 시리즈 #3 코스 레슨 — 13개 중 4번째.
챌린지
쉬움Node에 두 개의 자식(left와 right)을 지원하도록 기능을 추가하세요. 생성자는 두 자식을 모두 null/None 등으로 초기화해야 합니다. getLeft / setLeft 및 getRight / setRight 메서드를 추가하세요.
직접 해보기
#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) Node_setValue(&n, atoi(arg));
}
if (strcmp(cmd, "getValue") == 0) {
printf("%d\n", Node_getValue(&n));
}
if (strcmp(cmd, "setLeft") == 0) {
char* arg = strtok(NULL, " \t");
Node* c = Node_new();
Node_setValue(c, atoi(arg));
Node_setLeft(&n, c);
}
if (strcmp(cmd, "getLeftValue") == 0) {
printf("%d\n", Node_getValue(Node_getLeft(&n)));
}
if (strcmp(cmd, "setRight") == 0) {
char* arg = strtok(NULL, " \t");
Node* c = Node_new();
Node_setValue(c, atoi(arg));
Node_setRight(&n, c);
}
if (strcmp(cmd, "getRightValue") == 0) {
printf("%d\n", Node_getValue(Node_getRight(&n)));
}
}
return 0;
}
이진 트리 - 자료구조 시리즈 #3의 모든 레슨
2Binary Tree Project
Node ClassNode with Sons