cal7/trie

一个用于简化TRIE数据结构使用的包

dev-master 2017-01-31 09:06 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:13:14 UTC


README

一个用于简化PHP中trie数据结构使用的包。

允许插入和查找单词,从文本文件中生成,并为类似Scrabble的游戏提供搜索功能

单词插入

单词插入非常简单,只需

$trie = new Trie();
$trie->addWord("example");

或者如果您有一个包含单词列表的文本文件(每个单词一行),无论是本地还是在线,您可以使用

$trie1 = Trie::fromTextFile("/local/path/to/file.txt");
$trie2 = Trie::fromTextFile("https://example.com/file.txt");

单词查找

此包提供了许多与Scrabble相关的应用中非常有用的功能 - 单词查找。以下代码片段展示了这一功能

$trie = new Trie();
$trie->addWords([
  "example",
  "test",
  "tested",
  "testing"
]);
$letters = ["t", "e", "s", "t"];
$wildcardCount = 2; //represents a "blank" tile in the game of Scrabble
$trie->findWords($letters, $wildcardCount); //returns ["test", "tested"]