简体   繁体   中英

Storing data into an array list and then binary search tree

I have a homework assignment where I need to load patient data into a node, then be able to search through the tree. The node will store patient name, doctors name, their current appointment, and there next annual appointment date. The data is read in from a text file. I want to use an arrayList to store the data into the nodes, but the confusing part is how can I store just the certain data of the arrayList into each node? (I hope that makes sense). I posted a day or so ago and had some help to create a patient class and then store that inside the node and then array list. But I am still lost in exactly how I could implement that. Keep in mind I am horrible at BST's Oo And I chose to use an arrayList over LinkedList since I am more comfortable using those. This is my class for reading in my text file...(Array implementation isn't complete).

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();
    }

}

And here is my tree class

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;
    }

}

This is the text file

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

在此处输入图片说明

You don't need an ArrayList as intermediate storage; the way I understand the assignment you should directly populate your trere with the data from the file. Therefore, just read each patient from the file and add it to the tree, like so:

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);
    }
}

I took the liberty of introducing a key which is basically just the record number.

The assignment text indicates that you probably should sort the tree by appointment date (compare the dates rather than the keys.

You might also keep 3 trees, one for each doctor, eg by using a map (if you are allowed).

Then the operations are implemented quite easy:

  • "search for patients with appointments within next week by doctor's name" - get the tree for the doctor from the map then extract the subtree (nodes) whose appointment date is between now (today) and now + 7 days
  • "search for patients that need an annual appointment by doctor's name" - same as above but get all whose appointment date is lower than now - 1 year.
  • "search for any patient ..." the same as above just search in all 3 trees and combine the results in a list
  • "edit a patient's appointment" - this would mean resorting the tree, the easiest way to achieve that is probably to remove and reinsert the node (you can thus reuse your sorting code)
  • "delete a patient" - you'd either need to maintain another tree which is sorted by name/id and use the appointment date to look up the corresponding nodes in the other trees (note: you might get more if the dates are the same) or just iterate over one (all) tree and remove any matching node

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM