这是我的第一篇文章,如果我还没有完全正确地解决问题那么道歉。我会立刻举起双手,说这是我的“家庭作业”的一部分,我已经完成了大约99%的工作,但是我已经得到了这个让我整整疯狂的琐事,我只是能够看不到这里的树木。
基本概念是我们有一个文本文件,一种各种各样的诗,我们的程序必须查看然后放入二叉树。我们应该记录每个单词,每个单词的出现以及它出现在哪一行。我已设法做到这一点,除了示例"And I went to StackOverFlow and I asked the question, and the answer was..."
我的程序应该返回单词"And"
出现在第1行(它当前正在执行)中,但它当前正在记录该单词的每次出现,它应该只记录单词“和”出现在第1行而不是每个实例。< / p>
所以在上面的示例"And I went to StackOverFlow and I asked the question, and the answer was.."
所以只要在这里找出"And"
这个词,因为它多次出现,我的程序目前正在返回:
"And [1, 1, 1]"
但应返回:
"And [1]"
我已经提供了整个程序的代码,但我认为要关注的方法是recordWord
方法,在最后else if
,有些东西在那里显然不对!抱歉,如果这个解释非常复杂,但我想不出另一种方式来解释它!任何帮助都将非常感激
import java.util.*;
/**
* A class representing a binary tree containing words.
*
*/
public class WordTree {
public String word;
public ArrayList<Integer> lineNumbers;
public WordTree left, right;
/**
* Constructs a tree consisting of a single node, with the given word and
* line number.
*
* @param w
* the word
* @param lineNo
* the line number
* @pre true
* @post word tree containing word w on line lineNo has been constructed
*/
public WordTree(String w, int lineNo) {
word = w;
lineNumbers = new ArrayList<Integer>();
lineNumbers.add(lineNo);
left = null;
right = null;
}
/**
* Records a new occurrence of the given word, in the tree.
*
* @param w
* the word
* @param lineNo
* the line number
* @pre this is a well formed binary search tree
* @post if word was not in this tree, then the word and its line number
* line have been inserted into ordered word tree, else line has been
* appended to line-number list for word (if we haven't already
* recorded that line number for this word)
*/
public void recordWord(String word2, int lineNo) {
if (word.compareToIgnoreCase(word2) < 0) {
if (right != null) {
right.recordWord(word2, lineNo);
} else {
right = new WordTree(word2, lineNo);
}
} else if (word.compareToIgnoreCase(word2) > 0) {
if (left != null) {
left.recordWord(word2, lineNo);
} else {
left = new WordTree(word2, lineNo);
}
} else if (word.compareToIgnoreCase(word2) == 0) {
lineNumbers.add(lineNo);
}
}
// System.out.println("method recordWord not implemented yet");
/**
* Displays all the words in this tree.
*
* @pre this is a well formed binary search tree
* @post words have been written out in alphabetical order, each followed by
* ascending list of line numbers on which the word occurs
*/
public void display() {
if (left != null) {
left.display();
}
System.out.println(word + lineNumbers);
if (right != null) {
right.display();
}
}
/**
* Counts how many different words there are in this tree.
*
* @pre this is a well formed binary search tree
* @return the number of different words in tree
*/
public int numberOfEntries() {
int count = 1;
if (left != null) {
count += left.numberOfEntries();
}
if (right != null) {
count += right.numberOfEntries();
}
}
}
答案 0 :(得分:0)
由于List接受重复,因此它可以接受具有相同值的元素。相反,您可以使用Set界面自动消除重复。在您的示例中,您可以将 lineNumbers 声明为 TreeSet ,以便以升序行号打印输出。
将 lineNumbers 声明为Set
public Set<Integer> lineNumbers;
然后使用TreeSet在构造函数中初始化它
lineNumbers = new TreeSet<Integer>();
这就是你需要改变的一切。希望它足够清楚。