家族族谱二叉树c语言
时间: 2023-12-30 10:23:29 浏览: 121
家族族谱系统 二叉树
5星 · 资源好评率100%
以下是一个用C语言实现家族族谱二叉树的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义家族成员结构体
typedef struct FamilyMember {
char name[50];
struct FamilyMember* father;
struct FamilyMember* mother;
struct FamilyMember* leftChild;
struct FamilyMember* rightSibling;
} FamilyMember;
// 创建家族成员
FamilyMember* createFamilyMember(char* name) {
FamilyMember* member = (FamilyMember*)malloc(sizeof(FamilyMember));
strcpy(member->name, name);
member->father = NULL;
member->mother = NULL;
member->leftChild = NULL;
member->rightSibling = NULL;
return member;
}
// 插入新成员
void insertFamilyMember(FamilyMember* root, char* parentName, char* newMemberName) {
if (root == NULL) {
return;
}
if (strcmp(root->name, parentName) == 0) {
FamilyMember* newMember = createFamilyMember(newMemberName);
newMember->father = root;
if (root->leftChild == NULL) {
root->leftChild = newMember;
} else {
FamilyMember* sibling = root->leftChild;
while (sibling->rightSibling != NULL) {
sibling = sibling->rightSibling;
}
sibling->rightSibling = newMember;
}
} else {
insertFamilyMember(root->leftChild, parentName, newMemberName);
insertFamilyMember(root->rightSibling, parentName, newMemberName);
}
}
// 删除成员
void deleteFamilyMember(FamilyMember* root, char* memberName) {
if (root == NULL) {
return;
}
if (root->leftChild != NULL && strcmp(root->leftChild->name, memberName) == 0) {
FamilyMember* deleteMember = root->leftChild;
root->leftChild = deleteMember->rightSibling;
free(deleteMember);
} else {
deleteFamilyMember(root->leftChild, memberName);
deleteFamilyMember(root->rightSibling, memberName);
}
}
// 查找成员
FamilyMember* findFamilyMember(FamilyMember* root, char* memberName) {
if (root == NULL) {
return NULL;
}
if (strcmp(root->name, memberName) == 0) {
return root;
} else {
FamilyMember* result = findFamilyMember(root->leftChild, memberName);
if (result == NULL) {
result = findFamilyMember(root->rightSibling, memberName);
}
return result;
}
}
// 判断两个人的家族关系
int isFamilyRelation(FamilyMember* member1, FamilyMember* member2) {
if (member1 == NULL || member2 == NULL) {
return 0;
}
if (member1 == member2) {
return 1;
}
return isFamilyRelation(member1->father, member2) || isFamilyRelation(member1->mother, member2);
}
// 打印家族成员
void printFamilyMember(FamilyMember* member, int level) {
if (member == NULL) {
return;
}
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s\n", member->name);
printFamilyMember(member->leftChild, level + 1);
printFamilyMember(member->rightSibling, level);
}
int main() {
// 创建家族族谱根节点
FamilyMember* root = createFamilyMember("Root");
// 插入新成员
insertFamilyMember(root, "Root", "Father");
insertFamilyMember(root, "Root", "Mother");
insertFamilyMember(root, "Father", "Child1");
insertFamilyMember(root, "Father", "Child2");
insertFamilyMember(root, "Mother", "Child3");
// 删除成员
deleteFamilyMember(root, "Child2");
// 查找成员
FamilyMember* member = findFamilyMember(root, "Child1");
if (member != NULL) {
printf("Found member: %s\n", member->name);
} else {
printf("Member not found.\n");
}
// 判断家族关系
FamilyMember* member1 = findFamilyMember(root, "Child1");
FamilyMember* member2 = findFamilyMember(root, "Child3");
if (isFamilyRelation(member1, member2)) {
printf("They are family.\n");
} else {
printf("They are not family.\n");
}
// 打印家族成员
printFamilyMember(root, 0);
return 0;
}
```
阅读全文