我有一个通用的树类:myTree = new Tree(string), appendChildNode(node), createChildNode(string), myTree.print()个函数和myTree.name, myTree.children个等属性.

我还有一个多重映射,其中包含我想要分解并放入树对象中的数据.假设数据中没有圆圈.我的 idea 是让递归构建子分支.For some reason the recursion does not work and I cannot figure out why.在forEach内部返回给我一个"未定义的"

我的多重映射:

aaaa -> [b, c, d]
c    -> [f]
f    -> [g]
r    -> [p]

因此,我得出的"aaaa"树应该是:

         aaaa
   b      c    d
        f
      g

我的主要功能:

myMultiMap ... <multimap defined here>
myTree = new Tree('End2End')  // my tree object
// lets just pick the 'aaaa' to start with
myMultiMap.get('aaaa').forEach((entry) => {
  newNode = new Tree(entry)
  myTree.appendChildNode(recurse(newNode))

})
// show the tree after it was built
myTree.print()

递归函数

  function recurse (node) {
    // if the element is in the multimap, it has one or more children
    if(myMultiMap.has(node.name)) {
      // iterate through all children of the node in the multimap
      myMultiMap.get(node.name).forEach((child) => {
        // build a new node from the child
        newChildnode = new Tree(child);
        // build a subtree recursively, since this child could have children itself
        return node.appendChildNode(recurse(newChildnode))
      })
    
    // if the node is not in the multimap, thus it has no children, so just return the node   
    } else {
        return node
    }
  }

信息:我拿了这个树实现:https://github.com/beforesemicolon/tutorials-files/blob/master/tree-generic.js

推荐答案

forEach内部的返回给了我一个"未定义的"

是的,you cannot return out of a forEach callback.但这不是你想做的.相反,after执行循环时,您需要return node:

function recurse(node) {
  if (myMultiMap.has(node.name)) {
    myMultiMap.get(node.name).forEach((child) => {
      const newChildnode = new Tree(child);
      node.appendChildNode(recurse(newChildnode))
    })
    return node;
  } else {
    return node;
  }
}

或者只是

function recurse(node) {
  if (myMultiMap.has(node.name)) {
    myMultiMap.get(node.name).forEach((child) => {
      const newChildnode = new Tree(child);
      node.appendChildNode(recurse(newChildnode))
    })
  }
  return node;
}

或者,根本不返回 node ,只需写入即可

function recurse(node) {
  if (myMultiMap.has(node.name)) {
    myMultiMap.get(node.name).forEach((child) => {
      const newChildnode = new Tree(child);
      recurse(newChildnode);
      node.appendChildNode(newChildnode);
    })
  }
}

或者更好的做法是将 node 创建移到recurse函数中:

function recurse(name) {
  const node = new Tree(name);
  if (myMultiMap.has(name)) {
    myMultiMap.get(name).forEach((child) => {
      node.appendChildNode(recurse(child))
    })
  }
  return node;
}

顺便说一句,在主代码中,不需要复制循环.将其简化为

const myMultiMap = …; // multimap defined here
const myTree = recurse(new Tree('aaaa'));
myTree.print();

或者分别为我的上一个版本

const myMultiMap = …; // multimap defined here
const myTree = recurse('aaaa');
myTree.print();

Javascript相关问答推荐

我不知道为什么setwritten包装promise 不能像我预期的那样工作

WebRTC关闭navigator. getUserMedia正确

在Vite React库中添加子模块路径

如何解决useState错误—setSelect Image不是函数''

PDF工具包阿拉伯字体的反转数字

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

使用ThreeJ渲染的形状具有抖动/模糊的边缘

提交链接到AJAX数据结果的表单

如何在使用rhandsontable生成表时扩展数字输入验证?

映射类型定义,其中值对应于键

如何在Press上重新启动EXPO-AV视频?

警告框不显示包含HTML输入字段的总和

select 2-删除js插入的项目将其保留为选项

TypeORM QueryBuilder限制联接到一条记录

如何压缩图像并将其编码为文本?

在HTML中使用meta标记来指定定制元数据以用于使用JavaScript进行检索是不是一个坏主意?

如何在不将整个文件加载到内存的情况下,在Node.js中实现Unix粘贴命令?

从客户端更新MongoDB数据库

如何使用JavaScript将动态表上具有相同值的行与某些条件合并

从D3 v3更新,没有错误,但输出SVG路径不可见