Java帮助递归和二叉树
我对Java有些陌生,对递归和二叉树也很陌生。我正在构建一个程序,从文档中提取文本并将其存储在二叉树中。然后我需要取一个字符串,找出它在文本中出现了多少次 我的问题要么是在添加数据时,要么是在搜索数据以查找字符串时 我已经决定在构建时在每个节点中存储字符串和频率。因此,我的添加方法如下所示:Java帮助递归和二叉树,java,recursion,binary-tree,Java,Recursion,Binary Tree,我对Java有些陌生,对递归和二叉树也很陌生。我正在构建一个程序,从文档中提取文本并将其存储在二叉树中。然后我需要取一个字符串,找出它在文本中出现了多少次 我的问题要么是在添加数据时,要么是在搜索数据以查找字符串时 我已经决定在构建时在每个节点中存储字符串和频率。因此,我的添加方法如下所示: public void add(String newWord) { //Change the word case to make comparing easier newWord = ne
public void add(String newWord) {
//Change the word case to make comparing easier
newWord = newWord.toUpperCase();
root = recursiveAdd(root, newWord);
}
/**
* Takes the root and recurses until the root is null (base case)
* Frequency is incremented if the data is being added, or if
* it already exits. If the data is not present, the method recurses
*/
private Node recursiveAdd(Node subTree, String newWord) {
//Base case: the root is null
//Empty trees have a node created, set, and incr freq
if (subTree == null) {
subTree = new Node();
subTree.setStoredWord(newWord);
subTree.incrFreqCount();
return subTree;
}
int comparison = newWord.compareTo(subTree.getStoredWord());
//For a word already in tree, increment the frequency
if (comparison == 0) {
if(newWord.equalsIgnoreCase("translyvania"))
System.out.println("Entered same word incrementation");
subTree.incrFreqCount();
return subTree;
//The root comes before the new word, then
//move on to the right child
} else if(comparison < 0) {
subTree.setLchild(recursiveAdd(subTree.getLchild(), newWord));
} else { //if(comparison > 0) {
subTree.setRchild(recursiveAdd(subTree.getRchild(), newWord));
}
return subTree;
}
我似乎不知道我的问题在哪里。对于我正在搜索的单词,有时它说它出现了我应该得到的16次,有时它说1次。虽然我知道肯定有一个原因,但它看起来根本不一致,而且它的值似乎没有任何原因发生变化
一旦我的树被构建,我就会获取我正在搜索的字符串,并通过这些方法传递它
public void wordSearch(String lookForWord){
lookForWord = lookForWord.toUpperCase();
wordSearchRecur(root, lookForWord);
}
private boolean wordSearchRecur(Node subTree, String lookForWord){
//Base case
// The root is that same as the string
if(subTree == null){
System.out.println("The word \"" + lookForWord + "\" is not "
+ "found in the text");
return false;
}
int comparison = lookForWord.compareTo(subTree.getStoredWord());
if(comparison == 0){
System.out.println("The word \"" + lookForWord + "\" is found " +
subTree.getFreqCount() + " times in the text");
return true;
//Alphabetically before, then move to left branch
} else if (comparison < 0){
System.out.println("move to left");
return wordSearchRecur(subTree.getLchild(), lookForWord);
//Alphabetically after, then move to right branch
} else { // if(comparison > 0){
System.out.println("move to right");
return wordSearchRecur(subTree.getRchild(), lookForWord);
}
}
我也不能真正理解为什么我要结束wordSearchRecur方法。我不应该在它到达那个点之前回来吗?我的输出显示它多次到达那里
我知道我遗漏了这些概念的大部分内容,但查看之前的所有帖子并没有帮助。我一定花了3个小时在Stack上寻找答案,更不用说其他网站了
请帮忙
编辑:
在@Joop Eggen的帮助下,我编辑了代码以包含我所更改的内容。现在,在recursiveAdd期间,我已正确计算了频率,但在WordSearchRecurr期间,频率似乎没有跟随节点。即使比较==0,freqCount仍然是1
解决:在@Joop-Eggen的帮助下,进一步的问题仅仅是疏忽的结果。感谢您的帮助。在以下任何一种情况下,您都不会返回:
//Alphabetically before, then move to left branch
if(lookForWord.compareTo(root.getStoredWord()) < 0){
System.out.println("move to left");
wordSearchRecur(root.getLchild(), lookForWord);
//Alphabetically after, then move to right branch
} else if(lookForWord.compareTo(root.getStoredWord()) > 0){
System.out.println("move to right");
wordSearchRecur(root.getRchild(), lookForWord);
}
您需要返回wordSearchRecur,而不仅仅是调用它并丢弃结果。首先将代码简化为最简单的形式,您将从中获益
public void add(String word) {
//Change the word case to make comparing easier
word = word.toUpperCase();
root = recursiveAdd(root, word);
}
/**
* Takes a sub-tree and recurses until the sub-tree is null (base case)
* Frequency is incremented if the data is being added, or if
* it already exists. If the data is not present, the method recurses
*/
private Node recursiveAdd(Node subtree, String word) {
// Base case: the subtree is null
if (subtree == null) {
Node node = new Node();
node.setStoredWord(word);
node.incrFreqCount();
return node;
}
int comparison = word.compareTo(subtree.getStoredWord());
if (comparison == 0) {
// For data already in tree, increment the frequency
subtree.incrFreqCount();
} else if (comparison < 0) {
subtree.setLchild(recursiveAdd(subtree.getLchild(), word);
} else /*if (comparison > 0)*/ {
subtree.setRchild(recursiveAdd(subtree.getRchild(), word);
}
return subtree;
}
作为搜索:
public void wordSearch(String lookedForWord){
lookedForWord = lookedForWord.toUpperCase();
wordSearchRecur(root, lookedForWord);
}
private boolean wordSearchRecur(Node subtree, String word){
if (subtree == null) {
System.out.println("The word \"" + word + "\" is not "
+ "found in the text");
return false;
}
int comparison = word.compareTo(root.getStoredWord();
if (comparison == 0){
System.out.println("The word \"" + word + "\" is found " +
subtree.getFreqCount() + " times in the text");
return true;
} else if (comparison < 0) {
return wordSearchRecur(subtree.getLchild(), word);
} else /*if (comparison > 0)*/ {
wordSearchRecur(subtree.getRchild(), word);
}
}
这有助于防止错误,因为检查/出错的次数更少
正如您在这两种情况下所做的那样,在重写过程中也会进行相同的比较,您使用的是equals,并将compareTo转换为参数,一切都应该正常
事实上,显示的代码看起来很好。制作递归dumpTreeNode子树,字符串缩进。检查每一步
可能您在这里对代码进行了一些编辑,最初一些大括号{}放错了位置。您将到达递归方法的末尾,因为您应该返回递归调用的结果。返回单词searchRecurroot.getLchild,lookForWord;并返回单词searchRecurroot.getRchild,lookForWord;。我发布了与评论相同的内容,而不是作为答案,因为它不能解释为什么我正在搜索的单词,有时它说它发生了我应该得到的16次,有时它说1次。谢谢你的帮助。这解决了最后一次回击的问题。这一部分更有意义。我仍然被另一个问题困住了。@learn.beh修复退货可能已经修复了它,但是如果您不知道如何更改以使其中断/工作,那么我无法真正帮助您。你需要确定哪些是有效的,哪些是无效的,然后找出它们之间的区别。这样做会容易得多!它清除了很多,我现在有了wordSearchRecur,它的频率与recursiveAdd相同。然而,从调试中我可以看到,在recursiveAdd过程中,它只需要额外输入比较==0块1的时间。我现在的频率是2而不是16。哎呀。。。我收回这句话。它的频率仍然是1。