用C语言树形结构完成电子家谱成员信息的建立、查找、插入、修改、删除
时间: 2023-09-23 09:12:24 浏览: 119
好的,下面我给您讲解一下用C语言树形结构完成电子家谱成员信息的建立、查找、插入、修改、删除的方法。
首先,我们需要定义一个结构体来表示家谱成员的信息,包括姓名、性别、出生日期、死亡日期、父亲和母亲等信息。代码如下:
```c
typedef struct _family_member {
char name[20]; // 姓名
char gender; // 性别,M表示男性,F表示女性
char birthdate[20]; // 出生日期,格式为yyyy-mm-dd
char deathdate[20]; // 死亡日期,格式为yyyy-mm-dd,未死亡则为"0000-00-00"
struct _family_member* father; // 父亲
struct _family_member* mother; // 母亲
struct _family_member* left_child; // 左子节点
struct _family_member* right_sibling; // 右兄弟节点
} FamilyMember;
```
接着,我们定义一个函数来创建家谱成员,该函数接收姓名、性别、出生日期、死亡日期、父亲和母亲等参数,并返回一个FamilyMember结构体指针。代码如下:
```c
FamilyMember* create_family_member(char* name, char gender, char* birthdate, char* deathdate, FamilyMember* father, FamilyMember* mother) {
FamilyMember* member = (FamilyMember*)malloc(sizeof(FamilyMember));
strcpy(member->name, name);
member->gender = gender;
strcpy(member->birthdate, birthdate);
strcpy(member->deathdate, deathdate);
member->father = father;
member->mother = mother;
member->left_child = NULL;
member->right_sibling = NULL;
return member;
}
```
接下来,我们需要定义一个树形结构,用于存储家谱成员的信息。代码如下:
```c
typedef struct _family_tree {
FamilyMember* root; // 根节点
} FamilyTree;
```
然后,我们定义一些函数来操作这个树形结构:
1. 查找家谱成员
```c
FamilyMember* find_family_member(FamilyMember* member, char* name) {
if (member == NULL) {
return NULL;
}
if (strcmp(member->name, name) == 0) {
return member;
}
FamilyMember* left_result = find_family_member(member->left_child, name);
if (left_result != NULL) {
return left_result;
}
return find_family_member(member->right_sibling, name);
}
```
2. 插入家谱成员
```c
void insert_family_member(FamilyMember* parent, FamilyMember* child) {
if (parent == NULL || child == NULL) {
return;
}
if (parent->left_child == NULL) {
parent->left_child = child;
} else {
FamilyMember* sibling = parent->left_child;
while (sibling->right_sibling != NULL) {
sibling = sibling->right_sibling;
}
sibling->right_sibling = child;
}
}
```
3. 修改家谱成员信息
```c
void modify_family_member(FamilyMember* member, char* name, char gender, char* birthdate, char* deathdate) {
if (member == NULL) {
return;
}
strcpy(member->name, name);
member->gender = gender;
strcpy(member->birthdate, birthdate);
strcpy(member->deathdate, deathdate);
}
```
4. 删除家谱成员
```c
void delete_family_member(FamilyMember* member) {
if (member == NULL) {
return;
}
delete_family_member(member->left_child);
delete_family_member(member->right_sibling);
free(member);
}
```
最后,我们可以编写一个测试程序来使用这些函数。代码如下:
```c
int main() {
// 创建家谱成员
FamilyMember* grandpa = create_family_member("张三", 'M', "1900-01-01", "1970-01-01", NULL, NULL);
FamilyMember* grandma = create_family_member("李四", 'F', "1900-01-01", "1980-01-01", NULL, NULL);
FamilyMember* father = create_family_member("王五", 'M', "1920-01-01", "2000-01-01", grandpa, grandma);
FamilyMember* mother = create_family_member("赵六", 'F', "1925-01-01", "2010-01-01", NULL, NULL);
FamilyMember* son1 = create_family_member("钱七", 'M', "1950-01-01", "0000-00-00", father, mother);
FamilyMember* son2 = create_family_member("孙八", 'M', "1955-01-01", "0000-00-00", father, mother);
FamilyMember* daughter1 = create_family_member("周九", 'F', "1960-01-01", "0000-00-00", father, mother);
// 创建家谱树
FamilyTree* tree = (FamilyTree*)malloc(sizeof(FamilyTree));
tree->root = grandpa;
// 查找家谱成员
FamilyMember* result = find_family_member(tree->root, "孙八");
if (result != NULL) {
printf("找到了家谱成员:%s\n", result->name);
} else {
printf("没有找到家谱成员\n");
}
// 插入家谱成员
insert_family_member(father, daughter1);
// 修改家谱成员信息
modify_family_member(son1, "钱七七", 'M', "1950-01-02", "0000-00-00");
// 删除家谱成员
delete_family_member(son2);
return 0;
}
```
以上就是用C语言树形结构完成电子家谱成员信息的建立、查找、插入、修改、删除的方法。希望对您有所帮助。
阅读全文