php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51141 Add min-max heap to SPL
Submitted: 2010-02-25 11:17 UTC Modified: 2020-06-10 10:31 UTC
From: jeroen at jeroen-vandeven dot nl Assigned:
Status: Open Package: SPL related
PHP Version: 5.3.1 OS:
Private report: No CVE-ID: None
 [2010-02-25 11:17 UTC] jeroen at jeroen-vandeven dot nl
Description:
------------
Add a deleteMin() method to SplMaxHeap (and/or a deleteMax() to 
SplMinHeap) to limit the size of the heap without taking off the top 
element.

Reproduce code:
---------------
$heap = new SplMaxHeap();
for ($i = 0; $i < 10000; $i++) {
$heap.insert($arr[$i])
if ($heap.count() > 100) {
$heap.deleteMin();
}
}

Expected result:
----------------
The example is pretty useless, it will add 100 elements to the heap and 
then add 9900 more, immediately removing each one.

This is useful in other situations where you may only be interested in 
the top 100 sorted elements of 10000 unsorted elements and don't want 
the heap to grow excessively large.

Actual result:
--------------
There is no deleteMin() method in SplMaxHeap

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-09 20:51 UTC] jani@php.net
-Package: Feature/Change Request +Package: SPL related
 [2010-04-27 09:37 UTC] colder@php.net
One problem that I see with a deleteMin in a MaxHeap is that there is no easy way 
to find the min in the heap, so it's not a common operation on heaps.
 [2020-06-10 10:31 UTC] nikic@php.net
-Summary: Add a deleteMin() method to SplMaxHeap +Summary: Add min-max heap to SPL
 [2020-06-10 10:31 UTC] nikic@php.net
This would require a min-max heap, which is a completely different data structure.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC