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

时间:2012-03-12 01:03:57

标签: java add text-files binary-search-tree

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

阅读文件的代码:

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);
    }
}

二叉搜索树节点的代码:

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;
    }
}

二叉搜索树的代码:

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);
            }
        }
    }


}

2 个答案:

答案 0 :(得分:1)

除非文件每行有一个单词,否则您将遇到麻烦。缓冲读卡器为您提供整条生产线。这是一个单词还是一个句子?

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

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

  1. 您的输入不正确,除非每行一个字。如果每一行都是一个句子,那么您必须将其标记为单词。
  2. 您的插入方法不执行任何操作。难怪你的树不起作用。

答案 1 :(得分:0)

替换此代码

String word = input.readLine();

String word = input.read();

这应该有效