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

数据结构——树

深度优先/广度优先遍历

深度优先:

  1. 访问根节点

  1. 对根节点的 children 挨个进行深度优先遍历

const tree = {val: "a",children: [{val: "b",children: [{val: "d",children: [],},{val: "e",children: [],},],},{val: "c",children: [{val: "f",children: [],},{val: "g",children: [],},],},],
};const dfs = (root) => {console.log(root.val);root.children.forEach((child) => {dfs(child);});
};dfs(tree);

广度优先:

  1. 新建立一个队列,根节点入队

  1. 对头出队并访问

  1. 把对头的children挨个入队

  1. 重复2,3,直到队列为空

const tree = {val: "a",children: [{val: "b",children: [{val: "d",children: [],},{val: "e",children: [],},],},{val: "c",children: [{val: "f",children: [],},{val: "g",children: [],},],},],
};const bfs = (root) => {const q = [root];while (q.length > 0) {const n = q.shift();console.log(n.val);n.children.forEach((child) => {q.push(child);});}
};bfs(tree);

二叉树的先中后序遍历

二叉树:每个节点最多只能有两个节点

先序遍历(根、左、右):

  1. 访问根节点

  1. 对根节点的左子树进行先序遍历

  1. 对根节点的右子树进行先序遍历

1、2、3、4、5、6、7

const tree = {val: "1",left: {val: "2",left: {val: "3",left: null,right: null,},right: {val: "4",left: {val: "5",},right: null,},},right: {val: "6",left: null,right: {val: "7",right: null,left: null,},},
};const preorder = (root) => {if (!root) return;console.log(root.val);preorder(root.left);preorder(root.right);
};
preorder(tree);

中序遍历(左、中、右):

  1. 对根节点左子树遍历

  1. 访问根节点

  1. 对根节点右子树遍历

1、2、3、4、5、6、7

const tree = {val: "5",left: {val: "2",left: {val: "1",left: null,right: null,},right: {val: "4",left: {val: "3",},right: null,},},right: {val: "6",left: null,right: {val: "7",right: null,left: null,},},
};const inorder = (root) => {if (!root) return;inorder(root.left);console.log(root.val);inorder(root.right);
};
inorder(tree);

后序遍历(左、右、根):

  1. 对根节点左子树遍历

  1. 对根节点右子树遍历

  1. 访问根节点

1、2、3、4、5、6、7

const tree = {val: "7",left: {val: "4",left: {val: "1",left: null,right: null,},right: {val: "3",left: {val: "2",},right: null,},},right: {val: "6",left: null,right: {val: "5",right: null,left: null,},},
};const postorder = (root) => {if (!root) return;postorder(root.left);postorder(root.right);console.log(root.val);
};
postorder(tree);

非递归写法

递归调用函数,会不断的入栈出栈,所以考虑用栈实现。

先序遍历:

// 递归
const preorder = (root) => {if (!root) return;console.log(root.val);preorder(root.left);preorder(root.right);
};// 非递归
const preorder = (root) => {if (!root) return;const stack = [root]while(stack.length){const n = stack.pop()console.log(n.val)// 栈后进先出,先右后左if(n.right) stack.push(n.right)if(n.left) stack.push(n.left)}
}

中序遍历:

// 递归
const inorder = (root) => {if (!root) return;inorder(root.left);console.log(root.val);inorder(root.right);
};
// 非递归
const inorder = (root) => {if (!root) return;const stack = [];let p = root;while (stack.length || p) {// 所有的左子树进栈while (p) {stack.push(p);p = p.left;}//最尽头的左子树出栈const n = stack.pop();console.log(n.val);p = n.right;}
};

后续遍历:

const postorder = (root) => {if (!root) return;postorder(root.left);postorder(root.right);console.log(root.val);
};
// 先序遍历是 根、左、右,后续遍历时:左、右、根,倒过来是根、右、左,只需要把先序遍历的后面两个颠倒顺序const postorder = (root) => {if (!root) return;const outputStack = [];const stack = [root];while (stack.length) {const n = stack.pop();outputStack.push(n);if (n.left) stack.push(n.left);if (n.right) stack.push(n.right);}while (outputStack.length) {const n = outputStack.pop();console.log(n.val);}
};

http://www.lryc.cn/news/21338.html

相关文章:

  • 【华为OD机试模拟题】用 C++ 实现 - 找到它(2023.Q1)
  • python中yield的使用
  • GO进阶(4) 深入Go的内存管理
  • 【C++】类与对象理解和学习(下)
  • 【Neo4j】Spring Data Neo4j APi阅读随笔
  • JVM内存模型简介
  • k8s如何给node添加标签
  • 【大数据Hive】Hive ddl语法使用详解
  • Connext DDS录制服务 Recording Service(2)
  • mysql数据类型选择
  • 【Java】Spring Boot 配置文件
  • AtCoder Beginner Contest 290 G. Edge Elimination(思维题 枚举+贪心)
  • 数据挖掘概述
  • linux kernel iio 架构
  • Socket通信详解
  • 多分类、正则化问题
  • 史上最全面的软件测试面试题总结(接口、自动化、性能全都有)
  • 速来~与 Werner Vogels 博士一起探索敏捷性与创新速度一起提升的秘方
  • Apache Hadoop、HDFS介绍
  • python“r e 模块“常见函数详解
  • 【数据结构】二叉树的四种遍历方式——必做题
  • Nginx使用“逻辑与”配置origin限制,修复CORS跨域漏洞
  • Laravel框架02:路由与控制器
  • 【POJ 2418】Hardwood Species 题解(映射)
  • React组件之间的通信方式总结(下)
  • 【RabbitMQ笔记07】消息队列RabbitMQ七种模式之Publisher Confirms发布确认模式
  • 【华为OD机试模拟题】用 C++ 实现 - IPv4 地址转换成整数(2023.Q1)
  • 闭包与高阶函数
  • 人工智能轨道交通行业周刊-第35期(2023.2.20-2.26)
  • 快慢指针判断链表是否有环