在二进制搜索树中查找单词Java?
我遇到的问题是,我有一个二进制搜索3,所有节点都包含字符串值,而不是整数值。它从txt文件中获取这些字符串,并将文件中的文本行作为节点放入树中。这没有问题 我的问题是,我需要一个方法来遍历我的树并找到一个特定的单词。我已经有了单独的类,BinarySearchTree和BinaryStreeNode作为我试图创建的树的基础。它需要查找的单词位于一个名为“lookup test file copy.txt”的文件中,它需要将找到的单词写入另一个名为“SearchResults.txt”的文件中 我只是不知道怎么做,所以我在寻求建议 这是我需要帮助的方法:在二进制搜索树中查找单词Java?,java,string,search,tree,binary-search-tree,Java,String,Search,Tree,Binary Search Tree,我遇到的问题是,我有一个二进制搜索3,所有节点都包含字符串值,而不是整数值。它从txt文件中获取这些字符串,并将文件中的文本行作为节点放入树中。这没有问题 我的问题是,我需要一个方法来遍历我的树并找到一个特定的单词。我已经有了单独的类,BinarySearchTree和BinaryStreeNode作为我试图创建的树的基础。它需要查找的单词位于一个名为“lookup test file copy.txt”的文件中,它需要将找到的单词写入另一个名为“SearchResults.txt”的文件中 我
public boolean SearchWord(String target){
//returns true if the target string exists in the dictionary
// otherwise it returns false
//ALSO you need to write the results of Search
//in an output file called "SearchResults.txt"
return false;
}
这是我所有的代码,不包括上面提到的另外两个类,如果有帮助的话
public class Dictionary {
public BinaryTreeNode theBinaryTree;
/**
* I need to read one string by one string
* and then insert it into a tree.
* I need to read all of the strings in the source file, line by line,
* and insert them into my dictionary.
* After readinga single string, it needs to put it into a tree.
* I first need to create the dictionary tree,
* and then implement these methods on the tree.
* The Dictionary type is string.
* @throws FileNotFoundException
*/
private BinaryTreeNode dictionaryTree; // this is the tree within your dictionary class
public Dictionary(String filePath) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("Dictionary.txt"));
this.dictionaryTree = readFile(br);
br.close();
}
public BinaryTreeNode readFile(BufferedReader reader) throws IOException{
String word = reader.readLine();
if(word!=null){
return new BinaryTreeNode(word, readFile(reader), readFile(reader));
}else{
return new BinaryTreeNode() ; // empty node or null?
}
}
/**
* @return
* Once again, I already have this method written and modified
* within the BinarySearchTree class.
* I'm simply going to call it over here.
*/
public int CountWords(){
//returns the number of words in the dictionary
//This is just counting nodes.
BinarySearchTree Aria = new BinarySearchTree();
return Aria.countNodes(dictionaryTree);
}
public boolean SearchWord(String target){
//returns true if the target string exists in the dictionary
// otherwise it returns false
//ALSO you need to write the results of Search
//in an output file called "SearchResults.txt"
return false;
}
/**
* I already modified the print order method
* in BinarySearchTree
* to work with strings.
* So I just called it here on the dictionaryTree.
* @PrintOrderedDict()
*
* However, I also had to modify the method,
* so that it wrote whatever values the method recieved
* to teh output file.
*/
public void PrintOrderedDict() throws IOException{
//Print the dictionary words
//in order in a new file called "OrderedDictionary.txt".
//Just modify print order to work with strings.
try {
BinarySearchTree jojo = new BinarySearchTree();
FileWriter fstream = new FileWriter("OrderedDictionary.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(jojo.inorderPrint(dictionaryTree));
out.close();}
catch (Exception e) {
System.err.println("Error");
}
}
public boolean DeleteWord(String target){
//delete the target word if it exits in the dictionary and return true
//otherwise return false
return false;
}
}
任何帮助或建议都将不胜感激
----编辑----
这也是“dictionary.txt”文件的一个小示例(太长了,无法放入整个文件)
这是“查找测试文件copy.txt”文件:
您没有包含最相关的代码,即BinaryTreeNode,因此此处使用的字段名是猜测,但这样做可以: 字典中的方法:
public boolean SearchWord(String target){
boolean found = theBinaryTree.contains(word);
// write the values of "target" and "found" to file (code omitted)
return found;
}
二进制树节点中的方法:
private String word;
private BinaryTreeNode left;
private BinaryTreeNode right;
public boolean contains(String target) {
if (target.equals(word))
return true;
BinaryTreeNode next = target.compareTo(word) < 0 ? left : right;
return next != null && next.contains(target);
}
私有字符串字;
私有二叉树左;
私有二元树节点权;
公共布尔包含(字符串目标){
if(target.equals(word))
返回true;
BinaryTreeNode next=target.compareTo(word)<0?左:右;
return next!=null&&next.contains(目标);
}
这显然是一个家庭作业,因此我不会从您那里窃取解决它的可能性,但我会给您一些提示,您可以使用这些提示轻松解决您的问题:
compareTo
方法<代码>s1。与(s2)相比:
- 如果s1>s2,则为正值
- 如果s1
- 如果s1等于(s2),则为0
把问题分解成几个部分 1) 搜索如何读取文件。读取文件,并将结果回显到标准输出。这很简单,大约是你需要做的工作的三分之一 2) 将一些随机单词写入文件。打开文件,写下单词,然后检查你的工作,也不难做到,你正在接近 3) 加载二进制搜索树并编写代码进行查找,这非常简单。如果单词.等于当前节点,则返回true,否则如果小于当前节点,则向左,如果大于,则向右。如果指针为null,则返回false 4) 把它们放在一起
部分克服这一点就没有那么难了。如果我没有弄错的话,这是一个简单的例子,“如果你要搜索的单词的字母顺序低于光标所在的单词,那么就向左移动,否则就向右移动。”递归地执行此操作,直到找到单词并返回true,或者根本找不到单词并返回false。尝试使用铅笔和纸,看看它是如何工作的。+1但我希望OP能够理解三元运算符的用法:)这非常有帮助,但我不断收到使用“word”的错误消息在字典法中。@MariaStewart你读过波希米亚人说“这里使用的字段名是猜测”的那部分吗?
public boolean SearchWord(String target){
boolean found = theBinaryTree.contains(word);
// write the values of "target" and "found" to file (code omitted)
return found;
}
private String word;
private BinaryTreeNode left;
private BinaryTreeNode right;
public boolean contains(String target) {
if (target.equals(word))
return true;
BinaryTreeNode next = target.compareTo(word) < 0 ? left : right;
return next != null && next.contains(target);
}