PHPで線分ツリーを実装する方法

醉折花枝作酒筹
リリース: 2023-03-10 22:46:01
転載
2012 人が閲覧しました

線分木は区間木と同様に二分探索木であり、区間をいくつかの単位区間に分割し、各単位区間が線分木の葉ノードに相当します。以下では、PHP で線分ツリーを実装する方法をエディターが共有しますので、必要に応じて参照してください。

PHPで線分ツリーを実装する方法

1. 機能

  • は必ずしも完全なバイナリ ツリーである必要はありません。

  • 平和であることバイナリ ツリー

  • リーフ ノードは実際の値を保存し、非リーフ ノードはカスタマイズされたコンテンツを保存します

2. 時間計算量の程度

#操作時間計算量クエリO(logn)
3. 線分ツリーの図

PHPで線分ツリーを実装する方法

4. コード

<?php
/**
 * content: 线段树(区间树)
 * create: 2020-11-12
 */

namespace HeapBundle;

use ArrayBundle\BaseArray;

class SegmentTreeHeap
{
    /**
     * 传入的数组对象
     * @var BaseArray 
     */
    protected $array;

    /**
     * 数组
     * @var array 
     */
    protected $tree = [];

    public function __construct(BaseArray $array)
    {
        $this->array = $array;
        $this->build(0, 0, $this->array->getSize() - 1);
    }

    /**
     * 构建线段树
     * @param int $treeIndex
     * @param int $min
     * @param int $max
     * @throws \Exception
     */
    public function build(int $treeIndex, int $min, int $max)
    {
        // 如果线段区间的最小值和最小值相同,则表示为叶子结点
        if ($min == $max) {
            $this->tree[$treeIndex] = $this->array->get($max);
            return;
        }

        // 四舍五入取中间值  最大值减最小值然后除以2拿到中间值,并加上最小值
        $mid = floor(($max - $min) / 2) + $min;

        // 获取左儿子的索引值,并递归往下构建
        $leftIndex = $this->leftChildIndex($treeIndex);
        $this->build($leftIndex, $min, $mid);

        // 获取右儿子的索引值,并递归往下构建
        $rightIndex = $this->rightChildIndex($treeIndex);
        $this->build($rightIndex, $mid + 1, $max);

        // 非叶子结点的值保留的是它下面所有结点的相加值, 这里可以改为它下面结点的总和值
        $this->tree[$treeIndex] = $this->tree[$leftIndex] . &#39;+&#39; . $this->tree[$rightIndex];
    }

    /**
     * 打印线段树
     */
    public function varDump()
    {
        ksort($this->tree);
        print_r($this->tree);
    }

    /**
     * 获取线段树的长度
     * @return int
     */
    public function getSize(): int
    {
        return count($this->tree);
    }

    /**
     * 获取左儿子索引
     * @param int $parentIndex
     * @return int
     * @throws \Exception
     */
    public function leftChildIndex(int $parentIndex): int
    {
        if ($parentIndex < 0) throw new \Exception(&#39;父结点的索引不能小于0&#39;);
        return $parentIndex * 2 + 1;
    }

    /**
     * 获取右儿子索引
     * @param int $parentIndex
     * @return int
     * @throws \Exception
     */
    public function rightChildIndex(int $parentIndex): int
    {
        if ($parentIndex < 0) throw new \Exception(&#39;父结点的索引不能小于0&#39;);
        return $parentIndex * 2 + 2;
    }
}
ログイン後にコピー

5. 例

<?php
require_once __DIR__ . &#39;/../../vendor/autoload.php&#39;;
$array = new ArrayBundleBaseArray();
for ($i = 0; $i < 10; $i++) {
 $array->addLast($i + 10);
}
$heap = new HeapBundleSegmentTreeHeap($array);
$heap->varDump();
ログイン後にコピー
Array
(
    [0] => 10+11+12+13+14+15+16+17+18+19
    [1] => 10+11+12+13+14
    [2] => 15+16+17+18+19
    [3] => 10+11+12
    [4] => 13+14
    [5] => 15+16+17
    [6] => 18+19
    [7] => 10+11
    [8] => 12
    [9] => 13
    [10] => 14
    [11] => 15+16
    [12] => 17
    [13] => 18
    [14] => 19
    [15] => 10
    [16] => 11
    [23] => 15
    [24] => 16
)
ログイン後にコピー

推奨学習:

php ビデオ チュートリアル

以上がPHPで線分ツリーを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:segmentfault.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!