Lớp BinaryTree
Bài học 5 trên 13 trong khóa học Cây nhị phân, chuỗi cấu trúc dữ liệu #3 của Coddy.
Thử thách
DễViết một lớp BinaryTree có hàm khởi tạo (khởi tạo nút gốc thành null/None), getRoot và setRoot.
Tự làm thử
#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;
}
Tất cả bài học trong Cây nhị phân, chuỗi cấu trúc dữ liệu #3
2Dự án cây nhị phân
Lớp nútNút có nút conTự luyện tập: Trình biên dịch C online