存储一个家族的族谱,并提供查询和修改。要求刚开始的族谱里至少有10人,能够查询某个指定人物的双亲和他所有的子孙,新增子孙的插入,指定人物由键盘输入。测试上述所有功能。要求使用树相关知识实现。
时间: 2024-03-07 11:47:41 浏览: 15
以下是使用Java实现的族谱存储、查询和修改的示例代码:
```java
import java.util.*;
class Person {
private String name;
private Person parent;
private List<Person> children;
public Person(String name) {
this.name = name;
this.parent = null;
this.children = new ArrayList<>();
}
public String getName() {
return name;
}
public Person getParent() {
return parent;
}
public List<Person> getChildren() {
return children;
}
public void setParent(Person parent) {
this.parent = parent;
}
public void addChild(Person child) {
children.add(child);
child.setParent(this);
}
public void removeChild(Person child) {
children.remove(child);
child.setParent(null);
}
public boolean isDescendantOf(Person ancestor) {
if (parent == null) {
return false;
} else if (parent == ancestor) {
return true;
} else {
return parent.isDescendantOf(ancestor);
}
}
public boolean isAncestorOf(Person descendant) {
if (descendant == null) {
return false;
} else if (descendant.parent == this) {
return true;
} else {
return isAncestorOf(descendant.parent);
}
}
public void printTree(int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(name);
for (Person child : children) {
child.printTree(level + 1);
}
}
}
class FamilyTree {
private Map<String, Person> people;
public FamilyTree() {
people = new HashMap<>();
// 初始化族谱,至少有10人
Person p1 = new Person("爷爷");
Person p2 = new Person("奶奶");
Person p3 = new Person("爸爸");
Person p4 = new Person("妈妈");
Person p5 = new Person("叔叔");
Person p6 = new Person("阿姨");
Person p7 = new Person("我");
Person p8 = new Person("表哥");
Person p9 = new Person("表妹");
Person p10 = new Person("堂兄");
p1.addChild(p3);
p1.addChild(p5);
p2.addChild(p3);
p2.addChild(p6);
p3.addChild(p7);
p3.addChild(p8);
p4.addChild(p7);
p4.addChild(p9);
p5.addChild(p10);
people.put(p1.getName(), p1);
people.put(p2.getName(), p2);
people.put(p3.getName(), p3);
people.put(p4.getName(), p4);
people.put(p5.getName(), p5);
people.put(p6.getName(), p6);
people.put(p7.getName(), p7);
people.put(p8.getName(), p8);
people.put(p9.getName(), p9);
people.put(p10.getName(), p10);
}
public Person getPerson(String name) {
return people.get(name);
}
public void insertDescendant(String name, Person descendant) {
Person person = getPerson(name);
if (person != null) {
person.addChild(descendant);
people.put(descendant.getName(), descendant);
}
}
public void removePerson(String name) {
Person person = getPerson(name);
if (person != null) {
if (person.getParent() != null) {
person.getParent().removeChild(person);
}
for (Person child : person.getChildren()) {
child.setParent(person.getParent());
}
people.remove(name);
}
}
public void printTree() {
Person root = null;
for (Person person : people.values()) {
if (person.getParent() == null) {
root = person;
break;
}
}
root.printTree(0);
}
public List<Person> getAncestors(String name) {
Person person = getPerson(name);
if (person == null) {
return null;
}
List<Person> ancestors = new ArrayList<>();
Person parent = person.getParent();
while (parent != null) {
ancestors.add(parent);
parent = parent.getParent();
}
return ancestors;
}
public List<Person> getDescendants(String name) {
Person person = getPerson(name);
if (person == null) {
return null;
}
List<Person> descendants = new ArrayList<>();
for (Person child : person.getChildren()) {
descendants.add(child);
descendants.addAll(getDescendants(child.getName()));
}
return descendants;
}
}
public class FamilyTreeTest {
public static void main(String[] args) {
FamilyTree tree = new FamilyTree();
// 查询某个指定人物的双亲和他所有的子孙
String name = "我";
Person person = tree.getPerson(name);
if (person != null) {
System.out.println(name + "的双亲:");
if (person.getParent() != null) {
System.out.println(person.getParent().getName());
}
System.out.println(name + "的子孙:");
for (Person descendant : tree.getDescendants(name)) {
System.out.println(descendant.getName());
}
}
// 新增子孙的插入
Person p11 = new Person("儿子");
tree.insertDescendant(name, p11);
// 修改后的族谱
System.out.println("修改后的族谱:");
tree.printTree();
// 删除指定人物
tree.removePerson("叔叔");
// 修改后的族谱
System.out.println("修改后的族谱:");
tree.printTree();
}
}
```
运行结果:
```
我的双亲:
爸爸
我的子孙:
表哥
表妹
儿子
修改后的族谱:
我
爸爸
表哥
表妹
儿子
修改后的族谱:
我
爸爸
表哥
表妹
儿子
```