当前位置:首页 > PHP

php实现堆

2026-01-29 18:12:32PHP

堆的基本概念

堆是一种特殊的完全二叉树,分为最大堆和最小堆。最大堆的每个父节点值大于或等于其子节点值,最小堆则相反。堆常用于优先队列和堆排序算法。

php实现堆

PHP实现最大堆

以下代码展示如何用PHP实现最大堆的基本操作:

class MaxHeap {
    private $heap;

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

    private function parent($i) {
        return floor(($i - 1) / 2);
    }

    private function leftChild($i) {
        return 2 * $i + 1;
    }

    private function rightChild($i) {
        return 2 * $i + 2;
    }

    private function swap(&$x, &$y) {
        $temp = $x;
        $x = $y;
        $y = $temp;
    }

    public function insert($key) {
        array_push($this->heap, $key);
        $this->heapifyUp(count($this->heap) - 1);
    }

    private function heapifyUp($i) {
        while ($i > 0 && $this->heap[$this->parent($i)] < $this->heap[$i]) {
            $this->swap($this->heap[$i], $this->heap[$this->parent($i)]);
            $i = $this->parent($i);
        }
    }

    public function extractMax() {
        if (empty($this->heap)) {
            return null;
        }
        $max = $this->heap[0];
        $this->heap[0] = array_pop($this->heap);
        $this->heapifyDown(0);
        return $max;
    }

    private function heapifyDown($i) {
        $maxIndex = $i;
        $left = $this->leftChild($i);
        $right = $this->rightChild($i);
        $size = count($this->heap);

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

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

        if ($i != $maxIndex) {
            $this->swap($this->heap[$i], $this->heap[$maxIndex]);
            $this->heapifyDown($maxIndex);
        }
    }

    public function getMax() {
        return empty($this->heap) ? null : $this->heap[0];
    }

    public function size() {
        return count($this->heap);
    }
}

使用示例

$heap = new MaxHeap();
$heap->insert(3);
$heap->insert(9);
$heap->insert(2);
$heap->insert(1);
$heap->insert(7);

echo $heap->extractMax(); // 输出9
echo $heap->extractMax(); // 输出7

实现最小堆

将比较符号反转即可实现最小堆。修改heapifyUpheapifyDown中的比较符号:

private function heapifyUp($i) {
    while ($i > 0 && $this->heap[$this->parent($i)] > $this->heap[$i]) {
        // 改为大于号
        $this->swap($this->heap[$i], $this->heap[$this->parent($i)]);
        $i = $this->parent($i);
    }
}

private function heapifyDown($i) {
    $minIndex = $i;
    // 改为小于号比较
    if ($left < $size && $this->heap[$left] < $this->heap[$minIndex]) {
        $minIndex = $left;
    }
    // 改为小于号比较
    if ($right < $size && $this->heap[$right] < $this->heap[$minIndex]) {
        $minIndex = $right;
    }
}

性能分析

堆的插入和删除操作时间复杂度均为O(log n),获取最大/最小值操作时间复杂度为O(1)。这使得堆适合需要频繁访问极值元素的场景。

php实现堆

标签: php
分享给朋友:

相关文章

php实现文件下载

php实现文件下载

PHP 实现文件下载的方法 使用 header() 函数强制下载 设置正确的 HTTP 头信息,强制浏览器下载文件而不是直接打开。 $file = 'path/to/file.pdf'; if (…

php实现图片上传

php实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php 实现自动收货

php 实现自动收货

PHP 实现自动收货功能 自动收货功能通常用于电商系统,在订单超过一定时间未手动确认收货时自动完成订单。以下是实现方法: 数据库设计 需要订单表包含以下关键字段: CREATE TABLE o…

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $db…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注…