当前位置:首页 > PHP

php实现堆

2026-02-14 16:09:50PHP

PHP 实现堆数据结构

堆(Heap)是一种特殊的完全二叉树,分为最大堆和最小堆。最大堆的每个父节点值大于或等于子节点值,最小堆则相反。PHP 可以通过数组模拟堆结构,以下是实现方法。

php实现堆

最大堆的实现

class MaxHeap {
    private $heap;

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

    public function insert($value) {
        $this->heap[] = $value;
        $this->heapifyUp(count($this->heap) - 1);
    }

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

    private function heapifyUp($index) {
        $parent = floor(($index - 1) / 2);
        if ($index > 0 && $this->heap[$index] > $this->heap[$parent]) {
            $this->swap($index, $parent);
            $this->heapifyUp($parent);
        }
    }

    private function heapifyDown($index) {
        $left = 2 * $index + 1;
        $right = 2 * $index + 2;
        $largest = $index;

        if ($left < count($this->heap) && $this->heap[$left] > $this->heap[$largest]) {
            $largest = $left;
        }
        if ($right < count($this->heap) && $this->heap[$right] > $this->heap[$largest]) {
            $largest = $right;
        }
        if ($largest !== $index) {
            $this->swap($index, $largest);
            $this->heapifyDown($largest);
        }
    }

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

最小堆的实现

class MinHeap {
    private $heap;

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

    public function insert($value) {
        $this->heap[] = $value;
        $this->heapifyUp(count($this->heap) - 1);
    }

    public function extractMin() {
        if (count($this->heap) === 0) {
            return null;
        }
        $min = $this->heap[0];
        $last = array_pop($this->heap);
        if (count($this->heap) > 0) {
            $this->heap[0] = $last;
            $this->heapifyDown(0);
        }
        return $min;
    }

    private function heapifyUp($index) {
        $parent = floor(($index - 1) / 2);
        if ($index > 0 && $this->heap[$index] < $this->heap[$parent]) {
            $this->swap($index, $parent);
            $this->heapifyUp($parent);
        }
    }

    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;
    }
}

堆的应用示例

堆常用于优先级队列、堆排序等场景。以下是使用最大堆进行堆排序的示例:

function heapSort($array) {
    $heap = new MaxHeap();
    foreach ($array as $value) {
        $heap->insert($value);
    }
    $sorted = [];
    while ($max = $heap->extractMax()) {
        array_unshift($sorted, $max);
    }
    return $sorted;
}

$unsorted = [3, 7, 2, 9, 1, 5];
$sorted = heapSort($unsorted);
print_r($sorted); // 输出: [1, 2, 3, 5, 7, 9]

性能分析

堆的插入和删除操作的时间复杂度为 O(log n),堆排序的时间复杂度为 O(n log n)。空间复杂度为 O(n),因为需要存储堆结构。

通过上述实现,可以在 PHP 中高效地使用堆数据结构解决相关问题。

php实现堆

标签: php
分享给朋友:

相关文章

php实现图片上传

php实现图片上传

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

php实现定时任务

php实现定时任务

PHP 实现定时任务的几种方法 在 PHP 中实现定时任务可以通过多种方式完成,具体选择取决于项目需求和服务器环境。以下是常见的实现方法: 使用 Cron 任务 Cron 是 Linux 系统中常…

php 登录实现

php 登录实现

PHP 登录实现 PHP 登录功能通常包括用户认证、会话管理和安全性措施。以下是实现 PHP 登录功能的关键步骤。 数据库准备 创建一个用户表存储用户信息,通常包括用户名、密码(加密存储)和其他必…

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $num…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证码…

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…