php head topN算法

原文https://diffnest.github.io/2019/07/01/PHP小顶堆实现TopK/
<?php
class Topk
{
    public $top;
    public $topkArr = array();
    public $topkList = array();
    
    public function __construct($topk) {
        $this->top = $topk;
    }

    public function swap(&$arr, $i, $j) 
    {
        $temp = $arr[$i];
        $arr[$i] = $arr[$j];
        $arr[$j] = $temp;
    }
    //n:节点
    //i: 从哪个节点heapify
    private function heapify(&$tree, $n, $i)
    {
        if ($i >= $n) {
            return; 
        }

        $c1 = (2 * $i) + 1;//左节点
        $c2 = (2 * $i) + 2;//右节点
        $max = $i;
   
        //左右节点内容跟父节点比较,确保父节点是最大值
        if ($c1 < $n && $tree[$c1] > $tree[$max]) {
            $max = $c1;
        }
        if ($c2 < $n && $tree[$c2] > $tree[$max]) {
            $max = $c2;
        }
        //当i是最大值时,不用交换
        if ($max != $i) {
            $this->swap($tree, $max, $i);
            //交换之后对下一层继续heapify
            $this->heapify($tree, $n, $max);
        }
    }

    //从下往上构建堆:节点3->节点2->节点1
    public function buildHeap(&$tree, $n) 
    {
        $lastNode = $n - 1;
        $parent = ($lastNode - 1) / 2;
        for ($i = $parent; $i >= 0; $i--) {
            $this->heapify($tree, $n, $i);
        }
    }

    public function heapSort(&$tree, $n)
    {
        $this->buildHeap($tree, $n);
        for($i = $n-1; $i >= 0; $i--) {
            $this->swap($tree, $i, 0);
            //剩下的i个元素重新构建成堆
            $this->heapify($tree, $i, 0);
        }
    }

    //调整
    public function adjust($value)
    {   
        if (in_array($value, $this->topkArr)) {
            return;
        }

        //记录原始数据
        $this->init($value);
        $len = count($this->topkList);

        if ($len < $this->top) {
            array_push($this->topkList, $value);
            $this->heapSort($this->topkList, $len);
        } else {
            //堆顶值与新值比较
            if ($this->topkList[0] < $value) {
                if (count($this->topkList) >= $this->top) {
                    $this->topkList[0] = $value;
                } else {
                    array_unshift($this->topkList, $value);
                }
                $this->heapSort($this->topkList, $len);
            }
        }
    }

    public function getTopK()
    {
        return $this->topkList;
    }

    public function init($value)
    {
        array_push($this->topkArr, $value);
    }

    public function getInitData()
    {
        return $this->topkArr;
    }

    public function calc()
    {
        for ($i = 0; $i < 10; $i++) {
            $this->adjust(mt_rand(1, 100));
        }
    }
}
$heapTree = new Topk(5);
$heapTree->calc();
echo '<pre>';
var_dump($heapTree->getInitData(), $heapTree->getTopK());



?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值