Java 用文本文件填充二叉搜索树

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9660559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:45:55  来源:igfitidea点击:

Populating a binary search tree with a text file

javaaddtext-filesbinary-search-tree

提问by TonsOfDamage

I'm trying to fill in a binary search tree with a text file, but i'm having alot of trouble implementing my insert function. Am i reading the input correctly or is it my code?

我正在尝试用文本文件填充二叉搜索树,但是在实现插入功能时遇到了很多麻烦。我是正确读取输入还是我的代码?

Code for reading file:

读取文件的代码:

import java.io.*;
import java.util.Scanner;

public class Driver {
    public static void main(String[]args) throws IOException
    {
        //Checks if there is a correct number of arguments passed through the command line.
        if (args.length != 1)
        {
            quitError("Tree command word arguments expected");
        } 

        String inputFile = args[0];
        BST btree = new BST();  

        try
        {
            BufferedReader input = new BufferedReader(new FileReader(inputFile));

            //Scans each word from the input and prints it out
            String word = input.readLine();
            while (word != null)
            {
                btree.insert(word);
                word = input.readLine();
            }
            return;

        } catch(FileNotFoundException filenotfoundexception) //Catches file not found exception
        {
            System.out.println("File not found.");
        }
        catch(IOException ioexception) //Catches input/output exception
        {
            System.out.println("File input error occured!");
        }

    }

    //Displays an error message, program exits
    public static void quitError(String msg)
    {
        System.out.println(msg);
        System.out.println("Program will now quit.");
        System.exit(0);
    }
}

Code for the binary search tree node:

二叉搜索树节点的代码:

public class BSTNode {
    protected String data;
    protected BSTNode left, right;

    public BSTNode() 
    {
        left = null;
        right = null;
    }

    public BSTNode(String data)
    {
        this(data,null,null);
    }

    public BSTNode(String data, BSTNode lt, BSTNode rt) 
    {
        this.data = data; 
        left = lt; 
        right = rt;
    }
}

Code for the binary search tree:

二叉搜索树的代码:

public class BST {
    protected BSTNode root = null;
    public BST(){}

    public void clear()
    {
        root = null;
    }

    public boolean isEmpty() 
    {
        return root == null;
    }


    public void insert(String data) 
    {
BSTNode p = root, prev = null;
    while (p != null) {
        prev = p;
        if (p.data.compareTo(data) < 0)
            p = p.right;
        else p = p.left;
    }
    if (root == null)
        root = new BSTNode(data);
    else if (prev.data.compareTo(data) < 0)
        prev.right = new BSTNode(data);
    else prev.left  = new BSTNode(data);
    }   

    public void inorder()
    {
        inorder(root);
    }

    private void inorder(BSTNode p) 
    {
        if (p != null) 
        {
            inorder(p.left);
            System.out.print(p.data + " ");
            inorder(p.right);
        }
    }

    public void breadthFirst() 
    {
        BSTNode p = root;
        Queue queue = new Queue();
        if (p != null) 
        {
            queue.enqueue(p);
            while (!queue.isEmpty()) 
            {
                p = (BSTNode) queue.dequeue();
                System.out.print(p.data + " ");
                if (p.left != null)
                    queue.enqueue(p.left);
                if (p.right != null)
                    queue.enqueue(p.right);
            }
        }
    }


}

回答by duffymo

Unless the file has one word per line, you're going to have trouble. The buffered reader is giving you the entire line. Is it a word or a sentence?

除非文件每行一个字,否则你会遇到麻烦。缓冲阅读器为您提供整行。它是一个词还是一个句子?

Your insert() method is empty. Nothing will happen without that. Put some code into it and you may have better luck.

你的 insert() 方法是空的。没有它,什么都不会发生。把一些代码放进去,你可能会有更好的运气。

So it seems to me you have two problems:

所以在我看来你有两个问题:

  1. Your input is incorrect unless it's one word per line. If each line is a sentence, you'll have to tokenize it into words.
  2. Your insert method does nothing. No wonder your tree isn't working.
  1. 除非每行一个字,否则您的输入是不正确的。如果每一行都是一个句子,则必须将其标记为单词。
  2. 你的插入方法什么都不做。难怪你的树不工作。

回答by arsingh1212

Replace this code

替换此代码

String word = input.readLine();

with

String word = input.read();

This should work

这应该工作