Insert
Coddyの「二分木 - データ構造シリーズ #3」コースのレッスン 10/13。
チャレンジ
簡単BinaryTree に、整数を受け取り、それを二分探索木に挿入するメソッド 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;
}