gpt4 book ai didi

java - 在 BST 的节点中存储数据

转载 作者:行者123 更新时间:2023-11-30 07:30:48 25 4
gpt4 key购买 nike

我有一个家庭作业,需要将患者数据加载到节点中,然后能够在树中搜索。该节点将存储患者姓名、医生姓名、当前预约以及下一个年度预约日期。数据是从文本文件中读入的。我想使用 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

最佳答案

创建一个名为 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<>();

关于java - 在 BST 的节点中存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com