Java 用文本文件填充二叉搜索树
我试图用文本文件填充二叉搜索树,但在实现插入函数时遇到了很多问题。我是正确读取输入,还是我的代码 读取文件的代码:Java 用文本文件填充二叉搜索树,java,add,text-files,binary-search-tree,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
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);
}
}
}
}
公共类BST{
受保护的节点根=null;
公共BST(){}
公共空间清除()
{
root=null;
}
公共布尔值为空()
{
返回root==null;
}
公共void插入(字符串数据)
{
bstnodep=root,prev=null;
while(p!=null){
prev=p;
如果(p.data.compareTo(data)<0)
p=p.右;
否则p=p左;
}
if(root==null)
根=新节点(数据);
else if(上一个数据与(数据)<0)比较)
prev.right=新节点(数据);
else prev.left=新节点(数据);
}
公共无效序()
{
顺序(根);
}
专用无效索引(BSTP节点)
{
如果(p!=null)
{
顺序(p.left);
系统输出打印(p.data+“”);
顺序(p.right);
}
}
公共空间宽度优先()
{
BSTP=根节点;
队列=新队列();
如果(p!=null)
{
排队(p);
而(!queue.isEmpty())
{
p=(BSTNode)queue.dequeue();
系统输出打印(p.data+“”);
如果(p.left!=null)
排队。排队(p.左);
如果(p.right!=null)
排队。排队(右图);
}
}
}
}
除非文件每行有一个单词,否则您将遇到问题。缓冲读取器将为您提供整行内容。它是一个单词还是一个句子
您的insert()方法为空。没有这一切都不会发生。在里面输入一些代码,你可能会有更好的运气
在我看来,你有两个问题:
String word = input.readLine();
与
这应该可以对不起,我不小心删除了插入代码。下面是代码:public void insert(字符串数据){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);}我只能对我看到的内容进行评论。把它放回你的问题中去。这里难以辨认。
String word = input.read();