构造无限级树的框架套路,附上python/golang/php/js实现
前言
框架思维非常重要,和语言无关,这是一种非常重要的抽象能力,吹得厉害一点,就是要有高屋建瓴,统筹全局的能力。
树型结构的创建也是有套路的,下面由伟大的诗人chenqionghe给出套路框架,再分别套上相应的编程语言实现。
最终达到看到框架就能实现对应编程语言的代码,或者也可以直接copy实现拿去用~
迭代实现参考了这篇文章的思路列表list转树形结构:(python/golang/js/php),童鞋们可以看看这篇文章
需求
给定一级列表,构造出指定父ID的无限级树型结果
数据
[
{'id': 1, 'parent_id': 0, 'name': "A"},
{'id': 2, 'parent_id': 1, 'name': "AA"},
{'id': 3, 'parent_id': 1, 'name': "AB"},
{'id': 4, 'parent_id': 3, 'name': "ABA"},
{'id': 5, 'parent_id': 3, 'name': "ABB"},
{'id': 6, 'parent_id': 3, 'name': "ABC"},
{'id': 7, 'parent_id': 1, 'name': "AC"},
{'id': 8, 'parent_id': 7, 'name': "ACA"},
{'id': 9, 'parent_id': 8, 'name': "ACAA"},
{'id': 10, 'parent_id': 8, 'name': "ACAB"},
]
结果
最后构造成一颗根据父ID连接起来的树结构
{
"id": 1,
"parent_id": 0,
"name": "A",
"children": [
{
"id": 2,
"parent_id": 1,
"name": "AA",
"children": []
},
{
"id": 3,
"parent_id": 1,
"name": "AB",
"children": [
{
"id": 4,
"parent_id": 3,
"name": "ABA",
"children": []
},
{
"id": 5,
"parent_id": 3,
"name": "ABB",
"children": []
},
{
"id": 6,
"parent_id": 3,
"name": "ABC",
"children": []
}
]
},
{
"id": 7,
"parent_id": 1,
"name": "AC",
"children": [
{
"id": 8,
"parent_id": 7,
"name": "ACA",
"children": [
{
"id": 9,
"parent_id": 8,
"name": "ACAA",
"children": []
},
{
"id": 10,
"parent_id": 8,
"name": "ACAB",
"children": []
}
]
}
]
}
]
}
框架
递归框架
递归是一门艺术,用接近人类的语言来表达了程序,优点是代码较少,缺点是性能较差。
获取树(列表,父ID)
res = []
for 节点 in 列表:
if 节点的parent_id 等于 父ID
节点.children = 获取树(列表, 节点ID)
res.add(节点)
return res
迭代框架
迭代本身是创建一条引用链,将所有的节点串起来
获取树(列表,父ID)
memo = {}
for 节点 in 列表:
//构造memo给节点的父ID查找追加节点用
if 节点ID in memo:
节点.children = memo[节点ID].children //之前构造的children数组覆盖当前节点的children
memo[节点ID] = 节点
else
节点.children = []
memo[节点ID] = 节点
//给像父对象的children追加
if 节点父ID in memo:
memo[节点父ID].children.add(memo[节点ID]) //追加当前构造的ID节点
else:
memo[节点父ID] = {'children':[memo[节点ID]]} //初始化父对象再追加
return memo[父ID].children
递归框架实现
python
def get_tree_iterative(list_data, parent_id=0):
memo = {}
for v in list_data:
item_id = v['id']
item_paren_id = v['parent_id']
if item_id in memo:
v['children'] = memo[item_id]['children']
memo[item_id] = v
else:
v['children'] = []
memo[item_id] = v
if item_paren_id in memo:
memo[item_paren_id]['children'].append(memo[item_id])
else:
memo[item_paren_id] = {'children': memo[item_id]}
return memo[parent_id]['children']
list_data = [
{'id': 1, 'parent_id': 0, 'name': "A"},
{'id': 2, 'parent_id': 1, 'name': "AA"},
{'id': 3, 'parent_id': 1, 'name': "AB"},
{'id': 4, 'parent_id': 3, 'name': "ABA"},
{'id': 5, 'parent_id': 3, 'name': "ABB"},
{'id': 6, 'parent_id': 3, 'name': "ABC"},
{'id': 7, 'parent_id': 1, 'name': "AC"},
{'id': 8, 'parent_id': 7, 'name': "ACA"},
{'id': 9, 'parent_id': 8, 'name': "ACAA"},
{'id': 10, 'parent_id': 8, 'name': "ACAB"},
]
res = get_tree_iterative(list_data)
import json
print(json.dumps(res, indent=4))
运行如下
golang
package main
import (
"encoding/json"
"fmt"
)
type Node struct {
Id int `json:"id"`
ParentId int `json:"parent_id"`
Name string `json:"name"`
Children []*Node `json:"children"`
}
func getTreeRecursive(list []*Node, parentId int) []*Node {
res := make([]*Node, 0)
for _, v := range list {
if v.ParentId == parentId {
v.Children = getTreeRecursive(list, v.Id)
res = append(res, v)
}
}
return res
}
func main() {
list := []*Node{
{4, 3, "ABA", nil},
{3, 1, "AB", nil},
{1, 0, "A", nil},
{2, 1, "AA", nil},
{5, 3, "ABB", nil},
{6, 3, "ABC", nil},
{7, 1, "AC", nil},
{8, 7, "ACA", nil},
{9, 8, "ACAA", nil},
{10, 8, "ACAB", nil},
}
res := getTreeRecursive(list, 0)
bytes, _ := json.MarshalIndent(res, "", " ")
fmt.Printf("%s\n", bytes)
}
运行如下
php
function getTreeRecursive(&$list, $parentId = 0)
{
$res = [];
foreach ($list as $k => $v) {
if ($v['parent_id'] == $parentId) {
$v['children'] = getTreeRecursive($list, $v['id']);
$res[] = $v;
}
}
return $res;
}
$list = [
['id' => 4, 'parent_id' => 3, 'name' => "ABA"],
['id' => 3, 'parent_id' => 1, 'name' => "AB"],
['id' => 1, 'parent_id' => 0, 'name' => "A"],
['id' => 2, 'parent_id' => 1, 'name' => "AA"],
['id' => 5, 'parent_id' => 3, 'name' => "ABB"],
['id' => 6, 'parent_id' => 3, 'name' => "ABC"],
['id' => 7, 'parent_id' => 1, 'name' => "AC"],
['id' => 8, 'parent_id' => 7, 'name' => "ACA"],
['id' => 9, 'parent_id' => 8, 'name' => "ACAA"],
['id' => 10, 'parent_id' => 8, 'name' => "ACAB"],
];
$res = getTreeRecursive($list);
echo json_encode($res,JSON_PRETTY_PRINT);
运行结果如下
js
function getTreeRecursive(listData, parentId = 0) {
let res = []
listData.forEach((v, k) => {
if (v.parent_id == parentId) {
v.children = getTreeRecursive(listData, v.id)
res.push(v)
}
})
return res
}
dataList = [
{'id': 1, 'parent_id': 0, 'name': "A"},
{'id': 2, 'parent_id': 1, 'name': "AA"},
{'id': 3, 'parent_id': 1, 'name': "AB"},
{'id': 4, 'parent_id': 3, 'name': "ABA"},
{'id': 5, 'parent_id': 3, 'name': "ABB"},
{'id': 6, 'parent_id': 3, 'name': "ABC"},
{'id': 7, 'parent_id': 1, 'name': "AC"},
{'id': 8, 'parent_id': 7, 'name': "ACA"},
{'id': 9, 'parent_id': 8, 'name': "ACAA"},
{'id': 10, 'parent_id': 8, 'name': "ACAB"},
]
let res = getTreeRecursive(dataList);
console.log((JSON.stringify(res, null, 4)))
运行如下
迭代框架实现
python
def get_tree_iterative(list_data, parent_id=0):
memo = {}
for v in list_data:
id_ = v['id']
item_paren_id = v['parent_id']
if id_ in memo:
v['children'] = memo[id_]['children']
memo[id_] = v
else:
v['children'] = []
memo[id_] = v
if item_paren_id in memo:
memo[item_paren_id]['children'].append(memo[id_])
else:
memo[item_paren_id] = {'children': memo[id_]}
return memo[parent_id]['children']
list_data = [
{'id': 1, 'parent_id': 0, 'name': "A"},
{'id': 2, 'parent_id': 1, 'name': "AA"},
{'id': 3, 'parent_id': 1, 'name': "AB"},
{'id': 4, 'parent_id': 3, 'name': "ABA"},
{'id': 5, 'parent_id': 3, 'name': "ABB"},
{'id': 6, 'parent_id': 3, 'name': "ABC"},
{'id': 7, 'parent_id': 1, 'name': "AC"},
{'id': 8, 'parent_id': 7, 'name': "ACA"},
{'id': 9, 'parent_id': 8, 'name': "ACAA"},
{'id': 10, 'parent_id': 8, 'name': "ACAB"},
]
res = get_tree_iterative(list_data)
import json
print(json.dumps(res, indent=4))
运行如下
golang
package main
import (
"encoding/json"
"fmt"
)
type Node struct {
Id int `json:"id"`
ParentId int `json:"parent_id"`
Name string `json:"name"`
Children []*Node `json:"children"`
}
func getTreeIterative(list []*Node, parentId int) []*Node {
memo := make(map[int]*Node)
for _, v := range list {
if _, ok := memo[v.Id]; ok {
v.Children = memo[v.Id].Children
memo[v.Id] = v
} else {
v.Children = make([]*Node, 0)
memo[v.Id] = v
}
if _, ok := memo[v.ParentId]; ok {
memo[v.ParentId].Children = append(memo[v.ParentId].Children, memo[v.Id])
} else {
memo[v.ParentId] = &Node{Children: []*Node{memo[v.Id]}}
}
}
return memo[parentId].Children
}
func main() {
list := []*Node{
{4, 3, "ABA", nil},
{3, 1, "AB", nil},
{1, 0, "A", nil},
{2, 1, "AA", nil},
{5, 3, "ABB", nil},
{6, 3, "ABC", nil},
{7, 1, "AC", nil},
{8, 7, "ACA", nil},
{9, 8, "ACAA", nil},
{10, 8, "ACAB", nil},
}
res := getTreeIterative(list, 0)
bytes, _ := json.MarshalIndent(res, "", " ")
fmt.Printf("%s\n", bytes)
}
运行如下
php
function getTreeIterative($list, $parentId = 0)
{
$memo = [];
foreach ($list as &$v) {
$id = $v['id'];
$itemParentId = $v['parent_id'];
if (isset($memo[$id])) {
$v['children'] = &$memo[$id]['children'];
$memo[$id] = $v;
} else {
$v['children'] = [];
$memo[$id] = $v;
}
if (isset($memo[$itemParentId])) {
$memo[$itemParentId]['children'][] = &$memo[$id];
} else {
$memo[$itemParentId] = ['children' => [&$memo[$id]]];
}
}
return $memo[$parentId]['children'];
}
$list = [
['id' => 4, 'parent_id' => 3, 'name' => "ABA"],
['id' => 3, 'parent_id' => 1, 'name' => "AB"],
['id' => 1, 'parent_id' => 0, 'name' => "A"],
['id' => 2, 'parent_id' => 1, 'name' => "AA"],
['id' => 5, 'parent_id' => 3, 'name' => "ABB"],
['id' => 6, 'parent_id' => 3, 'name' => "ABC"],
['id' => 7, 'parent_id' => 1, 'name' => "AC"],
['id' => 8, 'parent_id' => 7, 'name' => "ACA"],
['id' => 9, 'parent_id' => 8, 'name' => "ACAA"],
['id' => 10, 'parent_id' => 8, 'name' => "ACAB"],
];
$res = getTreeIterative($list);
echo json_encode($res, JSON_PRETTY_PRINT);
运行结果如下
js
function getTreeIterative(listData, parentId = 0) {
let memo = {};
listData.forEach((v, k) => {
let id = v.id
let itemParentId = v.parent_id
if (memo[id]) {
v.children = memo[id].children
memo[id] = v
} else {
v.children = []
memo[id] = v;
}
if (memo[itemParentId]) {
memo[itemParentId].children.push(memo[id]);
} else {
memo[itemParentId] = {children: [memo[id]]};
}
})
return memo[parentId].children
}
dataList = [
{'id': 1, 'parent_id': 0, 'name': "A"},
{'id': 2, 'parent_id': 1, 'name': "AA"},
{'id': 3, 'parent_id': 1, 'name': "AB"},
{'id': 4, 'parent_id': 3, 'name': "ABA"},
{'id': 5, 'parent_id': 3, 'name': "ABB"},
{'id': 6, 'parent_id': 3, 'name': "ABC"},
{'id': 7, 'parent_id': 1, 'name': "AC"},
{'id': 8, 'parent_id': 7, 'name': "ACA"},
{'id': 9, 'parent_id': 8, 'name': "ACAA"},
{'id': 10, 'parent_id': 8, 'name': "ACAB"},
]
let res = getTreeIterative(dataList);
console.log((JSON.stringify(res, null, 4)))
运行如下
以后如果想用直接来拷贝代码就行,就是这么简单~
构造无限级树的框架套路,附上python/golang/php/js实现的更多相关文章
- silverlight中递归构造无限级树treeview+checkbox
两个实体,其实一个实体也能构造出来,我这里是为了增加一个 checkbox //第一个实体 public class person { public int no { get; set; } publ ...
- 轻量级表达式树解析框架Faller
有话说 之前我写了3篇关于表达式树解析的文章 干货!表达式树解析"框架"(1) 干货!表达式树解析"框架"(2) 干货!表达式树解析"框架" ...
- 干货!表达式树解析"框架"(1)
最新设计请移步 轻量级表达式树解析框架Faller http://www.cnblogs.com/blqw/p/Faller.html 关于我和表达式树 其实我也没有深入了解表达式树一些内在实现的原理 ...
- 干货!表达式树解析"框架"(2)
最新设计请移步 轻量级表达式树解析框架Faller http://www.cnblogs.com/blqw/p/Faller.html 为了过个好年,我还是赶快把这篇完成了吧 声明 本文内容需要有一定 ...
- 表达式树解析"框架"
干货!表达式树解析"框架"(2) 为了过个好年,我还是赶快把这篇完成了吧 声明 本文内容需要有一定基础的开发人员才可轻松阅读,如果有难以理解的地方可以跟帖询问,但我也不一定能回 ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
- 通过有序线性结构构造AVL树
通过有序线性结构构造AVL树 本博客旨在结局利用有序数组和有序链表构造平衡二叉树(下文使用AVL树代指)问题. 直接通过旋转来构造AVL树似乎是一个不错的选择,但是稍加分析就会发现,这样平白无故做了许 ...
- 某互联网后台自动化组合测试框架RF+Sikuli+Python脚本
某互联网后台自动化组合测试框架RF+Sikuli+Python脚本 http://www.jianshu.com/p/b3e204c8651a 字数949 阅读323 评论1 喜欢0 一.**Robo ...
- 干货!表达式树解析"框架"(3)
最新设计请移步 轻量级表达式树解析框架Faller http://www.cnblogs.com/blqw/p/Faller.html 这应该是年前最后一篇了,接下来的时间就要陪陪老婆孩子了 关于表达 ...
随机推荐
- 洛谷P1089 津津的储蓄计划
题目描述 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里 ...
- Linux文件共享服务之Vsftp
目录 FTP Vsftp服务的搭建 ftp.sftp.vsftp.vsftpd的区别 FTP FTP的工作原理: FTP会话时包含了两个通道,一个叫控制通道,一个叫数据通道.控制通道是和FTP服务器进 ...
- HBASE-使用问题-split region
问题描述: HBASE表的管理以REGION分区为核心,通常面临如下几个问题: 1) 数据如何存储到指定的region分区,即rowkey设计,region splitkey设计 2)设计的split ...
- tp5.1中返回当天、昨天、当月等的开始和结束时间戳
/** * 返回今日开始和结束的时间戳 * * @return array */function today(){ list($y, $m, $d) = explode('-', date('Y-m- ...
- 解决无法远程登录Docker中的MySQL
步骤: 进入mysql容器 登录mysql 授予远程登录权限 刷新权限,并退出 命令参考: docker exec -it [mysql] /bin/bash mysql -u root -p ALT ...
- Cmder右键配置
Cmder右键配置 每次用camder手动一层一层的进入目标文件夹,是一件很麻烦的事儿.所以,将camder添加到系统右键菜单是个很好地解决方法. 1.把 Cmder 加到环境变量 把Cmder.ex ...
- 利用cm压缩包手动安装cm和cdh
安装准备: 1.操作系统为centos6.9 CentOS-6.9-x86_64-bin-DVD1to2 2.安装Oracle JDK (1.8u121) 下载jdk-8u121-linux-x64. ...
- 联想ThinkServer服务器安装CentOS7 Redhat7系统 驱动R110i RAID卡
1.下载对应版本的驱动(因为联想没有CentOS的驱动用redhat的驱动就可以). 2.进入BIOS里,在高级设置里找到SATA设置,把SATA模式改成RAID(重启后配置raid),sSATA模式 ...
- [转载]层叠与并排win10
三. 层叠与并排 如果要排列的窗口超过4个,分屏就显得有些不够用了,这时不妨试一试最传统的窗口排列法.具体方法是,右击任务栏空白处,然后选择"层叠窗口"."并排显示窗口& ...
- LNMP/LAMP
LNMP/LAMP 环境: 名称 Linux Nginx MySQL PHP Apache 版本 Centos7 nginx-1.14.1 mysql-5.6.25 php-5.6.36 Apache ...