从team.txt中读取参赛队伍的基本信息,能够管理各参赛队的基本信息(包含参赛队编号,参赛作品名称,参赛学校,赛事类别,参赛者,指导老师),赛事类别共11项(参见大赛官网jsjds.blcu.edu.cn);包括增加、删除、修改参赛队伍的信息。 (2)实现基于二叉排序树的查找。根据提示输入参赛队编号,若查找成功,输出该赛事类别对应的基本信息(参赛作品名称、参赛学校、赛事类别、参赛者和指导老师信息),同时,输出查找成功时的平均查找长度ASL;否则,输出“查找失败!”。
时间: 2024-02-29 09:52:17 浏览: 78
实现这个功能需要以下步骤:
1. 从team.txt中读取参赛队伍的基本信息,存储到二叉排序树中。
2. 实现增加、删除、修改参赛队伍信息的函数。
3. 实现基于二叉排序树的查找函数。
4. 根据输入的参赛队编号,调用查找函数进行查找,输出查找结果。
以下是示例代码:
```python
class Team:
def __init__(self, team_id, name, school, category, members, teachers):
self.team_id = team_id
self.name = name
self.school = school
self.category = category
self.members = members
self.teachers = teachers
class TreeNode:
def __init__(self, team):
self.team = team
self.left = None
self.right = None
class TeamManager:
def __init__(self):
self.root = None
self.total_length = 0
self.total_count = 0
def add_team(self, team):
if not self.root:
self.root = TreeNode(team)
else:
self._add_team(self.root, team)
def _add_team(self, node, team):
if team.team_id < node.team.team_id:
if not node.left:
node.left = TreeNode(team)
else:
self._add_team(node.left, team)
elif team.team_id > node.team.team_id:
if not node.right:
node.right = TreeNode(team)
else:
self._add_team(node.right, team)
def delete_team(self, team_id):
self.root = self._delete_team(self.root, team_id)
def _delete_team(self, node, team_id):
if not node:
return None
if team_id < node.team.team_id:
node.left = self._delete_team(node.left, team_id)
elif team_id > node.team.team_id:
node.right = self._delete_team(node.right, team_id)
else:
if not node.left:
return node.right
elif not node.right:
return node.left
else:
min_node = self._find_min(node.right)
node.team = min_node.team
node.right = self._delete_team(node.right, min_node.team.team_id)
return node
def _find_min(self, node):
while node.left:
node = node.left
return node
def modify_team(self, team):
self.delete_team(team.team_id)
self.add_team(team)
def find_team(self, team_id):
self.total_length = 0
self.total_count = 0
node = self._find_team(self.root, team_id)
if node:
print("参赛作品名称:", node.team.name)
print("参赛学校:", node.team.school)
print("赛事类别:", node.team.category)
print("参赛者:", node.team.members)
print("指导老师:", node.team.teachers)
print("平均查找长度ASL:", self.total_length / self.total_count)
else:
print("查找失败!")
def _find_team(self, node, team_id):
if not node:
return None
self.total_length += 1
self.total_count += 1
if team_id == node.team.team_id:
return node
elif team_id < node.team.team_id:
return self._find_team(node.left, team_id)
else:
return self._find_team(node.right, team_id)
teams = TeamManager()
# 从team.txt中读取参赛队伍的基本信息,存储到二叉排序树中
with open('team.txt', 'r') as f:
for line in f:
team_info = line.split()
team_id = int(team_info[0])
name = team_info[1]
school = team_info[2]
category = team_info[3]
members = team_info[4]
teachers = team_info[5]
team = Team(team_id, name, school, category, members, teachers)
teams.add_team(team)
# 调用增加、删除、修改参赛队伍信息的函数
team = Team(12, '作品名称', '学校', '赛事类别', '参赛者', '指导老师')
teams.add_team(team)
teams.delete_team(12)
team = Team(13, '作品名称', '学校', '赛事类别', '参赛者', '指导老师')
teams.modify_team(team)
# 根据输入的参赛队编号,调用查找函数进行查找,输出查找结果
teams.find_team(1)
```
以上代码实现了从team.txt中读取参赛队伍的基本信息,并存储到二叉排序树中。同时,实现了增加、删除、修改参赛队伍信息的函数,以及基于二叉排序树的查找函数。最后,根据输入的参赛队编号,调用查找函数进行查找,并输出查找结果。
阅读全文