在字典中搜索单词时,二元搜索更相关吗?

时间:2014-01-31 15:53:25

标签: java algorithm search dictionary binary-search

我编写了一种在字典中搜索单词的算法。但它只搜索指定长度的单词的1或2个字母。

Ex search: -

  

A **

结果应该是: -

  

援助,瞄准,

我使用线性搜索算法来查找符合我标准的单词。但我想知道是否可以使用二进制搜索而不是线性搜索?若有,那么任何人都可以给我一些暗示

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Cse221LabAssignment1 {

    /**
     * @param args the command line arguments
     */
    public static final String wordListFileName = "E:\\BRAC UNI\\cse lab\\cse221 lab\\cse221LabAssignment1\\src\\cse221labassignment1\\WordList.txt";
    public static final String searchListFileName = "E:\\BRAC UNI\\cse lab\\cse221 lab\\cse221LabAssignment1\\src\\cse221labassignment1\\SearchList.txt";

    public static void main(String[] args) {
        // TODO code application logic here
        String wordList, searchList;
        wordList = ReadFile(wordListFileName);          //Reading wordlist
        searchList = ReadFile(searchListFileName);      //Reading searchList
        String[] w = wordList.split("\\,");              //Spliting wordlist and putting it into an array
        String[] s = searchList.split("\\,");            //Spliting searchList and putting it into an array
        for (int c = 0; c < s.length; c++) {                    //iterating through the searchList array
            // String [] refinedList=new String[w.length]; //array containing the list of words that matches with the lenght of search word.
            //  int refinedCounter=0;                       //counter for the refinedList array
            for (int i = 0; i < w.length; i++) {                //iterating through the wordList array
                if (s[c].length() == w[i].length()) {
                   // refinedList[refinedCounter]=w[i];
                    // refinedCounter++;
                    if (LetterMatch(w[i], s[c])) {
                        System.out.print(w[i]);
                        if (i < w.length - 1) {
                            System.out.print(",");
                        } else {
                            System.out.println(";");
                        }
                    }
                }

            }

        }
    }

    public static String ReadFile(String fileName) {

        Scanner k = null;
        try {
            k = new Scanner(new File(fileName));
        } catch (FileNotFoundException ex) {
            System.out.println(ex);

        }
        String rt = k.nextLine();
        while (k.hasNextLine()) {
            rt = rt + "," + k.nextLine(); //Words seperated by Comma
        }
        return rt;

    }

    public static boolean LetterMatch(String m, String s) {
        char[] letters = m.toCharArray();
        char[] searchLetters = s.toCharArray();
        boolean match = false;
        int c = 0;
        for (; c < s.length(); c++) {
            if (searchLetters[c] != '*') {
                if (searchLetters[c] == letters[c]) {
                    match = true;
                } else {
                    match = false;
                }
            }
        }
        if (c != s.length()) {
            return false;
        } else {
            return match;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我建议使用替代数据结构来帮助您完成一些繁重的工作。尝试基数树http://en.wikipedia.org/wiki/Radix_tree。这将允许您在遍历树时完成单词,而不必进行线性列表搜索。