BlessingCR’s Blog
BlessingCR’s Blog

目录树搜索文章反向构建目录

目录树搜索文章反向构建目录

需求背景: A平台带了某类文章管理功能,目录下带各类文章。要求搜索文章时前端显示具体目录树中每个节点中符合搜索要求的的节点,并显示该节点下有多少符合要求的文章(若A节点下A1节点有3篇,A2节点有5篇,则呈现目录树应该为A(8, A1(3), A2(5)))。

注意,若节点中只有某叶子节点带了信息,应该同时把从根节点到叶子节点的所有信息全量显示。如:

A(1)-B(1)-C(1)-D(1)-E(1)-F(1)

由于前期目录设计仅带id,pid, 没有维护路径信息,层级等内容,且不便于更改(目录要求可拖拽,所以不便限制层级,且目录可修改,可改变父节点,故不便维护路径)

实现思路如下:

1. redis维护全量目录树,方便后续步骤(可优化为redis维护版本,目录树维护在内存中,如果版本不对则更新内存中目录树)
2. redis维护每个节点的关键路径信息,如A-A1-A2的树,则维护map("A":[A,A1,A2], "A1":[A,A1], "A2":[A,A1,A2])
3. 数据库搜索到关键节点(从文章,文章目录表中,联查,得到所需节点,此时还不知关键路径)
4. 通过步骤2中关键信息,存储所有关键路径(set存,顺便去重,得到所有关键路径节点)
5. 通过步骤1中全量目录树,递归删除所有非关键路径节点数据,得到所需关键路径树
6. 通过步骤5中关键路径树,以及步骤3中所需节点中搜索出的每个节点中文章数目,递归构建关键树中每个节点带有文章数量信息

实践中,若所有步骤目录树均为同一种目录树,则步骤5,6可合为一步

没有标签
首页      Java基础      数据结构           目录树搜索文章反向构建目录

发表回复

textsms
account_circle
email

  • Good website! I really love how it is easy on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made. I’ve subscribed to your RSS which must do the trick! Have a great day!

    1 月前 回复
  • Very good visual appeal on this internet site, I’d value it 10 10.

    3 周前 回复
  • I simply wanted to compose a brief remark to say thanks to you for all of the fantastic guides you are writing at this website. My considerable internet look up has at the end been rewarded with professional know-how to write about with my colleagues. I ‘d believe that we site visitors are unequivocally lucky to live in a fabulous community with so many outstanding professionals with very beneficial concepts. I feel truly fortunate to have seen your entire web pages and look forward to really more entertaining times reading here. Thank you once again for a lot of things.

    21 小时前 回复

BlessingCR’s Blog

目录树搜索文章反向构建目录
目录树搜索文章反向构建目录 需求背景: A平台带了某类文章管理功能,目录下带各类文章。要求搜索文章时前端显示具体目录树中每个节点中符合搜索要求的的节点,并显示该节点下有多少符合…
扫描二维码继续阅读
2024-01-23