将数据读回到二叉搜索树中 [英] Reading data back into a binary search tree

查看:61
本文介绍了将数据读回到二叉搜索树中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它添加一个包含(int studentNumber, String firstName, String lastName, String major, double gpa)的节点并将其保存"到文件中,我有以下方法:

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

现在,我想撤回所写的每个节点,并能够对它们进行不同的排序(例如,按专业,gpa等).如何在保留所有字段的同时执行此操作,以便在我重新阅读它们时可以搜索它们?我曾考虑过将它们读回到一个列表中,但是它在每行中都添加了前面没有提到的参数.我的addNode方法使用这些参数将每个节点添加到树中,因此我不能只是调用它.我该怎么做?

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?

推荐答案

我在对其他问题的回答中已经包含了该问题的答案

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

这篇关于将数据读回到二叉搜索树中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆