diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php new file mode 100644 index 0000000..2a6a452 --- /dev/null +++ b/src/helper/MinHeap.php @@ -0,0 +1,62 @@ + $data to be inserted into the heap + */ + public function __construct(array $data) { + foreach ($data as $item) { + $this->insert($item); + } + } + + /** + * Insert the provided into the heap + * + * @param int|float $value + * @return void + */ + public function insert(int|float $value): void + { + $length = count($this->heap); + $this->heap[$length] = $value; + if ($length == 0) { + return; + } + $index = $length; + while ($index > 0 && $this->heap[(int)(($index - 1) / 2)] < $value) { + [$this->heap[(int)(($index - 1) / 2)], $this->heap[$index]] = [$this->heap[$index], $this->heap[(int)(($index - 1) / 2)]]; + $index = (int)(($index - 1) / 2); + } + } + + /** + * Delete the provided value from the heap + * + * @param int|float $value + * @return void + */ + public function delete(int|float $value): void {} + + /** + * Returns the min value of the heap or null if heap is empty. + * + * @return int|float|null The peak value, or null if not applicable. + */ + public function peak(): int|float|null + { + return $this->heap[0] ?? null; + } + + /** + * Heapify the array + * + * @return void + */ + private function heapify(): void {} +}