Skip to content

Commit c514568

Browse files
authored
Update README.md
1 parent 6838c61 commit c514568

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# PHP Interval tree
2-
Is an implementation of interval binary search tree according to Thomas H. Cormen book "Introduction to Algorithms".
2+
Is an implementation of interval binary search tree according to Thomas H. Cormen book "Introduction to Algorithms".
3+
4+
## Usage
5+
6+
```php
7+
<?php
8+
require_once 'vendor/autoload.php';
9+
10+
use Danon\IntervalTree\IntervalTree;
11+
12+
$tree = new IntervalTree();
13+
$intervals = [[6,8],[1,4],[2,3],[5,12],[1,1],[3,5],[5,7]];
14+
15+
// Insert interval as a key and string "val0", "val1" etc. as a value
16+
for ($i=0; $i < count($intervals); $i++) {
17+
$tree->insert($intervals[$i], "val" . $i);
18+
}
19+
20+
// Get array of keys sorted in ascendant order
21+
$sorted_intervals = $tree->getKeys(); // expected array [[1,1],[1,4],[5,7],[5,12],[6,8]]
22+
23+
// Search items which keys intersect with given interval, and return array of values
24+
$valuesInRange = $tree->search([2,3], function($value, $key) {
25+
return $value;
26+
});
27+
28+
print_r($valuesInRange);
29+
30+
// Array
31+
// (
32+
// [0] => val1
33+
// [1] => val2
34+
// [2] => val5
35+
// )
36+
```

0 commit comments

Comments
 (0)