简体   繁体   English

通过将测试失败的树节点映射到其子节点来修剪树

[英]Pruning a tree by mapping nodes of tree that fail a test to its children

My problem here builds upon another problem I was trying to solve and received an excellent answer for where I have a tree:我在这里的问题建立在我试图解决的另一个问题上,并收到了关于我有一棵树的地方的很好的答案

const treeData = [{
  title: '0-0',
  key: '0-0',
  children: [{
    title: '0-0-0',
    key: '0-0-0',
    children: [
      { title: '0-0-0-0', key: '0-0-0-0', children: [] },
      { title: '0-0-0-1', key: '0-0-0-1', children: [] },
      { title: '0-0-0-2', key: '0-0-0-2', children: [] },
    ],
  }, {
    title: '0-0-1',
    key: '0-0-1',
    children: [
      { title: '0-0-1-0', key: '0-0-1-0', children: [] },
      { title: '0-0-1-1', key: '0-0-1-1', children: [] },
      { title: '0-0-1-2', key: '0-0-1-2', children: [] },
    ],
  }, {
    title: '0-0-2',
    key: '0-0-2',
    children: []
  }],
}, {
  title: '0-1',
  key: '0-1',
  children: [
    { title: '0-1-0-0', key: '0-1-0-0', children: [] },
    { title: '0-1-0-1', key: '0-1-0-1', children: [] },
    { title: '0-1-0-2', key: '0-1-0-2', children: [] },
  ],
}, {
  title: '0-2',
  key: '0-2',
  children: []
}];

and an array of leaf nodes:和叶节点数组:

const leafNodes = ['0-0-1-2', '0-1-0-1', '0-1-0-2']

Before, I just wanted a filtered/pruned copy of the tree that contains all the paths to the leaf nodes, but now I would like to further prune it by removing the parent node that doesn't satisfy a test -- the test being having all of its children included in the list of leaf nodes.以前,我只想要一个包含所有叶节点路径的树的过滤/修剪副本,但现在我想通过删除不满足测试的父节点来进一步修剪它——测试正在它的所有子节点都包含在叶节点列表中。 The resulting tree would look like this:生成的树将如下所示:

const pruned = [{
    title: '0-0-1-2',
    key: '0-0-1-2',
    children: []
  },
  {
    title: '0-1-0-1',
    key: '0-1-0-1',
    children: []
  }, {
    title: '0-1-0-2',
    key: '0-1-0-2',
    children: []
  }
]

Here, the node with keys 0-0-1 would be removed because only one of its 3 child nodes ( 0-0-1-2 ) is included in the leafNodes list and the child nodes included in the leaf nodes list (in this case, just the one) are bumped up to the level of their now removed parent.在这里,键为0-0-1的节点将被删除,因为它的 3 个子节点( 0-0-1-2 )中只有一个包含在叶节点列表中,而子节点包含在叶节点列表中(在此情况下,只有一个)被提升到他们现在删除的父级的级别。 This would flow back up to the parent of the removed node, now with key 0-0 , since not all of its children are included in the pruned tree.这将流回已删除节点的父节点,现在使用键0-0 ,因为并非所有子节点都包含在修剪树中。

This same pattern would apply to 0-1 .同样的模式也适用于0-1

You could iterate the array and check if the child are complete selected , then get the actual node or just some children, then take the children only.您可以迭代数组并检查子节点是否已完成选择,然后获取实际节点或仅获取一些子节点,然后仅获取子节点。

 function getShort(array, keys) { var result = [], every = true; array.forEach(o => { var children; if (keys.includes(o.key)) return result.push(o); if (!o.children || !o.children.length) return every = false; children = getShort(o.children, keys); if (children.length && children.length === o.children.length) return result.push(o); result.push(...children); every = false; }); return every ? array : result; } const treeData = [{ key: '0-0', children: [{ key: '0-0-0', children: [{ key: '0-0-0-0', children: [] }, { key: '0-0-0-1', children: [] }, { key: '0-0-0-2', children: [] }] }, { key: '0-0-1', children: [{ key: '0-0-1-0', children: [] }, { key: '0-0-1-1', children: [] }, { key: '0-0-1-2', children: [] }] }, { key: '0-0-2', children: [] }] }, { key: '0-1', children: [{ key: '0-1-0-0', children: [] }, { key: '0-1-0-1', children: [] }, { key: '0-1-0-2', children: [] }] }, { key: '0-2', children: [] }], leafNodes = [ '0-0-0-0', '0-0-0-1', '0-0-0-2', // all 0-0-0 all 0-0 '0-0-1-0', '0-0-1-1', '0-0-1-2', // all 0-0-1 all 0-0 '0-0-2', // all 0-0-2 all 0-0 '0-1-0-1', '0-1-0-2' ], short = getShort(treeData, leafNodes); console.log(short);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM