日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TRIE - Data Structure

發(fā)布時(shí)間:2025/3/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TRIE - Data Structure 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Introduction

介紹

Trie,又稱單詞查找樹,是一種樹形結(jié)構(gòu),用于保存大量的字符串。它的優(yōu)點(diǎn)是:利用字符串的公共前綴來節(jié)約存儲(chǔ)空間。

Trie is an ordered tree data structure that uses strings as keys. Unlike Binary Trees, Tries do not store keys associated with the node. The key is actually determined based on the position of the node on the tree. Any descendants of a node shares a common prefix of the key string associated with that node. Hence, trie is also called as Prefix Tree. The word "trie" comes from Retrieval, and it is pronounced as "try".

Trie是一種以字符串為鍵的有序樹狀數(shù)據(jù)結(jié)構(gòu),與二叉樹不同的是, Trie并不會(huì)存儲(chǔ)節(jié)點(diǎn)鍵值,節(jié)點(diǎn)鍵值是被節(jié)點(diǎn)在樹狀數(shù)據(jù)結(jié)構(gòu)中的位置所決定的。一個(gè)特定節(jié)點(diǎn)的所有子孫的鍵值都有一個(gè)公共的前綴字符,所有Trie也被稱為前綴樹。 單詞“Trie”來自于英文單詞“Retrieve”,其發(fā)音與單詞“Try”相同。

Since this data structure is a prefix tree, trie is commonly used in Dictionaries, Phone Directories and matching algorithms. Trie is best-suited for phone directory (any matching application for that matter) because it is very efficient in matching strings.

因?yàn)檫@個(gè)數(shù)據(jù)結(jié)構(gòu)是一個(gè)前綴樹,Trie通常被用在字典算法中,尤其電話號(hào)碼目錄的匹配算法中。在字符串匹配中Trie是非常高效的,對(duì)于電話號(hào)碼目錄是非常適合的。

So I have decided to implement Trie myself in C#. I have created three classes:

所以我決定使用C#來實(shí)現(xiàn)一個(gè)Trie數(shù)據(jù)結(jié)構(gòu),定義了以下一些類:

  • Node: Represents a single tree node;
  • Node:樹節(jié)點(diǎn)類;
  • NodeCollection: Represents the children of a node;
  • NodeCollection:樹節(jié)點(diǎn)結(jié)合類;
  • Trie: Trie implementation to insert and search nodes.
  • TrieTrie類實(shí)現(xiàn)了節(jié)點(diǎn)的查找以及插入操作。

Implementation

實(shí)現(xiàn)

Node: Node represents a basic tree node. Node implements both Depth First and Breadth First algorithms to search its children. It also contains its Parent node and Children node. Node has a key and a Value. Key contains the single character and the value has the actual value of the node. The actual key of the node will be determined by suffixing the single character to its parent's key. Node has a special property called IsTerminal. This property is set to true if the key or a value represents a complete string.

NodeNode類代表了一個(gè)基本的節(jié)點(diǎn)類,該類同時(shí)實(shí)現(xiàn)了深度優(yōu)先和廣度優(yōu)先的子節(jié)點(diǎn)查找算法,該類包含了對(duì)父節(jié)點(diǎn)以及子節(jié)點(diǎn)的引用。Node類有兩個(gè)屬性KeyValueKey是一個(gè)單個(gè)字符,節(jié)點(diǎn)類真正的鍵值是以其Key屬性為后綴,然后依次加上其父節(jié)點(diǎn)的Key屬性。另外Node還有一個(gè)屬性叫IsTerminal,來表明其Value或者Key是否表示了一個(gè)完整的字符串。

See the picture below:

參見下面的圖片:

?

NodeCollection: This class is a simple collection class which implements the IEnumerable interface for iteration operations. This class contains an internal List class of type Node. NodeCollection implements the standard list methods such as Add( ), Contains( ), Remove( ) etc.

NodeCollection:該類是一個(gè)簡(jiǎn)單的集合類型,實(shí)現(xiàn)了用于枚舉的IEnumerable接口,其內(nèi)部包含了一個(gè)List<Node>類型,支持一些集合類的標(biāo)準(zhǔn)操作例如添加,刪除以及判斷是否包含指定節(jié)點(diǎn)等等操作。

Trie: This class implements the Trie algorithms such as Insert and Find methods.

Trie:該類實(shí)現(xiàn)了Trie算法,實(shí)現(xiàn)了插入和查找操作。

示例代碼:

//Inserts?Names?into?the?Trie?data?structure
public?static?Node?InsertNode(string?name,?Node?root)
{
????
//Is?name?null?
????if?(string.IsNullOrEmpty(name))
????????
throw?new?ArgumentNullException("Null?Key");

????
//set?the?index,?start?inserting?characters
????int?index?=?1;

????
//key
????string?key;

????
//start?with?the?root?node
????Node?currentNode?=?root;

????
//loop?for?all?charecters?in?the?name
????while?(index?<=?name.Length)
????{
????????
//get?the?key?character
????????key?=?name[index?-?1].ToString();

????????
//does?the?node?with?same?key?already?exist?
????????Node?resultNode?=?currentNode.Children.GetNodeByKey(key);

????????
//No,?this?is?a?new?key
????????if?(resultNode?==?null)
????????{
????????????
//Add?a?node
????????????Node?newNode?=?new?Node(key,?name.Substring(0,?index));

????????????
//If?reached?the?last?charaecter,?this?is?a?valid?full?name
????????????if?(index?==?name.Length)
????????????????newNode.IsTerminal?
=?true;

????????????
//add?the?node?to?currentNode(i.e.?Root?node?for?the?first?time)
????????????currentNode.Children.Add(newNode);

????????????
//set?as?the?current?node
????????????currentNode?=?newNode;
????????}
????????
else
????????{
????????????
//node?already?exist,?set?as?tghe?current?node
????????????
//and?move?to?the?next?character?in?the?name
????????????currentNode?=?resultNode;
????????}
????????
//move?to?the?next?character?in?the?name
????????index++;
????}
????
//all?done,?return?root?node
????return?root;
}

The Insert method inserts the string as one character at a time. It starts with the first character; if the first character doesn't already exist in the root node it adds a new node with the new character and returns the new node. Otherwise it returns the node with the fist character for adding remaining characters. It loops until it adds the entire string. Once it reaches the last character, it marks that node as a terminal node because this node represents a complete string in the tree hierarchy.

插入操作再插入一個(gè)字符串的時(shí)候,從第一個(gè)字符開始,每次只處理一個(gè)字符;如果第一個(gè)字符在根節(jié)點(diǎn)的子節(jié)點(diǎn)中沒有存在,那么會(huì)使用該字符添加一個(gè)新的節(jié)點(diǎn)然后返回,否則返回已經(jīng)存在的節(jié)點(diǎn),然后依次循環(huán)后面的字符串。一旦到達(dá)最后一個(gè)字符串,就會(huì)標(biāo)識(shí)該節(jié)點(diǎn)為一個(gè)終止節(jié)點(diǎn)(IsTerminalTrue),因?yàn)樵谡麄€(gè)樹結(jié)構(gòu)上其表示了一個(gè)完整的字符串。

The Find methods is implemented by Depth First search algorithm. The tree is searched until the complete string is found. Below is the code.

查找方法實(shí)現(xiàn)了深度優(yōu)先的查找算法,整個(gè)樹形數(shù)據(jù)結(jié)構(gòu)將被查找直至該字符串被找到。下面是示例代碼:


//Find?a?node?given?the?key("Jo")
public?static?bool?Find(Node?node,?string?key){????

????
//Is?key?empty
????if?(string.IsNullOrEmpty(key))
????????
return?true;//terminal?Node

????
//get?the?first?character
????string?first?=?key.Substring(0,?1);

????
//get?the?tail:?key?-?first?character
????string?tail?=?key.Substring(1);
????Node?curNode?
=?node.Children.GetNodeByKey(first);

????
//loop?until?you?locate?the?key?i.e.?"Jo"
????if?(curNode?!=?null)
????{
????????
return?Find(curNode,?tail);
????}
????
else
????{
????????
//not?found,?return?false
????????return?false;
????}
}

?

?

I've attached the entire source code above. The source code contains the Trie class library and a console application to test the Trie library. The console application loads a set of names (stored in names.txt in debug folder) in to the tree and provides options to run Depth First & Breadth First algorithm. The application also provides options for Directory Look-Up and Find option.

我已經(jīng)將源代碼添加在附件中了,源代碼中包含了Trie算法類庫一個(gè)Console測(cè)試程序。Console測(cè)試程序會(huì)加載一些字符串(存儲(chǔ)在Debug文件夾下的names.txt文件中)到Trie樹上,并且可以在深度優(yōu)先以及廣度優(yōu)先切換算法。

The class library can be further used to develop a web based phone directory. The data can also be stored on the client (it is too small) and the Trie can be implemented in JavaScript.

這個(gè)類庫可以被進(jìn)一步開發(fā)成為一個(gè)基于Web的電話目錄,數(shù)據(jù)可以存儲(chǔ)在客戶端,然后使用JavaScript來實(shí)現(xiàn)Trie算法。

Happy Coding

編程快樂!

原貼地址:http://www.codeproject.com/KB/recipes/PhoneDirectory.aspx

?

轉(zhuǎn)載于:https://www.cnblogs.com/tedzhao/archive/2008/10/19/1314665.html

總結(jié)

以上是生活随笔為你收集整理的TRIE - Data Structure的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。