
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'
即构数智人
即构数智人是由即构科技推出的AI虚拟数字人视频创作平台,支持数字人形象定制、短视频创作、数字人直播等。
36 查看详情
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 usagelet 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']]
以上就是利用字典构建层级树的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/498694.html
微信扫一扫
支付宝扫一扫