转换亲子阵列树阵列、亲子

2023-09-11 00:07:44 作者:狂妄忌友

谁能帮助将父子对象的清单如下:

[
   {
      名:根,
      _id:root_id,
   },
   {
      名称:A1,
      parentAreaRef:{
         ID:root_id
      },
      _id:a1_id,
   },
   {
      名:A2,
      parentAreaRef:{
         ID:a1_id
      },
      _id:a2_id,
   },
   {
      名A3,
      parentAreaRef:{
         ID:a2_id
      },
      _id:a3_id,
   },
   {
      名:B1,
      parentAreaRef:{
         ID:root_id
      },
      _id:b1_id,
   },
   {
      名:B2,
      parentAreaRef:{
         ID:b1_id
      },
      _id:b2_id,
   },
   {
      名:B3,
      parentAreaRef:{
         ID:b1_id
      },
      _id:b3_id,
   }
] 

成树形结构显示父子关系:

[
    {
        名:根,
        _id:root_id,
        孩子:
            {
                名称:A1,
                _id:a1_id,
                孩子:
                    {
                        名:A2,
                        _id:a2_id,
                        孩子:
                            {
                                名A3
                                _id:a3_id
                            }
                        ]
                    }
                ]
            },
            {
                名:B1,
                _id:b1_id,
                孩子:
                    {
                        名:B2
                        _id:b2_id
                    },
                    {
                        名:B3
                        _id:b3_id
                    }
                ]
            }
        ]
    }
]

(输出结构是一个数组,允许多根,但如果我们可以得到一个处理一个根那是太伟大的解决方案。)

输出树是这个样子:


根
  |
   -  A1
  | |
  | -  a2
  | |
  | -  A3
  |
   -  B1
      |
       -  B2
       -  B3


DARPA 转换 ACT IV 多功能阵列以支持未来的测试

谢谢!

解决方案

我有一个解决方案,工程。我可以给你,只要解决它的提示。好处是,你的数据不包含任何向前引用节点。所以,你可以只用一次通过数组来创建你的树。

您的算法是这样的。

创建该ID的映射到节点的映射。这将可以很容易地查找节点。 在遍历节点的数组。 对于每一个元素。 添加一个条目的映射。 添加孩子属性(数组)到该节点。 是否元素有一个父?如果没有它必须是根,所以该元素分配给树的根。 在此元素有父母,所以找了父节点,然后添加此当前节点的父节点(将其添加到孩子阵列)的孩子

这应该可以帮助你解决问题。如果您在使用这种算法的具体问题,我可以指出问题所在和如何解决或发布解决方案,并解释我是如何解决它。

更新

我看着,你有解决方案。你其实并不为此需要递归,你可以反复使用我上面介绍的算法做到这一点。你也修改结构就地,这使得该算法更复杂。但你是有点在正确的轨道。这是我如何解决它:

  VAR idToNodeMap = {}; //跟踪记录使用id作为关键节点,快速查找
VAR根= NULL; //开始设置我们的循环空

//遍历数据
对于(VAR I = 0; I< data.length;我++){
    VAR数据=数据[I]

    //每个节点有孩子,所以让我们给它一个孩子poperty
    datum.children = [];

    //此节点条目添加到地图,以便将来可儿
    //查找父
    idToNodeMap [datum._id] =基准;

    //这是否节点有一个父?
    如果(typeof运算datum.parentAreaRef ===未定义){
        //看起来不像它,所以这个节点是树的根
        根=基准;
    } 其他 {
        //这个节点有一个家长,让我们使用id看它
        parentNode = idToNodeMap [datum.parentAreaRef.id]

        //我们不需要这个属性,所以我们将其删除。
        删除datum.parentAreaRef;

        //让我们添加当前节点的父节点的子节点。
        parentNode.children.push(数据);
    }
}
 

现在指向整个树。

小提琴。

Can anyone help converting the following list of parent-child objects:

[
   {
      "name":"root",
      "_id":"root_id",
   },
   {
      "name":"a1",
      "parentAreaRef":{
         "id":"root_id",
      },
      "_id":"a1_id",
   },
   {
      "name":"a2",
      "parentAreaRef":{
         "id":"a1_id",
      },
      "_id":"a2_id",
   },
   {
      "name":"a3",
      "parentAreaRef":{
         "id":"a2_id",
      },
      "_id":"a3_id",
   },
   {
      "name":"b1",
      "parentAreaRef":{
         "id":"root_id",
      },
      "_id":"b1_id",
   },
   {
      "name":"b2",
      "parentAreaRef":{
         "id":"b1_id",
      },
      "_id":"b2_id",
   },
   {
      "name":"b3",
      "parentAreaRef":{
         "id":"b1_id",
      },
      "_id":"b3_id",
   }
]

into a tree structure showing the parent-child relationship:

[
    {
        "name": "root",
        "_id":"root_id",
        "children": [
            {
                "name": "a1",
                "_id":"a1_id",
                "children" : [
                    {
                        "name" : "a2",
                        "_id":"a2_id",
                        "children" : [
                            {
                                "name" : "a3"
                                "_id":"a3_id"
                            }
                        ]
                    }
                ]
            }, 
            {
                "name": "b1",
                "_id":"b1_id",
                "children" : [
                    {
                        "name" : "b2"
                        "_id":"b2_id"
                    },
                    {
                        "name" : "b3"
                        "_id":"b3_id"
                    }
                ]
            }
        ]
    }
]

(The output structure is an array to allow for multiple roots but if we can get a solution that handles a single root that's great too.)

The output tree looks like this:


root
  |
  -- a1
  |   |
  |   -- a2
  |       |
  |       -- a3
  | 
  -- b1
      |
      -- b2
      -- b3


Thanks!

解决方案

I have a solution that works. I can give you hints as far as solving it. The good thing is that your data doesn't contain any forward references to nodes. So you can create your tree with just one pass through the array.

Your algorithm will look like this.

Create a map that maps id's to nodes. This will make it easy to look up nodes. Loop through the array of nodes. For each element.

Add an entry into the map. Add a children property (an array) to this node. Does the element have a parent? If not it must be the root, so assign the this element to the root of the tree. This element has a parent, so look up the parent node, and then add this current node as a child of the parent node (add it to the children array).

This should help you solve the problem. If you're having specific issues with this algorithm I can point out where the problems are and how to solve it or post the solution and explain how I solved it.

UPDATE

I looked at the solution that you have. You actually don't need recursion for this and you can do this iteratively using the algorithm I described above. You are also modifying the structure in-place, which makes the algorithm more complicated. But you're somewhat on the right track. Here is how I solved it:

var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
var root = null; //Initially set our loop to null

//loop over data
for(var i = 0; i < data.length; i++) {
    var datum = data[i];

    //each node will have children, so let's give it a "children" poperty
    datum.children = [];

    //add an entry for this node to the map so that any future children can
    //lookup the parent
    idToNodeMap[datum._id] = datum;

    //Does this node have a parent?
    if(typeof datum.parentAreaRef === "undefined") {
        //Doesn't look like it, so this node is the root of the tree
        root = datum;        
    } else {        
        //This node has a parent, so let's look it up using the id
        parentNode = idToNodeMap[datum.parentAreaRef.id];

        //We don't need this property, so let's delete it.
        delete datum.parentAreaRef;

        //Let's add the current node as a child of the parent node.
        parentNode.children.push(datum);        
    }
}

Now root points to the entire tree.

Fiddle.