字首樹 java實現)

2021-08-28 21:28:07 字數 1463 閱讀 8932

package class_07;

/** *

* 字首樹

* * 例子:

* 乙個字串型別的陣列arr1,另乙個字串型別的陣列arr2。

* arr2中有哪些字元,是arr1中出現的?請列印

* arr2中有哪些字元,是作為arr1中某個字串字首出現的?請列印

* arr2中有哪些字元,是作為arr1中某個字串字首出現的?請列印arr2中出現次數最大的字首。

* */

public

class

code_01_trietree

}public

static

class

trie

public

void

insert

(string word)

char

chs = word.

tochararray()

; trienode node = root;

int index =0;

for(

int i =

0; i < chs.length; i++

) node = node.nexts[index]

; node.path++;}

node.end++;}

public

void

delete

(string word)

node = node.nexts[index];}

node.end--;}

}public

intsearch

(string word)

char

chs = word.

tochararray()

; trienode node = root;

int index =0;

for(

int i =

0; i < chs.length; i++

) node = node.nexts[index];}

return node.end;

}public

intprefixnumber

(string pre)

char

chs = pre.

tochararray()

; trienode node = root;

int index =0;

for(

int i =

0; i < chs.length; i++

) node = node.nexts[index];}

return node.path;}}

public

static

void

main

(string[

] args)

}

字首樹python實現

file name 字首樹.py class trienode object def init self self.path 0 路過此節點幾個 self.end 0 以此為結尾的幾個 self.map none for i in range 26 每乙個節點有26條路 class trie obj...

Trie字首樹簡單實現

trie樹,字首樹,字典樹,又稱單詞查詢樹或鍵樹,是一種樹形結構。典型應用是用於統計和排序大量的字串 但不僅限於字串 可以用於搜尋引擎系統,用於文字詞頻統計。trie利用字串的公共字首來避免無謂的查詢,從而降低查詢時間的開銷以達到提高效率的目的。1.根節點不包含字元,除根節點外每乙個節點都只包含乙個...

字首樹的簡單實現

1.字首樹 字首樹又稱為單詞查詢樹,是一種樹形的結構,用於儲存大量的字串,它的優點是 利用字串的公共字首來節約儲存空間 trie樹主要是利用詞的公共字首縮小查詞範圍 通過狀態間的對映關係避免了字元的遍歷,從而達到高效檢索的目的 2.可以先宣告乙個節點trienode,節點包括以下幾個屬性 trien...