当前位置:首页 > PHP

php堆实现

2026-02-28 10:26:02PHP

PHP 堆(Heap)实现方法

堆是一种特殊的树形数据结构,通常用于实现优先队列。PHP 提供了内置的 SplHeap 类,同时也支持通过数组手动实现堆。

php堆实现

使用 SplHeap

PHP 的 SPL(Standard PHP Library)提供了 SplHeap 抽象类,可以快速实现最大堆或最小堆。

// 最小堆实现
class MinHeap extends SplHeap {
    protected function compare($value1, $value2) {
        return $value2 - $value1;
    }
}

// 最大堆实现
class MaxHeap extends SplHeap {
    protected function compare($value1, $value2) {
        return $value1 - $value2;
    }
}

// 示例用法
$minHeap = new MinHeap();
$minHeap->insert(3);
$minHeap->insert(1);
$minHeap->insert(4);
echo $minHeap->extract(); // 输出 1

手动实现堆(基于数组)

如果需要更灵活的堆实现,可以通过数组手动构建:

class BinaryHeap {
    private $heap;

    public function __construct() {
        $this->heap = [];
    }

    // 插入元素并调整堆
    public function insert($item) {
        $this->heap[] = $item;
        $this->heapifyUp(count($this->heap) - 1);
    }

    // 提取堆顶元素
    public function extract() {
        if (count($this->heap) === 0) {
            return null;
        }

        $root = $this->heap[0];
        $last = array_pop($this->heap);

        if (count($this->heap) > 0) {
            $this->heap[0] = $last;
            $this->heapifyDown(0);
        }

        return $root;
    }

    // 向上调整(用于插入)
    private function heapifyUp($index) {
        $parent = ($index - 1) >> 1;

        while ($index > 0 && $this->heap[$parent] > $this->heap[$index]) {
            $this->swap($parent, $index);
            $index = $parent;
            $parent = ($index - 1) >> 1;
        }
    }

    // 向下调整(用于提取)
    private function heapifyDown($index) {
        $left = 2 * $index + 1;
        $right = 2 * $index + 2;
        $smallest = $index;

        if ($left < count($this->heap) && $this->heap[$left] < $this->heap[$smallest]) {
            $smallest = $left;
        }

        if ($right < count($this->heap) && $this->heap[$right] < $this->heap[$smallest]) {
            $smallest = $right;
        }

        if ($smallest !== $index) {
            $this->swap($index, $smallest);
            $this->heapifyDown($smallest);
        }
    }

    // 交换元素
    private function swap($i, $j) {
        $temp = $this->heap[$i];
        $this->heap[$i] = $this->heap[$j];
        $this->heap[$j] = $temp;
    }
}

性能对比

  • SplHeap:适合快速实现标准堆操作,但灵活性较低。
  • 手动实现:适合需要自定义比较逻辑或特殊调整的场景。

应用场景

  1. 优先队列:任务调度系统。
  2. Top K 问题:快速获取最大或最小的 K 个元素。
  3. 堆排序:时间复杂度为 O(n log n) 的排序算法。

php堆实现

标签: php
分享给朋友:

相关文章

git php实现

git php实现

Git 与 PHP 的集成实现 使用 PHP 执行 Git 命令 通过 PHP 的 exec()、shell_exec() 或 passthru() 函数可以直接调用 Git 命令,适用于服务器端操作…

php 实现购物车

php 实现购物车

PHP 实现购物车功能 购物车功能是电子商务网站的核心部分,可以通过 PHP 和 Session 或数据库来实现。以下是两种常见的实现方式: 使用 Session 存储购物车数据 Sessi…

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inte…

php 伪静态实现

php 伪静态实现

PHP 伪静态实现方法 伪静态(URL Rewrite)是一种通过服务器配置将动态URL转换为静态URL的技术,提升URL的可读性和SEO效果。以下是几种常见的实现方式: 使用Apache的mo…

php数据库实现

php数据库实现

PHP 连接 MySQL 数据库 使用 mysqli 扩展连接 MySQL 数据库: <?php $servername = "localhost"; $username = "userna…

php实现评论功能

php实现评论功能

数据库设计 创建评论表(示例结构): CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id`…