简体   繁体   中英

How do I insert into the next available node on a binary search tree?

I have a program I need to build where I continue adding to a Binary Search Tree (BST) by opening a file and adding the words from the file to the BST. I have figured out how to open a file and store the words from the original file in the tree, but when I try to open a second file to continue adding to the tree, it just acts as if I am starting from scratch. How do I point to the next available node so that I can continue to insert into it.

I have tried using my insert function, but it just acts as though I'm starting from scratch and deletes everything from the previous file.

My node class:

class BSTNode {
    String word;
    int data;
    BSTNode parent;
    BSTNode left;
    BSTNode right;


    public BSTNode(String word, int data) {
        this.word = word;
        this.data = data;
        this.left = null;
        this.right = null;
        this.parent = null;
    }


    public BSTNode() {
    }
}

My insert function:

  void insert(BSTNode node, String word, int data) {
        if (search(node, word)) {
        } else {
            insertNode(node, word, data);
        }
    }

And the button I'm selecting to add another file to the BST:

} else if (evt.getSource().equals(anotherFile)) {
                JFileChooser pickFile = new JFileChooser();
                int dialog = pickFile.showOpenDialog(GUI.this);
                if (dialog == JFileChooser.APPROVE_OPTION) {
                    GUI.this.file.setText(pickFile.getSelectedFile().getName());
                    directory.setText(pickFile.getCurrentDirectory().toString());
                }
                if (dialog == JFileChooser.CANCEL_OPTION) {
                    GUI.this.file.setText("You pressed cancel");
                    directory.setText("");
                }
                try {
                    Scanner scanner = new Scanner(file);
                    BSTFunctions bstf = new BSTFunctions();
                    while (scanner.hasNext()) {
                        bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
                    }

                    bstf.wordCount(bstf.ROOT);
                    bstf.listInOrder(bstf.ROOT);

                    scanner.close();
                } catch (IOException e1) {

                    results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
                }

Make BSTFunctions bstf = new BSTFunctions(); a field of your GUI class instead of declaring it inside actionPerformed . If you declare it inside the method, you'll be starting a new one from scratch everytime that method runs.

class GUI extends JFrame {
    private final BSTFunctions bstf = new BSTFunctions();
    // everything else
}

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