locr-company/splay-tree
Composer 安装命令:
composer require locr-company/splay-tree
包简介
Fast splay-tree data structure
关键字:
README 文档
README
Splay-tree: fast(non-recursive) and simple(< 1000 lines of code) Implementation is adapted directly from this GitHub Repository.
This tree is based on top-down splaying algorithm by D.Sleator. It supports
- splitting, merging
- updating of the keys
- bulk loading of the items into an empty or non-empty tree
- insertion with duplicates or no duplicates
- lookup without splaying
| Operation | Average | Worst case |
|---|---|---|
| Space | O(n) | O(n) |
| Search | O(log n) | amortized O(log n) |
| Insert | O(log n) | amortized O(log n) |
| Delete | O(log n) | amortized O(log n) |
Install
composer require locr-company/splay-tree
<?php use Locr\Lib\SplayTree\SplayTree; $tree = new SplayTree();
API
new SplayTree(callable $comparator = null), where$comparatoris optional comparison function$tree->insert(int|array $key, mixed $data = null): Node- Insert item, allow duplicate keys$tree->add(int|float $key, mixed $data = null): Node- Insert item if it is not present$tree->remove(int|float $key): void- Remove item$tree->find(int $key): ?Node- Return node by its key$tree->findStatic(int $key): ?Node- Return node by its key (doesn't re-balance the tree)$tree->at(int $index): ?Node- Return node by its index in sorted order of keys$tree->contains(int $key): bool- Whether a node with the given key is in the tree$tree->forEach(callable $visitor): selfIn-order traversal$tree->keys(): array- Returns the array of keys in order$tree->values(): array- Returns the array of data fields in order$tree->range(int $low, int $high, callable $fn): self- Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.$tree->pop(): array- Removes smallest node$tree->min(): int|float|array|null- Returns min key$tree->max(): int|float|array|null- Returns max key$tree->minNode(?Node $t = null): ?Node- Returns the node with smallest key$tree->maxNode(?Node $t = null): ?Node- Returns the node with highest key$tree->previousNode(Node $d): ?Node- Predecessor node$tree->nextNode(Node $d): ?Node- Successor node$tree->load(array $keys, array $values = [], bool $presort = false): self- Bulk-load items. It expects values and keys to be sorted, but ifpresortistrue, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
Comparator
function(int|float $a, int|float $b): int - Comparator function between two keys, it returns
0if the keys are equal<0ifa < b>0ifa > b
The comparator function is extremely important, in case of errors you might end
up with a wrongly constructed tree or would not be able to retrieve your items.
It is crucial to test the return values of your comparator(a, b) and comparator(b, a)
to make sure it's working correctly, otherwise you may have bugs that are very
unpredictable and hard to catch.
Duplicate keys
insert()method allows duplicate keys. This can be useful in certain applications (example: overlapping points in 2D).add()method will not allow duplicate keys - if key is already present in the tree, no new node is created
Example
<?php use Locr\Lib\SplayTree\SplayTree; $t = new SplayTree(); $t->insert(5); $t->insert(-10); $t->insert(0); $t->insert(33); $t->insert(2); print_r($t->keys()); /** * Array( * [0] => -10 * [1] => 0 * [2] => 2 * [3] => 5 * [4] => 33 * ) */ print $t->size; // 5 print $t->min(); // -10 print $t->max(); // 33 $t->remove(0); print $t->size; // 4
Custom comparator (reverse sort)
<?php use Locr\Lib\SplayTree\SplayTree; $t = new SplayTree(function ($a, $b) { return $b - $a; }); $t->insert(5); $t->insert(-10); $t->insert(0); $t->insert(33); $t->insert(2); print_r($t->keys()); /** * Array *( * [0] => 33 * [1] => 5 * [2] => 2 * [3] => 0 * [4] => -10 *) */
Bulk insert
<?php use Locr\Lib\SplayTree\SplayTree; $t = new SplayTree(); $t->load([3, 2, -10, 20], ['C', 'B', 'A', 'D']); print_r($t->keys()); /** * Array *( * [0] => 3 * [1] => 2 * [2] => -10 * [3] => 20 *) */ print_r($t->values()); /** * Array *( * [0] => C * [1] => B * [2] => A * [3] => D *) */
Develop
composer install
locr-company/splay-tree 适用场景与选型建议
locr-company/splay-tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「splay-tree」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 locr-company/splay-tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 locr-company/splay-tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-03