当前位置: 首页 > news >正文

树相关处理

树的结构

每层都有val和children

let tree = {val: 1,children: [{val: 2,children: [{val: 4, children:[]},{val: 5, children: []}]},{val: 3,children: [{val: 6, children:[]},{val: 7, children:[]},]}]}

树的遍历

  • 深度优先遍历(递归)
    const fun = (root) => {console.log(root.val)root.forEach(fun)}
  • 广度优先遍历(数组模拟队列的使用,右进左出)
1、新建队列,根节点入队
2、把对头出队
3、把对头的children挨个入队
4、重复2、3步,直到队列为空为止
const fun = (root) => {let arr = [root]while(arr.length){const o = arr.shift()o.children.forEach(item => {console.log(o.val)arr.push(item)})}}

将数组转化成树

前提:每项中有id和pId,且有对应关系,根节点项id为null

const bidsList = [{parent: null, id: 1, text: '111'},{parent: null, id: 2, text: '222'},{parent: 1, id: 3, text: '1-1'},{parent: 1, id: 4, text: '1-2'},{parent: 2, id: 4, text: '2-1'},
]
function getTreeList (rootList, id, list) {for (let item of rootList) {if (item.parent === id ) {list.push(item)}}for (let i of list) {const flagItem = bidsList.find(si => {return si.parentId === i.id})if(flagItem) {i.children = []getTreeList(rootList, i.id, i.children)}}return list
}const treeData = getTreeList(bidsList, null, [])
console.log('treeData', treeData)

二叉树的结构

let binaryTree = {val: 1,left: {val: 2,left: {val: 4, left: null, right: null},right: {val: 5, left: null, right: null}},right: {val: 3,left: {val: 6, left: null, right: null},right: {val: 7, left: null, right: null}}}

二叉树的遍历

  • 前序遍历(根左右1、2、4、5、3、6、7)
// 递归方式实现
let arr = []const fun = (node) => {if(node){arr.push(node.val)fun(node.left)fun(node.right)}}fun(binaryTree) console.log(arr)
//栈加循环方式实现
if(!root){return []}let arr = []let stack = [root]while(stack.length){let o = stack.pop()arr.push(o.val)o.right && arr.push(o.right)o.left && arr.push(o.left)}console.log(arr)
  • 中序遍历(左根右4、2、5、1、6、3、7)
// 递归实现
let arr = []
const fn = (tree) => {if(!tree){return}fn(tree.left)arr.push(tree.val)fn(tree.right)
}
fn(binaryTree)
console.log(arr)// 栈加循环实现
let arr = []
let stack = []
let o = binaryTree
while(stack.length || o){while(o){stack.push(o)o = o.left}const n = stack.pop()arr.push(n.val)o = n.right
}
console.log(arr)
  • 后序遍历(左右根4、5、2、6、7、3、1)
// 递归实现
let arr = []const fn = (node) => {if(node) {fn(node.left)fn(node.right)arr.push(node.val)}}fn(binaryTree)console.log(arr)// 栈加循环实现
let arr = []let stack = []while(stack.length){const o = stack.pop()arr.unshift(o)o.left && stack.push(o.left)o.right && stack.push(o.right)}console.log(arr)

相关文章:

  • 结合五层网络结构讲一下用户在浏览器输入一个网址并按下回车后到底发生了什么?
  • Eclipse 插件开发 1
  • 面试新收获-大模型学习
  • Python编程中的基本语句
  • 长短板理论——AI与思维模型【83】
  • 【C++11】右值引用和移动语义:万字总结
  • Docker Compose--在Ubuntu中安装Docker compose
  • 嵌入式C设计模式---策略模式
  • unity bug
  • SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
  • JAVA服务内存缓慢上涨,年轻代GC正常但Full GC频繁,如何定位?
  • [ACTF2020 新生赛]BackupFile题解
  • 用Podman Desktop创建自用的WSL-Fedora Linux子系统
  • LeetCode100题
  • linux blueZ 第五篇:高阶优化与性能调优——蓝牙吞吐、延迟与功耗全攻略
  • 编译语言、半编译语言(混合型)和非编译语言(解释型)的差异
  • ROS 快速入门教程05
  • Ardunio学习
  • 高中数学联赛模拟试题精选第16套几何题
  • 子网掩码的学习
  • 孟泽:我们简化了历史,因此也简化了人性
  • 新闻1+1丨应对外部冲击,中央政治局会议释放哪些信号?
  • 神二十成功对接空间站
  • 广东东莞调整普通住宅价格标准:一类镇街上浮300余元/平方米
  • 陕西全省公开征集涉企行政执法问题线索,切实减轻企业负担
  • 远香湖畔“戏”味浓,“吾嘉有戏”探索戏剧与图书跨界融合