怎么用java用树的知识是实现族谱查询和添加
时间: 2024-03-14 22:43:43 浏览: 119
家族树应用程序:使用Java的家族树生成器
使用Java实现族谱查询和添加,需要先定义一个节点类来表示家庭成员,包括其姓名、性别、出生日期等信息,并且每个节点还包含指向其父亲节点和子节点的引用。具体实现可以参考以下代码:
```java
class TreeNode {
private String name;
private String gender;
private String birth;
private TreeNode parent;
private List<TreeNode> children;
public TreeNode(String name, String gender, String birth) {
this.name = name;
this.gender = gender;
this.birth = birth;
this.parent = null;
this.children = new ArrayList<>();
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public void addChild(TreeNode child) {
children.add(child);
child.setParent(this);
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getBirth() {
return birth;
}
public TreeNode getParent() {
return parent;
}
public List<TreeNode> getChildren() {
return children;
}
}
```
在定义好节点类之后,就可以使用树的数据结构来存储家族的族谱了。可以定义一个根节点来表示家族的祖先,然后通过添加子节点的方式来扩展树形结构。具体实现可以参考以下代码:
```java
public class FamilyTree {
private TreeNode root;
public FamilyTree(String rootName, String rootGender, String rootBirth) {
this.root = new TreeNode(rootName, rootGender, rootBirth);
}
public TreeNode getRoot() {
return root;
}
public void addNode(String parentName, String parentGender, String parentBirth, String childName, String childGender, String childBirth) {
TreeNode parentNode = findNode(root, parentName, parentGender, parentBirth);
if (parentNode != null) {
TreeNode childNode = new TreeNode(childName, childGender, childBirth);
parentNode.addChild(childNode);
}
}
public TreeNode findNode(TreeNode node, String name, String gender, String birth) {
if (node == null) {
return null;
}
if (node.getName().equals(name) && node.getGender().equals(gender) && node.getBirth().equals(birth)) {
return node;
}
for (TreeNode childNode : node.getChildren()) {
TreeNode result = findNode(childNode, name, gender, birth);
if (result != null) {
return result;
}
}
return null;
}
}
```
在实现好族谱添加和查询的功能之后,就可以通过调用相应的方法来实现具体的操作了。例如,可以先创建一个家族树对象,然后添加一些家庭成员,最后通过遍历树来查询某个家庭成员的信息。具体实现可以参考以下代码:
```java
public class Main {
public static void main(String[] args) {
FamilyTree familyTree = new FamilyTree("祖先", "男", "1900-01-01");
familyTree.addNode("祖先", "男", "1900-01-01", "父亲", "男", "1930-01-01");
familyTree.addNode("祖先", "男", "1900-01-01", "叔叔", "男", "1935-01-01");
familyTree.addNode("祖先", "女", "1900-01-01", "姑姑", "女", "1938-01-01");
familyTree.addNode("父亲", "男", "1930-01-01", "我", "男", "1980-01-01");
familyTree.addNode("父亲", "女", "1932-01-01", "姐姐", "女", "1965-01-01");
// 查询我父亲的姓名、性别、出生日期
TreeNode parentNode = familyTree.findNode(familyTree.getRoot(), "父亲", "男", "1930-01-01");
if (parentNode != null) {
System.out.println("姓名:" + parentNode.getName() + ",性别:" + parentNode.getGender() + ",出生日期:" + parentNode.getBirth());
}
// 查询我祖先的所有子孙
List<TreeNode> descendants = new ArrayList<>();
getDescendants(familyTree.getRoot(), descendants);
for (TreeNode descendant : descendants) {
System.out.println("姓名:" + descendant.getName() + ",性别:" + descendant.getGender() + ",出生日期:" + descendant.getBirth());
}
}
public static void getDescendants(TreeNode node, List<TreeNode> descendants) {
if (node == null) {
return;
}
descendants.add(node);
for (TreeNode childNode : node.getChildren()) {
getDescendants(childNode, descendants);
}
}
}
```
通过以上的代码,就可以实现一个简单的家族族谱查询和添加功能了。
阅读全文