前缀搜索树的使用例子(PHP实现)

文章背景

根据PM的期望,我们最终需要给出归类好的数据让用户便捷查看。为啥不用MySQL的group by 是因为数据来源是其它系统的ES结果。

前缀树的作用

trie tree 译为前缀树或字典树。因为子孙节点的前缀都一样的关系,我们可以用它做路由、单词联想、归类排序等。

因为需求要为搜索页做同类分组,方便用户查看。因此思路是用 trie tree 分类然后按照子叶的后缀搜索顺序将乱掉的商品数据做排序。(看最后)

预期结果

php 复制代码
$line = [
            [1, 3, 2, 4, 5, 6, 7],
            [2, 3, 2, 4, 5, 6, 7],
            [2, 3, 1, 4, 5],
            [1, 3, 2, 4, 5, 7, 6],
        ];

代码

php 复制代码
public function insert3(array $parts, int $height, &$tree = [])
    {
        if (count($parts) <= $height) {
            $tree['isEnd'] = true;
            return;
        }
        $part  = $parts[$height];
        [$key,$child] = $this->mathChild($tree['children'], $part);
        if ($height == 0 ) {
            $this->warn(json_encode($tree['children']));
        }
        if ( ! $child) {
            $child = $this->newTrie($part);
            $this->insert3($parts, $height + 1, $child);
            $tree['children'][] = $child;
        } else {
            // 下面这一段非常地重要,它决定了同级兄弟成员
            $this->insert3($parts, $height + 1, $tree['children'][$key]);
        }
    }

public function mathChild(array $children, mixed $part)
    {
        foreach ($children as $key => $child) {
            if ($child['part'] == $part) {
                return [
                    $key,
                    $child,
                ];
            }
        }
        return null;
    }
private function newTrie(mixed $part): array
    {
        return [
            'children' => [],
            'isEnd'    => false,
            'part'     => $part,
        ];
    }
    
public function init()
{
    $line = [
            [1, 3, 2, 4, 5, 6, 7],
            [2, 3, 2, 4, 5, 6, 7],
            [2, 3, 1, 4, 5],
            [1, 3, 2, 4, 5, 7, 6],
        ];
    $tree = $this->newTrie('/');
        foreach ($line as $item) {
            $this->insert3($item, 0, $tree);
        }
    echo json_encode($tree, 256);
}

结果Json

ruby 复制代码
{"children":[{"children":[{"children":[{"children":[{"children":[{"children":[{"children":[{"children":[],"isEnd":true,"part":7}],"isEnd":false,"part":6},{"children":[{"children":[],"isEnd":true,"part":6}],"isEnd":false,"part":7}],"isEnd":false,"part":5}],"isEnd":false,"part":4}],"isEnd":false,"part":2}],"isEnd":false,"part":3}],"isEnd":false,"part":1},{"children":[{"children":[{"children":[{"children":[{"children":[{"children":[{"children":[],"isEnd":true,"part":7}],"isEnd":false,"part":6}],"isEnd":false,"part":5}],"isEnd":false,"part":4}],"isEnd":false,"part":2},{"children":[{"children":[{"children":[],"isEnd":true,"part":5}],"isEnd":false,"part":4}],"isEnd":false,"part":1}],"isEnd":false,"part":3}],"isEnd":false,"part":2}],"isEnd":false,"part":"\/"}

需求背景

现状

期望

相关推荐
Kalika0-044 分钟前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家1 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
小鹿( ﹡ˆoˆ﹡ )1 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
小字节,大梦想2 小时前
【C++】二叉搜索树
数据结构·c++
XKSYA(小巢校长)3 小时前
NatGo我的世界联机篇
开发语言·php
我是哈哈hh3 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
丶Darling.3 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5203 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code4 小时前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表
jiyisuifeng19914 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法