Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-on committed Mar 12, 2020
1 parent 6838c61 commit c514568
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
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
// )
```

0 comments on commit c514568

Please sign in to comment.