用字典构建层级树结构的技巧
时间:2025-05-26 09:55:52 493浏览 收藏
文章介绍了如何利用字典构建层级树结构来解决从首页到特定网页的所有路径查找问题。给定一个包含网页名称和内容的键值对字典,内容由其他网页名称组成。通过将字典转换为更易处理的形式,构建父网页字典,并递归查找路径,最终实现从首页到目标网页的所有路径查找。例如,对于网页 'product-d.html`,可以找到三条从首页 'section-a.html' 到该网页的不同路径。
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'] ]
今天关于《用字典构建层级树结构的技巧》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于递归,字典,层级树,路径查找,父网页的内容请关注golang学习网公众号!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
473 收藏
-
413 收藏
-
206 收藏
-
358 收藏
-
306 收藏
-
151 收藏
-
444 收藏
-
221 收藏
-
331 收藏
-
287 收藏
-
328 收藏
-
463 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习