数据结构
在线手册:中文 英文
PHP手册

The SplPriorityQueue class

(PHP 5 >= 5.3.0)

简介

The SplPriorityQueue class provides the main functionalities of an prioritized queue, implemented using a heap.

类摘要

SplPriorityQueue implements Iterator , Countable {
/* 方法 */
__construct ( void )
int compare ( mixed $priority1 , mixed $priority2 )
int count ( void )
mixed current ( void )
mixed extract ( void )
void insert ( mixed $value , mixed $priority )
bool isEmpty ( void )
mixed key ( void )
void next ( void )
void recoverFromCorruption ( void )
void rewind ( void )
void setExtractFlags ( int $flags )
mixed top ( void )
bool valid ( void )
}

Table of Contents


数据结构
在线手册:中文 英文
PHP手册
PHP手册 - N: The SplPriorityQueue class

用户评论:

Anonymous (14-Jan-2011 03:12)

This documentation does not mention whether this is a min-heap or a max-heap.

rajatn at rediff dot co dot in (30-Jul-2010 12:29)

quick implementation of SPL Priority Queue:

<?php

class PQtest extends SplPriorityQueue
{
    public function
compare($priority1, $priority2)
    {
        if (
$priority1 === $priority2) return 0;
        return
$priority1 < $priority2 ? -1 : 1;
    }
}

$objPQ = new PQtest();

$objPQ->insert('A',3);
$objPQ->insert('B',6);
$objPQ->insert('C',1);
$objPQ->insert('D',2);

echo
"COUNT->".$objPQ->count()."<BR>";

//mode of extraction
$objPQ->setExtractFlags(PQtest::EXTR_BOTH);

//Go to TOP
$objPQ->top();

while(
$objPQ->valid()){
   
print_r($objPQ->current());
    echo
"<BR>";
   
$objPQ->next();
}

?>

output:

COUNT->4
Array ( [data] => B [priority] => 6 )
Array ( [data] => A [priority] => 3 )
Array ( [data] => D [priority] => 2 )
Array ( [data] => C [priority] => 1 )