当前位置:首页 > 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;
    }
}

堆的应用示例

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

php实现堆

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实现websocket

php实现websocket

PHP 实现 WebSocket WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议。PHP 可以通过多种方式实现 WebSocket 功能,以下是几种常见的方法: 使用 Ratc…

php实现登录

php实现登录

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

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面向对…

php无限分类的实现

php无限分类的实现

PHP无限分类的实现 无限分类是一种常见的树形结构数据组织方式,适用于商品分类、菜单管理、地区选择等场景。以下是几种实现方法: 递归实现法 递归是最直观的实现方式,通过函数自身调用来遍历子分类。数据…

php 实现队列

php 实现队列

PHP 实现队列的方法 PHP 可以通过多种方式实现队列功能,以下是几种常见的实现方法: 使用数组实现队列 PHP 数组可以通过 array_push 和 array_shift 函数模拟队列的先进…

php变量的实现

php变量的实现

PHP变量的实现机制 PHP变量的实现基于Zend引擎的底层数据结构。核心结构体zval用于存储变量类型和值,其设计允许动态类型转换和高效内存管理。 zval结构体 PHP变量通过zval结构体实现…