我有一项家庭作业,我需要将患者数据加载到节点中,然后才能搜索树。该节点将存储患者姓名,医生姓名,当前预约以及下一年度预约日期。从文本文件中读取数据。我想使用arrayList将数据存储到节点中,但令人困惑的部分是如何将arrayList的某些数据存储到每个节点? (我希望这是有道理的)。我在大约一天前发布了一些帮助来创建一个患者类,然后将其存储在节点内,然后存储在数组列表中。但我仍然迷失在于如何实现这一点。请记住,我在BST的O.o.很可怕。我选择在LinkedList上使用arrayList,因为我更习惯使用它们。 这是我在我的文本文件中阅读的课程......(数组实现并不完整)。
public class readFile {
private Scanner x;
public void openFile() {
try {
x = new Scanner(new File("patients.txt"));
} catch (Exception e){
System.out.println("Couldn't find file!");
}
}
public void readFile() {
ArrayList<String> data = new ArrayList<String>();
while(x.hasNext()) {
String PatientName = x.next();
String DoctorName = x.next();
String currentApp = x.next();
String NextApp = x.next();
}
}
public void closeFile(){
x.close();
}
}
这是我的树类
public class Tree {
Node root;
public void addNode(int key, String patientName, String DocName, String currentApp, String nextApp){
Node newNode = new Node(key, patientName, DocName, currentApp, nextApp);
if(root == null) {
root = newNode;
} else{
Node currentNode = root;
Node parent;
while(true) {
parent = currentNode;
if(key < currentNode.key) {
currentNode = currentNode.leftChild;
if (currentNode == null){
parent.leftChild = newNode;
return;
}
} else{
currentNode = currentNode.rightChild;
if (currentNode ==null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public void Traversal(Node currentNode) {
if(currentNode != null){
Traversal(currentNode.leftChild);
System.out.println(currentNode);
Traversal(currentNode.rightChild);
}
}
public static void main(String[] args){
Tree binaryTree = new Tree();
readFile read = new readFile();
read.openFile();
read.readFile();
}
}
class PatientData {
String patientName;
String DocName;
String currentApp;
String nextApp;
public PatientData (/*Get parameters*/) {
/*Set parameters to members*/
}
}
class Node{
int key;
PatientData patient;
Node leftChild;
Node rightChild;
Node(int key, PatientData patient){
this.key = key;
this.patient = patient;
}
}
这是文本文件
Baker, William, Chavez, 04/01/05, 04/10/06
Sanchez, Jose, Chavez, 06/15/05,
Anderson, Robert, Wong, 04/02/05, 03/30/06
Watson, David, Chavez, 05/03/05, 04/28/06
Chung, Yu, Gilbert, 07/10/05,
Griffin, Sandy, Gilbert, 06/20/05, 06/20/06
Marcus, Wendy, Wong, 08/02/05, 08/03/06
Williams, Rebbeca, Chavez, 08/10/05, 08/11/06
Kennedy, Fred, Wong, 07/16/05, 07/15/06
Henderson, Paul, Wong, 02/15/05,
Tucker, Matthew, Wong, 04/10/05, 04/11/06
Coombs, Jean, Gilbert, 05/01/05, 04/10/06
Earl, Gary, Gilbert, 06/03/05, 05/10/06
Atkins, Anthony, Chavez, 09/10/05, 09/11/06
Garcia, Jesus, Chavez, 10/10/05,
David, James, Wong, 02/02/05, 02/03/06
Young, Ed, Gilbert, 07/09/05, 07/10/06
Jones, Richard, Gilbert, 08/01/05, 08/10/06
Peterson, Jerry, Wong, 06/02/05, 06/03/06
Arnold, Belinda, Chavez, 01/10/05, 01/11/06
Franklin, Jason, Wong, 09/12/05, 09/13/06
Trent, Joseph, Gilbert, 03/12/05,
Valdez, Tomas, Gilbert, 10/15/05, 10/10/06
Gent, Charles, Wong, 10/22/05, 10/11/06
Roper, Joan, Chavez, 03/10/05, 03/21/06
Lopez, Ricky, Wong, 03/24/05, 03/25/06
Henry, Sarah, Gilbert, 04/18/05, 04/17/06
Nathan, James, Chavez, 06/10/05, 08/11/06
Ulvan, Rachel, Chavez, 09/10/05,
Mears, Sally, Wong, 05/05/05,
Edwards, Sam, Gilbert, 05/21/05, 05/22/06
Rubino, Ian, Gilbert, 07/24/05, 07/21/06
Osborn, Janet, Chavez, 07/10/05, 07/11/06
Barton, Michael, Chavez, 10/10/05, 10/16/06
Quinn, Pat, Gilbert, 08/27/05, 08/29/06
Inglis, Peggy, Wong, 08/30/05, 08/29/06
答案 0 :(得分:1)
您不需要ArrayList
作为中间存储空间;我理解作业的方式你应该直接使用文件中的数据填充你的trere。因此,只需从文件中读取每个患者并将其添加到树中,如下所示:
public Tree readFile(Tree tree){
int key=0;
while(x.hasNext()){
String patientName = x.next();
String doctorName = x.next();
String currentApp = x.next();
String nextApp = x.next();
tree.addNode(key++, patientName, doctorName, currentApp, nextApp);
}
}
我冒昧地引入了一个基本上只是记录号的密钥。
答案 1 :(得分:1)
分配文本表明您可能应该按预约日期对树进行排序(比较日期而不是键。
您也可以保留3棵树,每位医生一棵,例如使用地图(如果允许的话)。
然后操作非常简单: