-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
# PHP Interval tree | ||
Is an implementation of interval binary search tree according to Thomas H. Cormen book "Introduction to Algorithms". | ||
Is an implementation of interval binary search tree according to Thomas H. Cormen book "Introduction to Algorithms". | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
require_once 'vendor/autoload.php'; | ||
|
||
use Danon\IntervalTree\IntervalTree; | ||
|
||
$tree = new IntervalTree(); | ||
$intervals = [[6,8],[1,4],[2,3],[5,12],[1,1],[3,5],[5,7]]; | ||
|
||
// Insert interval as a key and string "val0", "val1" etc. as a value | ||
for ($i=0; $i < count($intervals); $i++) { | ||
$tree->insert($intervals[$i], "val" . $i); | ||
} | ||
|
||
// Get array of keys sorted in ascendant order | ||
$sorted_intervals = $tree->getKeys(); // expected array [[1,1],[1,4],[5,7],[5,12],[6,8]] | ||
|
||
// Search items which keys intersect with given interval, and return array of values | ||
$valuesInRange = $tree->search([2,3], function($value, $key) { | ||
return $value; | ||
}); | ||
|
||
print_r($valuesInRange); | ||
|
||
// Array | ||
// ( | ||
// [0] => val1 | ||
// [1] => val2 | ||
// [2] => val5 | ||
// ) | ||
``` |