企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>[danger] 注意,因为是`堆`实现,所以`rewind`方法是一个`no-op`没有什作用的操作,因为`头指针`始终指向`堆顶`,即`current`始终等于`top`,不像`List`只是游走指针,出队是会删除堆元素的,`extract`\=`current + next`(current出队,从堆中删除) ## **例子:** ``` $obj=new SplMinHeap(); $obj->insert( 4 ); $obj->insert( 8 ); $obj->insert( 1 ); $obj->insert( 0 ); foreach( $obj as $number ) { echo $number."\n"; } 结果:0 1 4 8 ``` 等同于: ``` class MySimpleHeap extends SplHeap { public function compare( $value1, $value2 ) { return ( $value2 - $value1 ); } } $obj = new MySimpleHeap(); $obj->insert( 4 ); $obj->insert( 8 ); $obj->insert( 1 ); $obj->insert( 0 ); foreach( $obj as $number ) { echo $number."\n"; } 结果:0 1 4 8 ``` ## **例子:** ``` <?php $h = new SplMinHeap(); // [parent, child] $h->insert([9, 11]); $h->insert([0, 1]); $h->insert([1, 2]); $h->insert([1, 3]); $h->insert([1, 4]); $h->insert([1, 5]); $h->insert([3, 6]); $h->insert([2, 7]); $h->insert([3, 8]); $h->insert([5, 9]); $h->insert([9, 10]); for ($h->top(); $h->valid(); $h->next()) { list($parentId, $myId) = $h->current(); echo "$myId ($parentId)\n"; } ?> 结果: 1 (0) 2 (1) 3 (1) 4 (1) 5 (1) 7 (2) 6 (3) 8 (3) 9 (5) 10 (9) 11 (9) ```