在二叉搜索树中搜索词对象
Searching for Word Object in Binary Search Tree
此函数的目的是获取用户输入以查找已添加到二叉搜索树中的单词。然后使用我在 BST
中的搜索算法,如果找到该词,则打印出该词的频率。我的方法是获取用户输入并创建一个新的 Word 对象,然后使用 tree.search
函数 BST
来查找单词,但这不是查找单词。我不确定我是否应该根据用户输入创建一个新的 Word 对象,所以我认为我的错误就在那里。
这是我的主要方法:
public static void search( BST tree ){
Scanner input = new Scanner(System.in);
System.out.print("Search For: ");
Word searchWord = new Word(input.next());
if ( tree.search(searchWord) == null ){
System.out.println("Value was not found.");
}else{
System.out.println(searchWord.getFrequency());
}
}
这是我的话class:
public class Word {
private String word;
private int frequency;
public Word( String w, int f ){
word = w;
frequency = f;
}
public Word( String w ){
word = w;
}
public void increment(){
frequency++;
}
public String getWord(){
return word;
}
public int getFrequency(){
return frequency;
}
public int compareTo(Word w){
return word.compareTo( w.getWord() );
}
@Override
public String toString(){
return word +" "+ frequency;
}
}
这是我的 BST 搜索算法:
public Node search( Word w ){
if ( root == null ){
System.out.println("No items to search.");
return null;
}else{
return search(w,root);
}
}
private Node search( Word w, Node n){
if ( w == n.getData() ){
return n;
}
if ( w.compareTo( n.getData() ) < 0 ){
if( n.getLeft() == null){
System.out.println("Item not found.");
return null;
}else{
return search(w, n.getLeft());
}
}else{
if ( n.getRight() == null ){
System.out.println("Item not found.");
return null;
}else{
return search(w, n.getRight());
}
}
}
您的代码中有两个问题。
这执行指针比较:if ( w == n.getData() )
。你想比较对象内的数据,所以写成 if ( w.equals(n.getData()) )
.
但是现在您仍然需要重写 Word.equals()
,以便只要两个包含的字符串具有相同的内容,它就会 returns true
。像这样:
public boolean equals(Object other) {
if (!(other instanceof Word))
return false;
return word.equals(((Word)other).word);
}
此函数的目的是获取用户输入以查找已添加到二叉搜索树中的单词。然后使用我在 BST
中的搜索算法,如果找到该词,则打印出该词的频率。我的方法是获取用户输入并创建一个新的 Word 对象,然后使用 tree.search
函数 BST
来查找单词,但这不是查找单词。我不确定我是否应该根据用户输入创建一个新的 Word 对象,所以我认为我的错误就在那里。
这是我的主要方法:
public static void search( BST tree ){
Scanner input = new Scanner(System.in);
System.out.print("Search For: ");
Word searchWord = new Word(input.next());
if ( tree.search(searchWord) == null ){
System.out.println("Value was not found.");
}else{
System.out.println(searchWord.getFrequency());
}
}
这是我的话class:
public class Word {
private String word;
private int frequency;
public Word( String w, int f ){
word = w;
frequency = f;
}
public Word( String w ){
word = w;
}
public void increment(){
frequency++;
}
public String getWord(){
return word;
}
public int getFrequency(){
return frequency;
}
public int compareTo(Word w){
return word.compareTo( w.getWord() );
}
@Override
public String toString(){
return word +" "+ frequency;
}
}
这是我的 BST 搜索算法:
public Node search( Word w ){
if ( root == null ){
System.out.println("No items to search.");
return null;
}else{
return search(w,root);
}
}
private Node search( Word w, Node n){
if ( w == n.getData() ){
return n;
}
if ( w.compareTo( n.getData() ) < 0 ){
if( n.getLeft() == null){
System.out.println("Item not found.");
return null;
}else{
return search(w, n.getLeft());
}
}else{
if ( n.getRight() == null ){
System.out.println("Item not found.");
return null;
}else{
return search(w, n.getRight());
}
}
}
您的代码中有两个问题。
这执行指针比较:
if ( w == n.getData() )
。你想比较对象内的数据,所以写成if ( w.equals(n.getData()) )
.但是现在您仍然需要重写
Word.equals()
,以便只要两个包含的字符串具有相同的内容,它就会 returnstrue
。像这样:public boolean equals(Object other) { if (!(other instanceof Word)) return false; return word.equals(((Word)other).word); }