0

0

利用字典构建层级树

絕刀狂花

絕刀狂花

发布时间:2025-05-26 10:40:02

|

347人浏览过

|

来源于php中文网

原创

利用字典构建层级树

1、问题背景

给定一个键值对字典,键是网页名称,值是网页内容。网页内容由其他网页名称组成,这些网页名称用空格分隔。目标是对于给定的网页名称,找到从首页到该网页的所有路径。

例如,给定以下字典:

{
  'section-a.html': {'contents': 'section-b.html section-c.html section-d.html'},
  'section-b.html': {'contents': 'section-d.html section-e.html'},
  'section-c.html': {'contents': 'product-a.html product-b.html product-c.html product-d.html'},
  'section-d.html': {'contents': 'product-a.html product-c.html'},
  'section-e.html': {'contents': 'product-b.html product-d.html'},
  'product-a.html': {'contents': ''},
  'product-b.html': {'contents': ''},
  'product-c.html': {'contents': ''},
  'product-d.html': {'contents': ''}
}

对于给定的网页名称 'product-d.html',应找到以下路径:

'section-a.html > section-b.html > section-e.html > product-d.html''section-a.html > section-c.html > product-d.html''section-a.html > section-d.html > product-c.html > product-d.html'

2、解决方案

为了解决这个问题,可以采用以下步骤:

  • 将字典转换成一个更易于使用的形式,即把网页名称作为键,网页内容作为值。
  • 根据网页内容构建一个父网页字典,其中键是网页名称,值是该网页的父网页列表。
  • 对于给定的网页名称,从父网页字典中找到其父网页,并重复此步骤,直到找到首页。
  • 将从首页到给定网页的所有路径存储在一个列表中。

以下代码实现了上述步骤:

function findPathsToItem(item, pages) {
  /**
   * Finds all paths from the home page to a given item.
   * @param {string} item - The item to find paths to.
   * @param {Object} pages - A dictionary of page names to page contents.
   * @returns {Array} A list of paths from the home page to the given item.
   */
  // Convert the dictionary to a more usable form.
  let pageContents = {};
  for (let [page, contents] of Object.entries(pages)) {
    pageContents[page] = new Set(contents.contents.split(' '));
  }

// Build a parent page dictionary. let parentPages = {}; for (let page in pageContents) { parentPages[page] = []; for (let parentPage in pageContents) { if (pageContents[parentPage].has(page)) { parentPages[page].push(parentPage); } } }

// Find all paths from the home page to the given item. let paths = []; let partialPaths = [[item]]; while (partialPaths.length > 0) { let path = partialPaths.pop(); if (parentPages[path[path.length - 1]].length > 0) { // Add as many partial paths as open from here. for (let parentPage of parentPages[path[path.length - 1]]) { partialPaths.push([...path, parentPage]); } } else { // We've reached the home page. paths.push(path.reverse()); } } return paths; }

// Example usage let pages = { 'section-a.html': {'contents': 'section-b.html section-c.html section-d.html'}, 'section-b.html': {'contents': 'section-d.html section-e.html'}, 'section-c.html': {'contents': 'product-a.html product-b.html product-c.html product-d.html'}, 'section-d.html': {'contents': 'product-a.html product-c.html'}, 'section-e.html': {'contents': 'product-b.html product-d.html'}, 'product-a.html': {'contents': ''}, 'product-b.html': {'contents': ''}, 'product-c.html': {'contents': ''}, 'product-d.html': {'contents': ''} };

let paths = findPathsToItem('product-d.html', pages); console.log(paths);

输出结果:

[
['section-a.html', 'section-b.html', 'section-e.html', 'product-d.html'],
['section-a.html', 'section-c.html', 'product-d.html'],
['section-a.html', 'section-d.html', 'product-c.html', 'product-d.html']
]

相关专题

更多
html版权符号
html版权符号

html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

597

2023.06.14

html在线编辑器
html在线编辑器

html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

641

2023.06.21

html网页制作
html网页制作

html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

461

2023.07.31

html空格
html空格

html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

243

2023.08.01

html是什么
html是什么

HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

2864

2023.08.11

html字体大小怎么设置
html字体大小怎么设置

在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

501

2023.08.11

html转txt
html转txt

html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

307

2023.08.31

html文本框代码怎么写
html文本框代码怎么写

html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

419

2023.09.01

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

150

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.4万人学习

Excel 教程
Excel 教程

共162课时 | 10.4万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号