ここまではできましたが最後の部分ができませんでした。市のデータを構造体として6つの市をノードとする二分探索木を作りたいです。また入力された市の名前と人口に対し、これらを格納するノードをメモリ確保し作成し、二分探索木となるようにノードを追加するプログラムです。わかる方お願いします。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct cityTN {
char name[20];
int population;
struct cityTN *left;
struct cityTN *right;
};
void printCitiesTree(int indent, struct cityTN *s);
struct cityTN *tuikaCity(struct cityTN *ct, char *str, int num);
int main(){
struct cityTN c1 = {"Hiratsuka", 258000, NULL, NULL},
c2 = {"Odawara", 190000, NULL, NULL},
c3 = {"Yokosuka", 396000, NULL, NULL},
c4 = {"Kamakura", 172000, NULL, NULL},
c5 = {"Chigasaki", 242000, NULL, NULL},
c6 = {"Fujisawa", 434000, NULL, NULL};
struct cityTN *root = NULL;
root = &c1; c1.left = &c2; c1.right = &c3; c2.left = &c4; c2.right = &c5; c3.right = &c6;
char str[20];
int num;
printf("input a city name and its population : ");
fflush(stdout);
scanf("%s %d", str, &num);
root = tuikaCity(root, str, num);
printCitiesTree(0, root);
printf("\n\n");
printf("input a city name and its population: ");
fflush(stdout);
scanf("%s %d", str, &num);
root = tuikaCity(root, str, num);
printCitiesTree(0, root);
printf("\n\n");
return 0;
}
void printCitiesTree(int indent, struct cityTN *ct){
int i;
if (ct == NULL) printf("NULL");
else {
printf("\n");
for (i = 0; i < indent; i++) printf(" ");
printf("(%-10s: population = %6d) ", ct->name, ct->population);
printCitiesTree(indent + 5, ct->left);
printf(", ");
printCitiesTree(indent + 5, ct->right);
printf(") ");
}
}
struct cityTN *tuikaCity(struct cityTN *ct, char *str, int num)
{
}
二分探索木 C言語
で検索して見つかるこんな記事が参考になるかもしれません。C言語で二分探索木(木構造・ツリー構造)をプログラミング, 二分探索木, C言語アルゴリズム-二分探索木