参赛队伍信息查询

#  参赛队伍信息查询
class TreeNode:  # 节点类
    def __init__(self, value=None):
        self.left = None  # 左子节点
        self.right = None  # 右子节点
        self.value = value   # 节点的值,初始化为空值None


def read_txt(file_path):   # 读取txt文件,将每个参赛队伍的信息保存为一个字典对象,并以赛事编号为key,将字典对象存储到字典teams中
    teams = {}
    if os.path.exists(file_path):  # 判断文件是否存在
        with open(file_path, 'r', encoding='utf-8') as f:
            i = 0
            for line in f:
                if i > 0:
                    data = line.strip().split('#')
                    team = {
                        'id': int(data[0].strip()),
                        'name': data[1].strip(),
                        'school': data[2].strip(),
                        'category': data[3].strip(),
                        'members': data[4].strip(),
                        'adviser': data[5].strip()
                    }
                    teams[team['id']] = team
                i += 1
    return teams


def build_tree(teams):  # 构建二叉树
    root = TreeNode()
    for key in teams:
        node = TreeNode(teams[key])
        if not root.value:
            root.value = node.value
        else:
            cur = root
            while True:
                if key < cur.value['id']:
                    if not cur.left:
                        cur.left = node
                        break
                    else:
                        cur = cur.left
                elif key > cur.value['id']:
                    if not cur.right:
                        cur.right = node
                        break
                    else:
                        cur = cur.right
                else:
                    cur.value = node.value
    return root


def search_tree(root, key):  # 查找目标节点,跟根节点比较,等于:返回节点的值,小于:和左子树继续比较,大于:和右子树继续比较
    cur = root
    count = 0
    while cur:
        count += 1
        if key == cur.value['id']:
            return cur.value, count
        elif key < cur.value['id']:
            cur = cur.left
        else:
            cur = cur.right
    return None, count


def total_num(root):  # 总查找长度
    file_path = 'teams_exists.txt'
    teams = read_txt(file_path)
    total_count = 0
    total_numm = 0
    team_id = []
    for i in teams.keys():
        team_id.append(i)
    for t_id in team_id:
        team, count = search_tree(root, t_id)
        total_count += count
    total_numm += total_count
    return total_numm


def print_search_menu():   # 信息查询系统菜单
    print('===============信息查询系统===============')
    print('1. 显示所有参赛队伍信息')
    print('2. 按照参赛队伍编号查找')
    print('0. 退出程序')
    print('==========================================')


def search_team():
    file_path = 'teams_exists.txt'
    teams = read_txt(file_path)
    root = build_tree(teams)
    total_count = 0     # 总查找长度
    success_count = 0  # 查找成功的次数
    while True:
        print_search_menu()
        choice = input('请输入要执行的操作序号(0-2):')
        if choice == '1':   # 输出全部参赛队伍信息
            print("参赛队编号\t参赛作品名称\t参赛学校\t赛事类别\t参赛者\t指导老师")
            for key in teams:
                print(teams[key]['id'], '\t', teams[key]['name'], '\t',  teams[key]['school'], '\t',
                      teams[key]['category'], '\t',  teams[key]['members'], '\t',  teams[key]['adviser'])
        elif choice == '2':   # 按编号查找参赛队伍
            key = int(input('请输入要查找的参赛队伍编号:'))
            team, count = search_tree(root, key)
            total_count += count
            if team:
                print("参赛队编号\t参赛作品名称\t参赛学校\t赛事类别\t参赛者\t指导老师")
                print(team['id'], '\t', team['name'], '\t',  team['school'], '\t',
                      team['category'], '\t',  team['members'], '\t',  team['adviser'])
                success_count += 1
            else:
                print('没有找到该参赛队,请确认编号是否正确!')
        elif choice == '0':
            print('程序已退出。')
            break
        else:
            print('输入错误,请重新输入!')

        if success_count > 0:
            total_numm = total_num(root)
            asl = total_numm / 398
            print(f"查找成功次数:{success_count}, 查找长度:{total_count}")
            print(f'平均查找长度ASL:{asl:.3f}')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值