Skip to content

Commit 66b93f6

Browse files
committed
Initial commit
0 parents  commit 66b93f6

File tree

10 files changed

+2492
-0
lines changed

10 files changed

+2492
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.phpunit.cache
3+
.vscode
4+
vendor

LICENCE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright 2024 Luke Watts
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8+

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Heap Class in PHP
2+
3+
The `Heap` class is an implementation of a Max Heap data structure in PHP. A Heap is a complete binary tree where the value of each node is greater than or equal to the values of its children, which makes it useful for efficiently finding the maximum element.
4+
5+
## Features
6+
7+
- **Heap Construction**: Initialize the heap with an array and build a max heap.
8+
- **Insert**: Add a new element to the heap.
9+
- **Remove**: Remove a specific element from the heap.
10+
- **Sort**: Perform heap sort and return the array sorted in ascending or descending order.
11+
12+
## Installation
13+
14+
Install the `Heap` class via composer:
15+
16+
```bash
17+
composer require affinity4/heap
18+
```
19+
20+
## Usage
21+
22+
Here is an example of how to use the `Heap` class:
23+
24+
```php
25+
<?php
26+
27+
require_once __DIR__ . '/vendor/autoload.php';
28+
29+
use Affinity4\Heap\Heap;
30+
31+
// Initialize the heap with an array
32+
$initialArray = [3, 10, 5, 1, 2, 7];
33+
$heap = new Heap($initialArray);
34+
35+
// Insert a new value
36+
$heap->insert(12);
37+
38+
// Sort the heap
39+
$sortedArray = $heap->sort();
40+
echo "Sorted Array: " . implode(', ', $sortedArray) . "\n";
41+
42+
// Remove a value from the heap
43+
$heap->remove(5);
44+
echo "Heap after removing 5: " . implode(', ', $heap->heap) . "\n";
45+
46+
```
47+
48+
## Tests
49+
50+
Run tests with:
51+
52+
```bash
53+
./vendor/bin/phpunit
54+
```

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "affinity4/heap",
3+
"description": "Full Binary Heap implementation with maxheap, minheap and memory-efficient O(nlogn) and space-complexity of O(1) heapsort algorithm",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Affinity4\\Heap\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Luke Watts",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require-dev": {
18+
"phpunit/phpunit": "^10.5"
19+
}
20+
}

0 commit comments

Comments
 (0)