提问人:millisim 提问时间:10/27/2023 最后编辑:Chaosfiremillisim 更新时间:10/27/2023 访问量:33
我正在开发一个在命令行中编译的程序,该程序获取文本文件并将其放入二叉树中,然后索引或搜索
I'm working on a program compiled in the command line that takes a text file and places it into a binary tree, then either indexes or searches
问:
这基本上就是我迄今为止对二叉树的看法。
据我所知,我很确定我应该在 MyBinarySearchTree 中创建一个二叉树。树本身的长度应根据您输入的文件而有所不同。
索引查找每个单词并将其与弹出的行一起输出。
搜索类似于索引,但它提示用户输入一个单词,然后在树中找到该单词并输出它搜索的每个节点的单词。
Binary Tree 类:
import java.io.*;
import java.lang.*;
public class MyBinarySearchTree{
public MyBinarySearchTree(){
}
// Commands for the program
/* If the user inputs '-h':
* print out instructions on how to use the program.
* Do not read a file.
*
* @return nothing
*/
protected static void help(){
System.out.println("-h: Displays any available commands");
System.out.println("-i: Outputs each word in the filename and the line they appear on.");
System.out.println("-s: Does the same as '-i', but also finds the word in the tree and outputs\r\n"
+ " * the words each node it searches.");
}
/* If the user inputs '-i':
*
* Output each word found in the filename followed by a colon (no spaces).
* Output the list of lines in the file where the word appears, with each
* integer separated by a comma, with one space after the comma.
* Use strip punctuation from the words.
* Apostrophe does not count as punctuation.
* Each word's out put will be on a separate line
* Do NOT put blank lines in-between.
*
* Summary: Output the word and the lines it appears on.
* Do NOT output the word twice.
* Do NOT count the word if it appears on a line twice.
*
* Print out the word in alphabetical order with no duplicates.
* Convert all the words to LOWER CASE first.
*
* @param String filename (name of the file the user wishes to index)
*
* @return nothing
*/
protected static void index(String filename){
}
/* If the user inputs '-s', read the file and index like '-i', but prompt
* the user to enter a word. Instruct the user that entering an empty line
* will cause the user to exit. Find the word in the tree and output
* the words of each node it searches. This is to prove that it is performing
* a proper search and demonstrating that your binary search tree is properly
* structured. If it finds the target word, output the word entry as specified
* in '-i' on a separate line. (Create a method to output a node's data
* into a String and then print it out.) Loop until the user exits.
*
* Summary: Does the same as '-i', but finds a user-inputted word in the tree.
*
*
*
* @param String filename (name of the file the user wishes to search)
*
* @return nothing
*/
protected static void search(String filename){
}
} // End of Class MyBinarySearchTree
This is the main driver I'm using. I'm pretty sure it's complete, but I'll still include it to show how these commands get used.
Driver 类:
public class Driver{
// Main Method
public static void main(String[] args) {
MySearchBinaryTree newBinaryTree = new MySearchBinaryTree();
if(1 > args.length) {
System.out.println("You need to pass an argument");
System.out.println("Use -h for a list of commands.");
}
else {
if(args[0].charAt(0) == '-'){
switch(args[0].charAt(1)) {
// Java Driver -h
case 'h':
System.out.println("You asked for help!");
help();
// Java Driver -i theFile
case 'i':
System.out.println("Preparing to read...");
index(args[1]);
// Java Driver -s theFile
case 's':
System.out.println("Preparing to search...");
search(args[1]);
// Java Driver -[Anything Else] theFile
default:
System.out.println("Not a command: " + args[0]);
}
}
else{
System.out.println("You need to pass an argument");
System.out.println("Use -h for a list of commands.");
}
}
} // End of Main
} // End of Driver
我正在尽最大努力弄清楚我应该做什么,但我就是无法弄清楚如何在 Java 中制作二叉树,更不用说根据您输入的文本文件而变化的二叉树了。
答: 暂无答案
上一个:检测二叉搜索树重复项并解决冲突
评论