0

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 O.o. 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

enter image description here

5
  • Post the assignment question. Commented Mar 24, 2016 at 15:42
  • @AnkitShubham, Sorry while you were commenting that's what I was doing haha. Commented Mar 24, 2016 at 15:43
  • You need to create a Patient implements Comparable class in order to create a proper BST. Commented Mar 24, 2016 at 15:45
  • @cricket_007 he could also use a Comparator<Patient> like TreeSet etc. do. Commented Mar 24, 2016 at 15:49
  • Okay, I'l see if I can get it going with this. Commented Mar 24, 2016 at 15:54

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, i'll try to implement it this way.
Note that the assignment suggests using multiple trees. With your current Node class this won't work well; I think you should not use a single class for both the patient and the tree node, but rather separate them into a Patient class which holds only the patient data, and a Node class which has references to the Patient and the two child Nodes. This setup would allow multiple trees which all reference the same Patient instances, but with a different tree order each. Therefore, you should also add a way to perform node comparison to your Tree class, as to order them meaningfully.
Yeah, I know that is a bad way to code. I should've split my classes from the get go. I'll work on that also! Thanks buddy.
1

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, e.g. 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

2 Comments

Yeah, I was thinking about doing a tree per doctor. I appreciate the insight!
@wadeaston you're welcome. Btw, don't forget the rebalancing code since the trees might degenerate into lists upon heavy usage. Maybe you should rebalance them right at the start, that is after building the trees.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.