用JS实现扁平数据结构转Tree,考虑最优性能
时间: 2023-03-27 10:04:53 浏览: 53
可以使用递归的方式实现扁平数据结构转Tree,具体实现可以参考以下代码:
```
function buildTree(data) {
const map = {};
const result = [];
data.forEach(item => {
map[item.id] = item;
item.children = [];
});
data.forEach(item => {
const parent = map[item.parentId];
if (parent) {
parent.children.push(item);
} else {
result.push(item);
}
});
return result;
}
```
这个实现方式的时间复杂度为 O(n),可以达到最优性能。
相关问题
用JS实现扁平数据结构转Tree
可以使用递归的方式实现扁平数据结构转Tree,具体实现可以参考以下代码:
function buildTree(data) {
let map = {};
let tree = [];
for (let i = ; i < data.length; i++) {
map[data[i].id] = data[i];
data[i].children = [];
}
for (let i = ; i < data.length; i++) {
let node = data[i];
if (node.parentId !== ) {
map[node.parentId].children.push(node);
} else {
tree.push(node);
}
}
return tree;
}
其中,data为扁平数据结构,每个节点包含id、parentId等属性,返回的tree为转换后的树形结构。
扁平化数据结构转成树形数据结构 使用递归方法
假设现有一个扁平化的数据结构,其中每个节点包含id、parentId和name三个属性,表示该节点的唯一标识、父节点的唯一标识和节点名称。现在需要将这个扁平化的数据结构转换成树形数据结构。
可以使用递归方法来实现。具体步骤如下:
1. 定义一个递归函数,参数为当前节点的id和整个扁平化数据结构。
2. 在递归函数中,遍历整个扁平化数据结构,查找所有父节点为当前节点id的节点,并将这些节点作为当前节点的子节点。
3. 对于每个子节点,递归调用该函数,参数为子节点的id和整个扁平化数据结构。
4. 递归函数返回当前节点及其所有子节点构成的树形数据结构。
代码实现如下:
```
function flattenToTree(id, data) {
var tree = {id: id, name: '', children: []};
for (var i = 0; i < data.length; i++) {
if (data[i].parentId == id) {
var child = {id: data[i].id, name: data[i].name, children: []};
tree.children.push(child);
var subChildren = flattenToTree(data[i].id, data);
if (subChildren.children.length > 0) {
child.children = subChildren.children;
}
}
}
return tree;
}
```
调用该函数的示例代码如下:
```
var data = [
{id: 1, parentId: 0, name: 'A'},
{id: 2, parentId: 1, name: 'B'},
{id: 3, parentId: 1, name: 'C'},
{id: 4, parentId: 2, name: 'D'},
{id: 5, parentId: 2, name: 'E'},
{id: 6, parentId: 3, name: 'F'}
];
var tree = flattenToTree(1, data);
console.log(tree);
```
输出结果为:
```
{
id: 1,
name: '',
children: [
{
id: 2,
name: 'B',
children: [
{id: 4, name: 'D', children: []},
{id: 5, name: 'E', children: []}
]
},
{
id: 3,
name: 'C',
children: [
{id: 6, name: 'F', children: []}
]
}
]
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)