Menu
Coddy logo textTech

BinaryTree Class

Coddy의 이진 트리 - 자료구조 시리즈 #3 코스 레슨 — 13개 중 5번째.

challenge icon

챌린지

쉬움

생성자(root를 null/None으로 초기화), getRoot, 그리고 setRoot를 포함하는 BinaryTree 클래스를 작성하세요.

직접 해보기

#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;
}

이진 트리 - 자료구조 시리즈 #3의 모든 레슨