复杂度

作者: 湘兰沅芷 | 来源:发表于2022-09-24 14:35 被阅读0次

常数阶O(1)
对数阶O(log2 n)
线性阶O(n)
线性对数阶O(n log2 n)
平方阶O(n^2)
立方阶O(n^3)
k次方阶O(n^K)
指数阶O(2^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;
}

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},
]


arrayToTree(arr)

相关文章

  • 时间复杂度(下)

    时间复杂度知识点 最好时间复杂度 最坏时间复杂度 平均情况复杂度 均摊时间复杂度

  • 复杂度分析

    为什么需要复杂度分析? 大O复杂度表示法 时间复杂度分析 常见复杂度量级 复杂度量级简单说明 空间复杂度 时间复杂...

  • 复杂度分析笔记

    常见复杂度 :常数复杂度 :对数复杂度 :线性时间复杂度 :线性对数复杂度 :平方阶 :立方 :K次方阶 :指数阶...

  • 常用的排序算法

    插入排序 复杂度 思路 希尔排序 复杂度 思路 选择排序 复杂度 思路 归并排序 复杂度 思路 快速排序复杂度 思...

  • NLP初学之-算法复杂度

    算法的复杂度分为:时间复杂度和空间复杂度。

  • 算法基础知识

    算法的复杂度 算法的复杂度: 算法的时间复杂度和空间复杂度合称为算法的复杂度,一般不特别说明,讨论的时间复杂度均是...

  • 数据结构-0-时间复杂度和空间复杂度

    1. 算法的复杂度: 算法的复杂度分为时间复杂度和空间复杂度。时间复杂度,是衡量算法执行时间的长度;空间复杂度,是...

  • 时间复杂度和空间复杂度笔记

    复杂度分析笔记 复杂度主要分为时间和空间复杂度 时间复杂度:算法(程序)执行的时间变化趋势 空间复杂度:算法(程序...

  • 算法复杂度

    算法的复杂度是以什么来度量的? 算法的复杂度是以时间复杂度和空间复杂度来计算的。 ①算法的时间复杂度 ...

  • sort_algorithm

    排序方法 时间复杂度(平均) 时间复杂度(最坏) 时间复杂度(最好) 空间复杂度 稳定性 复...

网友评论

      本文标题:复杂度

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