Trie樹演算法實現 c 版

2021-10-02 22:53:46 字數 2236 閱讀 2872

老規矩。。推薦部落格

trie樹詳解

**如下:

//

// created by administrator on 2020/2/18.

//#include

#include

#include

#include

#include

#include

#include

using

namespace std;

typedef

struct trienode

*lptnode,tnode;

vector

split

(const string& str,

const string& dlim)

return res;

}class

trietree};

trietree::

trietree()

int trietree::

insert

(const string& str)

int strlen = str.

length()

; lptnode atr = root;

int i;

for(i=

0;i)else

} atr-

>occurances++

;// 無論是找到還是插入 最後乙個字元都++;

if(atr-

>occurances==1)

this

->size++

;return1;

}bool trietree::

remove

(const string &str)

int strlen = str.

length()

; lptnode atr = root;

int i;

for(i=

0;i)else

}// 如果完整執行到這:一是有這個字串,二是有以該字串為字首的字串

bool residue = atr-

>occurances >0;

// 簡化表示式

if(residue)

return

false;}

bool trietree::

contains

(const string &str)

int strlen = str.

length()

; lptnode atr = root;

int i;

for(i=

0;i)else

}return atr-

>occurances >0;

// 簡化表示式

}int trietree::

frequency

(const string &str)

int strlen = str.

length()

; lptnode atr = root;

int i;

for(i=

0;i)else

}return atr-

>occurances;

// 簡化表示式

}int

main()

}int size = tt.

getsize()

; cout<<

"這個trie樹共有: "

<" 種不同單詞"

;for

(auto

const

&word:words)

cout

("die"

)

remove

("and");

cout<<

"and: "

("and"

)

remove

("the");

cout<<

"the: "

("the"

)

remove

("no");

cout<<

"no: "

("no"

)

}}

Trie樹的C 實現

先給出lintcode的題目 實現trie樹 trie樹的一般性質如下 1.根節點不包含字元,除根節點外每乙個節點都只包含乙個字元。2.從根節點到某一節點,路徑上經過的字元連線起來,為該節點對應的字串。3.每個節點的所有子節點包含的字元都不相同。那麼既然是樹,既然需要和字串匹配,那麼樹的節點怎麼定義...

Trie樹的C 實現

先給出lintcode的題目 實現trie樹 trie樹就是字典樹,用在搜尋引擎如搜尋詞條,還比如說之前dns網域名稱解析系統搜尋根據網域名稱搜尋ip。總之,是棵樹,根據字串搜尋某一節點,同時就可獲得節點儲存的資訊了。trie樹的一般性質如下 1.根節點不包含字元,除根節點外每乙個節點都只包含乙個字...

Trie樹(字典樹)的C 實現

trie樹,又稱字典樹 單詞查詢樹 字首樹,是一種雜湊樹的變種,應用於字串的統計與排序,經常被搜尋引擎系統用於文字詞頻統計。優點是查詢快,利用字串的公共字首來節省儲存空間,最大限度的減少無謂的字串比較。對於長度為m的鍵值,最壞情況下只需花費o m 的時間 而bst需要o mlogn 的時間。leet...