Trie 字典樹 字首樹

2021-09-11 06:53:18 字數 4208 閱讀 9380

所有的php檔案放到同乙個目錄下

../trie/

├── charmap.php

├── map.php

├── stdmap.php

├── trie.php

├── trienode.php

├── index.php

├── test.php~

└── words.txt

* trienode.php

<?php

/** * class trienode

* 字典樹是利用字串的公共字首來節約儲存空間

*/class trienode

public function setpath(int $n)

public function getpath() : int

public function setend(int $n)

public function getend() : int

}

* trie.php

<?php

/** * created by phpstorm.

* user: mch

* date: 2019-02-23

* time: 20:29

*/class trie

private function newnode()

/*** 新增word 可重複新增

* @param string $word

* @return trie

*/public function insert(string $word) : trie

/** @var $node trienode */

$node = $this->root;

$len = strlen($word);

for ($i = 0; $i < $len; $i++)

$node = $node->map->get($ch);

$node->setpath($node->getpath()+1);

}$node->setend($node->getend()+1 );

return $this;

}/**

* 刪除word, 如果word新增過多次,僅刪除乙個

* @param string $word

*/public function delete(string $word)

$node = $this->root;

$len = strlen($word);

for ($i = 0; $i < $len; $i++)

$node = $child;

}$node->setend( $node->getend()-1 );

}/**

* 查詢word是否在字典樹中

* @param string $word

* @return bool

*/public function contains(string $word) : bool

return $node->getend() !== 0;

}/**

* 根據單詞的每個字母, 順著字典樹找到最後乙個匹配的節點

* @param string $word

* @return trienode|null

*/private function _find(string $word)

/** @var $node trienode */

$node = $this->root;

$len = strlen($word);

for ($i = 0; $i < $len; $i++)

$node = $node->map->get($ch);

}return $node;

}/**

* 返回以字串為字首的單詞數量

* @param string $pre

* @return int

*/public function prefixnumber(string $pre)

$node = $this->root;

$len = strlen($pre);

for ($i = 0; $i < $len; $i++)

$node = $node->map->get($ch);

}return $node->getpath();

}}

* index.php

<?php

/** * created by phpstorm.

* user: mch

* date: 2019-02-23

* time: 20:28

*/function __autoload($classname)

function getrows($path)

while (!$file->eof())

}$trie = new trie(charmap::class);

// $trie = new trie(stdmap::class);

try

} catch (exception $e)

var_dump($trie->contains('contain'));

var_dump($trie->contains('contaminated'));

var_dump($trie->contains('excite'));

$trie->delete('excite');

var_dump($trie->contains('excite'));

printf("以字串con為字首的單詞數量: %d\n", $trie->prefixnumber('con'));

* words.txt

consider

contaminated

consequence

means

transport

board

destiny

destination

simply

basic

backpack

spider

stream

normal

excite

excitement

exciting

converse

conserve

map 字元 => 子節點

* map.php

<?php

inte***ce map

* charmap.php

<?php

class charmap implements map

public function put($key, $value)

public function get($key)

public function remove($key)

private function _findindex($key)

public function clear()

return $n;

}}

* stdmap.php

<?php

class stdmap implements map

public function put($key, $value)

public function get($key)

return null;

}public function remove($key)

return null;

}public function clear()

return $i;

}}

* test:

$ php index.php 

bool(false)

bool(true)

bool(true)

bool(false)

以字串con為字首的單詞數量: 4

切換不同的map, 得到相同的結果

$trie = new trie(charmap::class);

// $trie = new trie(stdmap::class);

Trie 字典樹 字首樹

目錄trie是乙個多叉樹,trie專門為處理字串而設計的。使用我們之前實現的二分搜尋樹來查詢字典中的單詞,查詢的時間複雜度為o logn 如果有100萬 220 個單詞,則logn大約等於20,但是使用trie這種資料結構,查詢每個條目的時間複雜度,和一共有多少個條目無關!時間複雜度為o w w為被...

Trie樹,字典樹,字首樹

trie樹,字典樹,字首樹,都是同一顆樹,雜湊樹的變種題目鏈結 常用於文字的詞頻統計 它的特點就是,空間占用小,查詢快 它的儲存方式如下圖所示 對於從樹的根節點走到每乙個黑色節點所經過的路徑,如果將路徑上的字母都連起來的話,就都對應著詞典中的乙個單詞 trie樹,分別有插入,查詢和刪除3種操作,插入...

Trie樹 字典樹 字首樹

trie樹,即字典樹,又稱單詞查詢樹或鍵樹,是一種樹形結構,是一種雜湊樹的變種。典型應用是用於統計和排序大量的字串 但不僅限於字串 所以經常被搜尋引擎系統用於文字詞頻統計。它的優點是 最大限度地減少無謂的字串比較,查詢效率比雜湊表高。trie的核心思想是空間換時間。利用字串的公共字首來降低查詢時間的...