Java中關(guān)于字典樹的算法實(shí)現(xiàn)
字典樹(前綴樹)算法實(shí)現(xiàn)
前言
字典樹,又稱單詞查找樹,是一個(gè)典型的 一對(duì)多的字符串匹配算法。“一”指的是一個(gè)模式串,“多”指的是多個(gè)模板串。字典樹經(jīng)常被用來統(tǒng)計(jì)、排序和保存大量的字符串。它利用字符串的公共前綴來減少查詢時(shí)間,最大限度地減少無謂的字符串比較。
字典樹有3個(gè)基本性質(zhì):
- 根節(jié)點(diǎn)不包含字符,其余的每個(gè)節(jié)點(diǎn)都包含一個(gè)字符;
- 從根節(jié)點(diǎn)到某一節(jié)點(diǎn),路徑上經(jīng)過的字符連接起來,為該節(jié)點(diǎn)對(duì)應(yīng)的字符串;
- 每個(gè)節(jié)點(diǎn)的所有子節(jié)點(diǎn)包含的字符都不相同。

pass參數(shù):代表從這個(gè)點(diǎn)經(jīng)過的單詞數(shù)量。root根即就是整棵樹有多少單詞。
end參數(shù): 代表在這個(gè)點(diǎn)結(jié)束的單詞有幾個(gè)。例如: 上圖有兩個(gè) hello,在o結(jié)點(diǎn)的end參數(shù)就是2。
實(shí)現(xiàn)的基本功能: 增刪查。
算法解析
首先是結(jié)點(diǎn)的參數(shù):
public class Node {
public int pass;
public int end;
public Node[] nexts; //下一個(gè)字母的地址
public Node() {
pass = 0;
end = 0;
nexts = new Node[26]; //這里我們就以小寫字母為例
}
}
下面就是基本功能的實(shí)現(xiàn):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] arr = {"hello", "hello"};
Trie root = new Trie();
for (int i = 0; i < arr.length; i++) {
root.addWord(arr[i]);
}
//root.delWord("hello");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if (root.searchWord(s) != 0) {
System.out.println("該字典樹有這個(gè)" + s + " 單詞");
}
}
public static class Node {
public int pass;
public int end;
public Node[] nexts; //下一個(gè)字母的地址
public Node() {
pass = 0;
end = 0;
nexts = new Node[26];
}
}
public static class Trie {
private Node root;
public Trie() {
root = new Node();
}
//增加
public void addWord(String str) {
char[] arr = str.toCharArray();
root.pass++;
Node node = root;
for (char s : arr) {
int index = s - 'a'; //以相應(yīng)的ASCII碼值差值,進(jìn)行數(shù)組的下標(biāo)存儲(chǔ)
if (node.nexts[index] == null) {
node.nexts[index] = new Node();
}
node = node.nexts[index];
node.pass++; //經(jīng)過這個(gè)結(jié)點(diǎn),pass就加1
}
node.end++;
}
//刪除
public void delWord(String str) {
//刪除之前,應(yīng)該查詢一下這顆樹有沒有這個(gè)單詞
while (searchWord(str) != 0) {
char[] arr = str.toCharArray();
Node node = root;
node.pass--;
for (int i = 0; i < str.length(); i++) {
int index = arr[i] - 'a';
node = node.nexts[index];
node.pass--;
}
node.end--;
}
}
//查找
public int searchWord(String str) {
if (str == null) {
return 0;
}
char[] arr = str.toCharArray();
Node node = root;
for (int i = 0; i < str.length(); i++) {
int index = arr[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end; //返回最后那一個(gè)結(jié)點(diǎn)的end值即可
}
}
}
到此這篇關(guān)于Java中關(guān)于字典樹的算法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java 字典樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Java class文件格式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
對(duì)于理解JVM和深入理解Java語言, 學(xué)習(xí)并了解class文件的格式都是必須要掌握的功課2017-06-06
JAVA發(fā)送HTTP請(qǐng)求的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于JAVA發(fā)送HTTP請(qǐng)求的多種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類,可變參數(shù)
這篇文章主要介紹了Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類,可變參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-05-05
springboot 使用yml配置文件自定義屬性的操作代碼
在SpringBoot中yml/yaml文件可以自定義一些屬性,以供注入給自定義bean對(duì)象的屬性,主要通過空格和層次來實(shí)現(xiàn),類似于python代碼,本文通過實(shí)例代碼給大家介紹springboot 使用yml配置文件自定義屬性,感興趣的朋友跟隨小編一起看看吧2024-03-03
Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解
這篇文章主要介紹了Java 高并發(fā)的三種實(shí)現(xiàn)案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Struts2源碼分析之ParametersInterceptor攔截器
這篇文章主要介紹了Struts2源碼分析之ParametersInterceptor攔截器,ParametersInterceptor攔截器其主要功能是把ActionContext中的請(qǐng)求參數(shù)設(shè)置到ValueStack中,,需要的朋友可以參考下2019-06-06

