Download the PHP package locr-company/splay-tree without Composer
On this page you can find all versions of the php package locr-company/splay-tree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download locr-company/splay-tree
More information about locr-company/splay-tree
Files in locr-company/splay-tree
Package splay-tree
Short Description Fast splay-tree data structure
License MIT
Informations about the package splay-tree
Fast splay tree
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
API
new SplayTree(callable $comparator = null)
, where$comparator
is 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): self
In-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 ifpresort
istrue
, 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
0
if the keys are equal<0
ifa < b
-
>0
ifa > 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)
andcomparator(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
Custom comparator (reverse sort)
Bulk insert