【学习打卡】第13天 数据结构和算法
2022/8/18 4:22:51
本文主要是介绍【学习打卡】第13天 数据结构和算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
二叉树
二叉树是什么
- 树的每个节点最多只有两个子节点。
- JS可以用Object来模拟二叉树。
{ val: 'a', left: { val: 'b', left: { val: 'c', left: null, right: null }, right: { val: 'd', left: null, right: null } }, right: { val: 'e', left: { val: 'f', left: null, right: null }, right: { val: 'g', left: null, right: null } } }
二叉树的先、中、后序遍历 (递归版)
先序遍历
算法口诀:(根 —> 左 —> 右)
- 访问根节点
- 对根节点的左子树进行先序遍历
- 对根节点的右子树进行先序遍历
const root = { val: 'a', left: { val: 'b', left: { val: 'c', left: null, right: null }, right: { val: 'd', left: null, right: null } }, right: { val: 'e', left: { val: 'f', left: null, right: null }, right: { val: 'g', left: null, right: null } } }; const preorder = (root) => { if (!root) return; console.log(root.val); preorder(root.left); preorder(root.right); } preorder(root); // 输出:a b c d e f g
中序遍历
算法口诀:(左 —> 根 —> 右)
- 对根节点的左子树进行中序遍历
- 访问根节点
- 对根节点的右子树进行中序遍历
const root = { val: 'a', left: { val: 'b', left: { val: 'c', left: null, right: null }, right: { val: 'd', left: null, right: null } }, right: { val: 'e', left: { val: 'f', left: null, right: null }, right: { val: 'g', left: null, right: null } } }; const midorder = (root) => { if (!root) return; midorder(root.left); console.log(root.val); midorder(root.right); } midorder(root); // 输出: c b d a f e g
后序遍历
算法口诀:(左 —> 右 —> 根)
- 对根节点的左子树进行后序遍历
- 对根节点的右子树进行后序遍历
- 访问根节点
const root = { val: 'a', left: { val: 'b', left: { val: 'c', left: null, right: null }, right: { val: 'd', left: null, right: null } }, right: { val: 'e', left: { val: 'f', left: null, right: null }, right: { val: 'g', left: null, right: null } } }; const nextorder = (root) => { if (!root) return; nextorder(root.left); nextorder(root.right); console.log(root.val); } nextorder(root); // 输出: c d b f g e a
二叉树的先、中、后序遍历 (非递归版)
- 利用栈和循环实现
先序遍历(根 —> 左 —> 右)
- 创建一个栈
- 先把根节点放入栈中
- 根节点出栈并打印,然后把右节点先入栈,再把左节点入栈
- 重复3步骤,直至栈为空
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); } } preorder(root);
中序遍历(左 —> 根 —> 右)
- 创建一个栈
- 先把根节点放入栈中,再把左节点入栈
- 重复2步骤,直至当前子树的左节点为空
- 当前栈顶出栈并打印,将指针指向当前栈顶元素的右节点
- 重复2、3、4步骤,直至栈和指针都为空停止
const midorder = (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; } } midorder(root);
后序遍历(左 —> 右 —> 根)
- 创建两个栈stack和outStack
- stack用来循环将节点以根 —> 右 —> 左的顺序推入outStack栈中
- 依次输出outStack栈顶节点
const nextorder = (root) => { if (!root) return; const stack = [root]; const outStack = []; while (stack.length) { const n = stack.pop(); outStack.push(n); if (n.left) stack.push(n.left); if (n.right) stack.push(n.right); } while (outStack.length) { const r = outStack.pop(); console.log(r.val) } } nextorder(root);
这篇关于【学习打卡】第13天 数据结构和算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性