Menu
CoddyTech

BinaryTree 类

Coddy 二叉树:数据结构系列第 3 篇 课程的第 5 课,共 13 课。

challenge icon

挑战

简单

编写一个类 BinaryTree,包含一个构造函数(将根节点初始化为 null/None)、getRoot 和 setRoot。

亲手试试

#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 篇中的所有课

自己练习: C 在线编译器