用Java编写代码:从team.txt中读取参赛队伍的基本信息,实现基于二叉排序树的查找。根据提示输入参赛队编号,若查找成功,输出该赛事类别对应的基本信息(参赛作品名称、参赛学校、赛事类别、参赛者和指导老师信息),同时,输出查找成功时的平均查找长度ASL;否则,输出“查找失败!”。
时间: 2024-03-12 14:45:07 浏览: 21
好的,以下是Java代码实现:
```java
import java.io.*;
import java.util.*;
class TeamNode {
String teamNo; // 队伍编号
String projectName; // 参赛作品名称
String school; // 参赛学校
String eventType; // 赛事类别
String participant; // 参赛者信息
String teacher; // 指导老师信息
TeamNode left, right; // 左右子节点
public TeamNode(String teamNo, String projectName, String school, String eventType, String participant, String teacher) {
this.teamNo = teamNo;
this.projectName = projectName;
this.school = school;
this.eventType = eventType;
this.participant = participant;
this.teacher = teacher;
left = right = null;
}
}
class BinarySearchTree {
TeamNode root; // 根节点
public BinarySearchTree() {
root = null;
}
// 插入节点
void insert(String teamNo, String projectName, String school, String eventType, String participant, String teacher) {
root = insertRec(root, teamNo, projectName, school, eventType, participant, teacher);
}
TeamNode insertRec(TeamNode root, String teamNo, String projectName, String school, String eventType, String participant, String teacher) {
if (root == null) {
root = new TeamNode(teamNo, projectName, school, eventType, participant, teacher);
return root;
}
if (teamNo.compareTo(root.teamNo) < 0)
root.left = insertRec(root.left, teamNo, projectName, school, eventType, participant, teacher);
else if (teamNo.compareTo(root.teamNo) > 0)
root.right = insertRec(root.right, teamNo, projectName, school, eventType, participant, teacher);
return root;
}
// 查找节点
TeamNode search(String teamNo) {
int[] asl = new int[1];
asl[0] = 0;
return searchRec(root, teamNo, asl);
}
TeamNode searchRec(TeamNode root, String teamNo, int[] asl) {
if (root == null || root.teamNo.equals(teamNo))
return root;
asl[0]++; // 计算ASL
if (teamNo.compareTo(root.teamNo) < 0)
return searchRec(root.left, teamNo, asl);
else
return searchRec(root.right, teamNo, asl);
}
}
public class Main {
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
// 从team.txt中读取数据并插入到二叉排序树中
try {
BufferedReader br = new BufferedReader(new FileReader("team.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] info = line.split("\\s+");
bst.insert(info[0], info[1], info[2], info[3], info[4], info[5]);
}
} catch (IOException e) {
e.printStackTrace();
}
// 输入参赛队编号并查找
Scanner sc = new Scanner(System.in);
System.out.print("请输入参赛队编号:");
String teamNo = sc.next();
TeamNode result = bst.search(teamNo);
if (result != null) {
System.out.println(result.projectName + " " + result.school + " " + result.eventType + " " + result.participant + " " + result.teacher);
System.out.println("查找成功!ASL为:" + (float)bst.search(teamNo, new int[1]).get(1) / bst.search(teamNo, new int[1]).get(0));
} else {
System.out.println("查找失败!");
}
}
}
```
其中,`team.txt`是存储参赛队伍信息的文件,每行格式为:`参赛队编号 参赛作品名称 参赛学校 赛事类别 参赛者信息 指导老师信息`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)