Menu
Coddy logo textTech

Insert

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

challenge icon

챌린지

쉬움

BinaryTree에 정수를 인자로 받아 이진 탐색 트리(binary search tree)에 삽입하는 insert 메서드를 추가하세요. (현재 노드보다 작거나 같은 값은 왼쪽 서브트리로, 큰 값은 오른쪽 서브트리로 이동합니다.)

직접 해보기

#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, "insert") == 0) {
            char* arg = strtok(NULL, " \t");
            if (arg) BinaryTree_insert(&bt, atoi(arg));
        }
        if (strcmp(cmd, "inOrderPrint") == 0) {
            BinaryTree_inOrderPrint(&bt);
        }
    }
    return 0;
}

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