Reading data back into a binary search tree

2019-08-27 09:40发布

问题:

I have a program that adds a node containing (int studentNumber, String firstName, String lastName, String major, double gpa) and to "save" it to a file, I have this method:

public void saveRecord (Node focusNode) {

    JOptionPane.showMessageDialog(null,"saving file...");
    try
    {
        ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file));         

        if (focusNode != null){

            System.out.println(focusNode);
            output.writeObject((focusNode));

            preOrderTraverseTree(focusNode.leftChild);

            preOrderTraverseTree(focusNode.rightChild);
        }//end if
        output.close();

    }
   //catch exceptions
}//end save

Now, I want to pull back each node that was written and be able to sort them differently, (such as by major, gpa, etc). How do I go about doing that while retaining all of the fields so that they are searchable when I read them back in? I have thought about reading them back into a list, but it adds each line in with none of the previously mentioned parameters. My addNode method adds each node to the tree using those parameters so I can't just call that. How would I do this?

回答1:

I've included the answer to this question in my answer to your other question here.