数据结构家谱管理系统代码
时间: 2023-07-06 16:10:41 浏览: 112
数据结构家谱图代码.doc
这是一个比较复杂的系统,需要设计多个数据结构来管理家谱信息。以下是一个简单的代码示例,可以作为参考:
```python
class Person:
def __init__(self, name, gender, birthdate):
self.name = name
self.gender = gender
self.birthdate = birthdate
self.parents = []
self.children = []
def add_parent(self, parent):
self.parents.append(parent)
def add_child(self, child):
self.children.append(child)
class FamilyTree:
def __init__(self):
self.people = {}
def add_person(self, person):
self.people[person.name] = person
def find_person(self, name):
return self.people.get(name)
def add_relationship(self, parent_name, child_name):
parent = self.find_person(parent_name)
child = self.find_person(child_name)
if parent and child:
parent.add_child(child)
child.add_parent(parent)
else:
raise ValueError("Person not found in family tree")
# 示例用法
tree = FamilyTree()
# 添加家庭成员
alice = Person("Alice", "Female", "1990-01-01")
bob = Person("Bob", "Male", "1980-01-01")
charlie = Person("Charlie", "Male", "2010-01-01")
tree.add_person(alice)
tree.add_person(bob)
tree.add_person(charlie)
# 建立关系
tree.add_relationship("Bob", "Alice")
tree.add_relationship("Alice", "Charlie")
# 查找家庭成员
person = tree.find_person("Alice")
print(person.name) # 输出 "Alice"
# 查找家庭成员的父母和子女
parents = person.parents
children = person.children
print([p.name for p in parents]) # 输出 ["Bob"]
print([c.name for c in children]) # 输出 ["Charlie"]
```
以上代码仅作为示例,实际的家谱管理系统可能需要更复杂的数据结构和算法来支持更多功能,如搜索、排序、删除等。
阅读全文