美文网首页
面试:扁平数据结构转Tree

面试:扁平数据结构转Tree

作者: 涅槃快乐是金 | 来源:发表于2022-01-29 22:35 被阅读0次

题目

扁平的数据结构,转成树。
打平的数据内容如下:

let arr = [
    {id: 1, name: '部门1', pid: 0},
    {id: 2, name: '部门2', pid: 1},
    {id: 3, name: '部门3', pid: 1},
    {id: 4, name: '部门4', pid: 3},
    {id: 5, name: '部门5', pid: 4},
]

输出结果

[
    {
        "id": 1,
        "name": "部门1",
        "pid": 0,
        "children": [
            {
                "id": 2,
                "name": "部门2",
                "pid": 1,
                "children": []
            },
            {
                "id": 3,
                "name": "部门3",
                "pid": 1,
                "children": [
                    // 结果 ,,,
                ]
            }
        ]
    }
]

递归遍历查找

主要思路是提供一个递getChildren的方法,该方法递归去查找子集。

/**
 * 递归查找,获取children
 */
const getChildren = (data, result, pid) => {
  for (const item of data) {
    if (item.pid === pid) {
      const newItem = {...item, children: []};
      result.push(newItem);
      getChildren(data, newItem.children, item.id);
    }
  }
}

/**
* 转换方法
*/
const arrayToTree = (data, pid) => {
  const result = [];
  getChildren(data, result, pid)
  return result;
}

时间复杂度为O(2^n)

map转换

主要思路是先把数据转成Map去存储,之后遍历的同时借助对象的引用,直接从Map找对应的数据做存储;

function arrayToTree(items) {
  const result = [];   // 存放结果集
  const itemMap = {};  // 
    
  // 先转成map存储
  for (const item of items) {
    itemMap[item.id] = {...item, children: []}
  }
  
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;
    const treeItem =  itemMap[id];
    if (pid === 0) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        }
      }
      itemMap[pid].children.push(treeItem)
    }

  }
  return result;
}

时间复杂度为O(2n),需要一个Map把数据存储起来,空间复杂度O(n)

再次优化

function arrayToTree(items) {
  const result = [];   // 存放结果集
  const itemMap = {};  // 
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;

    if (!itemMap[id]) {
      itemMap[id] = {
        children: [],
      }
    }

    itemMap[id] = {
      ...item,
      children: itemMap[id]['children']
    }

    const treeItem =  itemMap[id];

    if (pid === 0) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        }
      }
      itemMap[pid].children.push(treeItem)
    }

  }
  return result;
}

相关文章

  • 面试:扁平数据结构转Tree

    题目 扁平的数据结构,转成树。打平的数据内容如下: 输出结果 递归遍历查找 主要思路是提供一个递getChildr...

  • 扁平数据结构转Tree

    如上一个数据结构,将他转为树形结构。方法一: 解析:遍历两次原数组,并在第一次遍历的时候给数组的每一项添加一个ch...

  • 扁平数据结构转Tree

    在做后台系统和权限的时候经常遇到 扁平化数据转tree 结构 转化前 转化后 最优化性能 主要思路也是先把数据转成...

  • 扁平数据结构转Tree

    自己写的方法,使用递归方式 网上借鉴的方法(该实现的时间复杂度为O(2n)) 网上借鉴的方法(该实现的时间复杂度为...

  • js数组扁平数据结构转tree

    记录一下日常学习,js数组扁平数据结构转tree 演示数据 1.map存储 唯一性 2.递归 输出

  • 扁平数组转tree

    (function init() { let arr = [{ id: 1, ...

  • 扁平数据结构转Tree、递归等方法

    一维数组转成树结构,如下所示 1.不考虑性能实现,递归遍历查找 2.不用递归,也能搞定 3.最优性能 https:...

  • 算法:扁平数据结构转tree(JavaScript版)

    https://blog.csdn.net/qq_36647492/article/details/1213863...

  • 扁平数组转tree数组

    1、首先生成扁平数组 2、两种方法 1、递归写法,空间复杂度O(2^n) 2、对象方法,空间复杂度O(n)(一次遍...

  • Vue实战第8天

    可编辑表格 Tree组建实现文件目录 扁平化数据转层级数据 打了一晚上云顶之奕

网友评论

      本文标题:面试:扁平数据结构转Tree

      本文链接:https://www.haomeiwen.com/subject/usgfkrtx.html