日韩天堂,国产精品久久久久久久久久一区,羞羞羞网站,自拍视频网站,久久亚洲欧美成人精品,桃花阁成人网在线观看

Hello! 歡迎來到小浪云!


利用字典構建層級樹


avatar
小浪云 2025-05-26 16

利用字典構建層級樹 alt=”利用字典構建層級樹” />

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(' '));   } <p>// 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); } } }</p><p>// 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; }</p><p>// 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': ''} };</p><p>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'] ]

相關閱讀

主站蜘蛛池模板: 高清国产性色视频在线 | 自拍欧美日韩 | 亚洲天堂网在线观看视频 | 永久免费毛片在线播放 | 伊人婷婷色香五月综合缴缴情小蛇 | 六月丁香深爱六月综合激情 | 羞羞视频在线观看网站 | 欧美精品福利视频 | 欧美精品一区二区三区四区 | 欧美精品一区二区精品久久 | 伊人久久艹 | 国产成人高清亚洲一区久久 | 亚洲欧美另类精品久久久 | 亚洲人成高清在线播放 | 久久久久久国模大尺度人体 | 久久久国产精品视频 | 性欧美日韩 | 在线色网址 | 黄色污网站在线观看 | 小视频电影| 欧美一区2区 | 色综合五月激情综合色一区 | 五月天色婷婷在线 | 麻豆www| 一区二区三区四区在线播放 | 欧美日韩精品一区二区三区视频 | 日韩在线观看精品 | 国内精品久久久久久不卡影院 | 亚洲专区一 | 黑人一区二区 | 亚洲69 | 华人色| 亚洲欧美一区二区三区国产精品 | 亚洲一个色| 一本久久a久久精品vr综合 | 国产在线观看一区 | 日日操日日爽 | 日日综合网| 久久99精品久久久久久噜噜 | 开心色播站 | 久久免费视频精品 |