java实现英文文件单词搜索系统_java Trie实现英文单词查找树 搜索自动提示

该博客展示了用Java实现英文文件单词搜索系统的代码。定义了WordTrie类构建单词Trie树,包含添加单词、前缀搜索、单词搜索等方法,还给出了测试类,通过添加多个英文单词进行前缀搜索和单词搜索测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.xq.algorithm;

import java.util.ArrayList;

import java.util.List;

/**

*

*

Title:

*

Description: 单词Trie树

*

* @createDate:2013-10-17

* @author xq

* @version 1.0

*/

public class WordTrie {

/**trie树根*/

private TrieNode root = new TrieNode();

/**英文字符串正则匹配*/

static String englishPattern="^[A-Za-z]+$";

/**中文正则匹配*/

static String chinesePattern="[\u4e00-\u9fa5]";

static int ARRAY_LENGTH=26;

static String zeroString="";

/**

*

* @Title: addWord

* @Description: add word

* @param @param word

* @return void

* @throws

*/

public void addWord(String word) {

if(word==null || "".equals(word.trim())){

throw new IllegalArgumentException("word can not be null!");

}

if(!word.matches(englishPattern)){

throw new IllegalArgumentException("word must be english!");

}

addWord(root, word);

}

/**

*

* @Title: addWord

* @Description:add word to node

* @param @param node

* @param @param word

* @return void

* @throws

*/

private void addWord(TrieNode node, String word) {

if (word.length() == 0) { // if all characters of the word has been

// added

node.count++;

node.nodeState=1;

} else {

node.prefixCount++;

char c = word.charAt(0);

c = Character.toLowerCase(c);

int index = c - 'a';

if(index>=0 && index

if (node.next[index] == null) {

node.next[index] = new TrieNode();

}

// go the the next character

addWord(node.next[index], word.substring(1));

}

}

}

/**

*

* @Title: prefixSearchWord

* @Description: 前缀搜索

* @param @param word

* @param @return

* @return List

* @throws

*/

public List prefixSearchWord(String word){

if(word==null || "".equals(word.trim())){

return new ArrayList();

}

if(!word.matches(englishPattern)){

return new ArrayList();

}

char c = word.charAt(0);

c = Character.toLowerCase(c);

int index = c - 'a';

if(root.next!=null && root.next[index]!=null){

return depthSearch(root.next[index],new ArrayList(),word.substring(1),""+c,word);

}else{

return new ArrayList();

}

}

/**

*

* @Title: searchWord

* @Description: 搜索单词,以a-z为根,分别向下递归搜索

* @param @param word

* @param @return

* @return List

* @throws

*/

public List searchWord(String word){

if(word==null || "".equals(word.trim())){

return new ArrayList();

}

if(!word.matches(englishPattern)){

return new ArrayList();

}

char c = word.charAt(0);

c = Character.toLowerCase(c);

int index = c - 'a';

List list=new ArrayList();

if(root.next==null){

return list;

}

for(int i=0;i

int j='a'+i;

char temp=(char)j;

if(root.next[i]!=null){

if(index==i){

fullSearch(root.next[i],list,word.substring(1),""+temp,word);

}else{

fullSearch(root.next[i],list,word,""+temp,word);

}

}

}

return list;

}

/**

*

* @Title: fullSearch

* @Description: 匹配到对应的字母,则以该字母为字根,继续匹配完所有的单词。

* @param @param node

* @param @param list 保存搜索到的字符串

* @param @param word 搜索的单词.匹配到第一个则减去一个第一个,连续匹配,直到word为空串.若没有连续匹配,则恢复到原串。

* @param @param matchedWord 匹配到的单词

* @param @return

* @return List

* @throws

*/

private List fullSearch(TrieNode node,List list,String word,String matchedWord,String inputWord){

if(node.nodeState==1 && word.length()==0){

list.add(matchedWord);

}

if(word.length() != 0){

char c = word.charAt(0);

c = Character.toLowerCase(c);

int index = c - 'a';

for(int i=0;i

if(node.next[i]!=null){

int j='a'+i;

char temp=(char)j;

if(index==i){

//连续匹配

fullSearch(node.next[i], list, word.substring(1), matchedWord+temp,inputWord);

}else{

//未连续匹配,则重新匹配

fullSearch(node.next[i], list, inputWord, matchedWord+temp,inputWord);

}

}

}

}else{

if(node.prefixCount>0){

for(int i=0;i

if(node.next[i]!=null){

int j='a'+i;

char temp=(char)j;

fullSearch(node.next[i], list, zeroString, matchedWord+temp,inputWord);

}

}

}

}

return list;

}

/**

*

* @Title: depthSearch

* @Description: 深度遍历子树

* @param @param node

* @param @param list 保存搜索到的字符串

* @param @param word 搜索的单词.匹配到第一个则减去一个第一个,连续匹配,直到word为空串.若没有连续匹配,则恢复到原串。

* @param @param matchedWord 匹配到的单词

* @param @return

* @return List

* @throws

*/

private List depthSearch(TrieNode node,List list,String word,String matchedWord,String inputWord){

if(node.nodeState==1 && word.length()==0){

list.add(matchedWord);

}

if(word.length() != 0){

char c = word.charAt(0);

c = Character.toLowerCase(c);

int index = c - 'a';

//继续完全匹配,直到word为空串,否则未找到

if(node.next[index]!=null){

depthSearch(node.next[index], list, word.substring(1), matchedWord+c,inputWord);

}

}else{

if(node.prefixCount>0){//若匹配单词结束,但是trie中的单词并没有完全找到,需继续找到trie中的单词结束.

//node.prefixCount>0表示trie中的单词还未结束

for(int i=0;i

if(node.next[i]!=null){

int j='a'+i;

char temp=(char)j;

depthSearch(node.next[i], list, zeroString, matchedWord+temp,inputWord);

}

}

}

}

return list;

}

class TrieNode {

/**

* trie tree word count

*/

int count=0;

/**

* trie tree prefix count

*/

int prefixCount=0;

/**

* 指向各个子树的指针,存储26个字母[a-z]

*/

TrieNode[] next=new TrieNode[26];

/**

* 当前TrieNode状态 ,默认 0 , 1表示从根节点到当前节点的路径表示一个词

*/

int nodeState = 0;

TrieNode(){

count=0;

prefixCount=0;

next=new TrieNode[26];

nodeState = 0;

}

}

}

测试类:

package com.xq.algorithm;

import java.util.List;

public class WordTrieMain {

public static void main(String[] args){

test();

}

public static void test1(){

WordTrie trie=new WordTrie();

trie.addWord("ibiyzbi");

System.out.println("----------------------------------------");

List words=trie.searchWord("bi");

for(String s: words){

System.out.println(s);

}

}

public static void test(){

WordTrie trie=new WordTrie();

trie.addWord("abi");

trie.addWord("ai");

trie.addWord("aqi");

trie.addWord("biiiyou");

trie.addWord("dqdi");

trie.addWord("ji");

trie.addWord("li");

trie.addWord("liqing");

trie.addWord("liqq");

trie.addWord("liqqq");

trie.addWord("qi");

trie.addWord("qibi");

trie.addWord("i");

trie.addWord("ibiyzbi");

List list=trie.prefixSearchWord("li");

for(String s: list){

System.out.println(s);

}

System.out.println("----------------------------------------");

List li=trie.searchWord("i");

for(String s: li){

System.out.println(s);

}

System.out.println("----------------------------------------");

List words=trie.searchWord("bi");

for(String s: words){

System.out.println(s);

}

System.out.println("----------------------------------------");

List lst=trie.searchWord("q");

for(String s: lst){

System.out.println(s);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值