我有一项家庭作业,我需要将患者数据加载到节点中,然后才能搜索树。该节点将存储患者姓名,医生姓名,当前预约以及下一年度预约日期。从文本文件中读取数据。我想使用arrayList将数据存储到节点中,但令人困惑的部分是如何将arrayList的某些数据存储到每个节点? (我希望这是有道理的) 这是我在我的文本文件中读取的类...(数组实现不完整)
import java.io.*;
import java.util.*;
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 Node{
int key;
String patientName;
String DocName;
String currentApp;
String nextApp;
Node leftChild;
Node rightChild;
Node(int key, String patientName, String DocName, String currentApp, String nextApp){
this.key = key;
this.patientName = patientName;
this.DocName = DocName;
this.currentApp = currentApp;
this.nextApp = nextApp;
}
}
这是文本文件
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)
创建一个名为Appointment
的类。将它们存储在您的节点和ArrayList
。
class Appointment {
String patientName;
String DocName;
String currentApp;
String nextApp;
public Appointment (/*Get parameters*/) {
/*Set parameters to members*/
}
}
这是您的新Node
class Node{
int key;
Appointment app;
Node leftChild;
Node rightChild;
Node(int key, Appointment app){
this.key = key;
this.app = app;
}
}
您可以像ArrayList
ArrayList<Appointment> appointments = new ArrayList<>();