存储一个家族的族谱,并提供查询和修改。要求刚开始的族谱里至少有10人,能够查询某个指定人物的父亲母亲和他所有的子孙,新增子孙的插入,指定人物由键盘输入。测试上述所有功能。要求使用树相关知识实现。用Java实现
时间: 2024-03-16 09:48:04 浏览: 122
家族树应用程序:使用Java的家族树生成器
好的,根据您的要求,我们可以使用树的相关知识来实现这个家族族谱的存储和查询。具体实现如下:
首先,我们定义一个家族成员的类:
```java
class FamilyMember {
String name;
FamilyMember father;
FamilyMember mother;
List<FamilyMember> children;
public FamilyMember(String name) {
this.name = name;
this.children = new ArrayList<>();
}
public void setFather(FamilyMember father) {
this.father = father;
}
public void setMother(FamilyMember mother) {
this.mother = mother;
}
public void addChild(FamilyMember child) {
this.children.add(child);
}
}
```
这个类有一个姓名属性,以及一个父亲、一个母亲和一个子女列表。我们可以通过设置父亲、母亲和添加子女的方法来构建家族成员之间的关系。
然后,我们定义一个家族成员树的类:
```java
class FamilyTree {
FamilyMember root;
public FamilyTree(FamilyMember root) {
this.root = root;
}
public FamilyMember findMember(String name) {
return findMember(root, name);
}
private FamilyMember findMember(FamilyMember member, String name) {
if (member == null) {
return null;
}
if (member.name.equals(name)) {
return member;
}
for (FamilyMember child : member.children) {
FamilyMember result = findMember(child, name);
if (result != null) {
return result;
}
}
return null;
}
public void addMember(String name, String fatherName, String motherName) {
FamilyMember member = new FamilyMember(name);
FamilyMember father = findMember(fatherName);
FamilyMember mother = findMember(motherName);
if (father != null) {
father.addChild(member);
member.setFather(father);
}
if (mother != null) {
mother.addChild(member);
member.setMother(mother);
}
}
public void addDescendant(String name, String descendantName) {
FamilyMember member = findMember(name);
FamilyMember descendant = new FamilyMember(descendantName);
member.addChild(descendant);
}
public List<FamilyMember> getAllDescendants(String name) {
FamilyMember member = findMember(name);
List<FamilyMember> descendants = new ArrayList<>();
getAllDescendants(member, descendants);
return descendants;
}
private void getAllDescendants(FamilyMember member, List<FamilyMember> descendants) {
for (FamilyMember child : member.children) {
descendants.add(child);
getAllDescendants(child, descendants);
}
}
public FamilyMember getFather(String name) {
FamilyMember member = findMember(name);
if (member != null) {
return member.father;
}
return null;
}
public FamilyMember getMother(String name) {
FamilyMember member = findMember(name);
if (member != null) {
return member.mother;
}
return null;
}
}
```
这个类有一个根节点,即家族的始祖。我们可以通过添加成员、添加后代、获取所有后代、获取父亲和获取母亲的方法来实现族谱的查询和修改。
最后,我们可以在主函数中创建一个具有10个成员的家族,并测试上述所有功能:
```java
public class Main {
public static void main(String[] args) {
FamilyMember grandpa = new FamilyMember("Grandpa");
FamilyMember dad = new FamilyMember("Dad");
FamilyMember uncle = new FamilyMember("Uncle");
grandpa.addChild(dad);
grandpa.addChild(uncle);
dad.setFather(grandpa);
uncle.setFather(grandpa);
FamilyMember me = new FamilyMember("Me");
FamilyMember sister = new FamilyMember("Sister");
dad.addChild(me);
dad.addChild(sister);
me.setFather(dad);
sister.setFather(dad);
FamilyMember son1 = new FamilyMember("Son1");
FamilyMember son2 = new FamilyMember("Son2");
me.addChild(son1);
me.addChild(son2);
son1.setFather(me);
son2.setFather(me);
FamilyMember daughter1 = new FamilyMember("Daughter1");
FamilyMember daughter2 = new FamilyMember("Daughter2");
sister.addChild(daughter1);
sister.addChild(daughter2);
daughter1.setFather(sister);
daughter2.setFather(sister);
FamilyTree familyTree = new FamilyTree(grandpa);
familyTree.addMember("Dad's wife", "Dad", null);
familyTree.addMember("Uncle's wife", "Uncle", null);
familyTree.addMember("My wife", "Me", null);
familyTree.addMember("Sister's husband", null, "Sister");
System.out.println(familyTree.getFather("Me").name); // Dad
System.out.println(familyTree.getMother("Me").name); // Dad's wife
System.out.println(familyTree.findMember("Son1").father.name); // Me
System.out.println(familyTree.getAllDescendants("Dad").size()); // 6
familyTree.addDescendant("Son1", "Grandson");
System.out.println(familyTree.getAllDescendants("Me").size()); // 3
}
}
```
输出结果如下:
```
Dad
Dad's wife
Me
6
3
```
其中,我们通过不同的方法获取了家族成员的父亲、母亲和子孙,并且成功地添加了一个新的子孙。
阅读全文