Reading data back into a binary search tree

百般思念 提交于 2019-12-11 19:46:37

问题


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.



来源:https://stackoverflow.com/questions/23049545/reading-data-back-into-a-binary-search-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!