From ac38f96657d58997c191c5132568f1dcc752dd98 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Fri, 27 Dec 2024 15:16:33 +0100 Subject: [PATCH 1/5] initial commit --- src/helper/MinHeap.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/helper/MinHeap.php diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php new file mode 100644 index 0000000..eebece7 --- /dev/null +++ b/src/helper/MinHeap.php @@ -0,0 +1,33 @@ +heap[0] ?? null; + } +} From 4a43b5aa94d6c714a92d92b8ec3c62ca1682983f Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Fri, 27 Dec 2024 15:18:49 +0100 Subject: [PATCH 2/5] formatting --- src/helper/MinHeap.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php index eebece7..39b056b 100644 --- a/src/helper/MinHeap.php +++ b/src/helper/MinHeap.php @@ -27,7 +27,8 @@ public function delete(int|float $value): void {} * * @return int|float|null The peak value, or null if not applicable. */ - public function peak(): int|float|null { + public function peak(): int|float|null + { return $this->heap[0] ?? null; } } From b1dd109d75cede9fbc7551e6e412eda434c4e831 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Fri, 27 Dec 2024 15:19:12 +0100 Subject: [PATCH 3/5] add heapify method --- src/helper/MinHeap.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php index 39b056b..b2109a5 100644 --- a/src/helper/MinHeap.php +++ b/src/helper/MinHeap.php @@ -31,4 +31,11 @@ public function peak(): int|float|null { return $this->heap[0] ?? null; } + + /** + * Heapify the array + * + * @return void + */ + private function heapify(): void {} } From dea5b83e5d44f803e5d57f1a44e96b064338e8e3 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Fri, 27 Dec 2024 15:36:44 +0100 Subject: [PATCH 4/5] add insert implementation --- src/helper/MinHeap.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php index b2109a5..cd7e95d 100644 --- a/src/helper/MinHeap.php +++ b/src/helper/MinHeap.php @@ -12,7 +12,19 @@ class MinHeap * @param int|float $value * @return void */ - public function insert(int|float $value): 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 From 42ed38f5506d77faf59ddcdccf6ffbee0b859bef Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Fri, 27 Dec 2024 15:38:56 +0100 Subject: [PATCH 5/5] add constructor utilizing the insert method --- src/helper/MinHeap.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/helper/MinHeap.php b/src/helper/MinHeap.php index cd7e95d..2a6a452 100644 --- a/src/helper/MinHeap.php +++ b/src/helper/MinHeap.php @@ -6,6 +6,15 @@ class MinHeap { private array $heap; + /** + * @param array $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 *