[Algorithms] Build a Binary Tree in JavaScript and Several Traversal Algorithms
A binary tree is a tree where each node may only have up to two children. These children are stored on the left
and right
properties of each node.
When traversing a binary tree, we have three common traversal algorithms: in order, pre-order, and post-order. In this lesson, we write each of these algorithms and explore their differences.
// Binary Trees and Tree Traversal // Binary trees are trees whose nodes can only have up to two children function createBinaryNode(key) {
return {
key,
left: null,
right: null,
addLeft(leftKey) {
const newLeft = createBinaryNode(leftKey)
this.left = newLeft
return newLeft
},
addRight(rightKey) {
const newRight = createBinaryNode(rightKey)
this.right = newRight
return newRight
}
}
} const TRAVERSALS = {
/**
* Javascript Call stack is Last in, First Out,
* So it keep calling
* TRAVERSALS.IN_ORDER(node.left, visitFn)
* Until it reach the bottom left node 'h' (b- d- h)
* h - visitFn get called
* h - TRAVERSALS.IN_ORDER(node.right, visitFn) get called
*
* d - visitFn get called
* d - left
* d - right
*
* b - visitFn
* b - left
* b - right
*/
IN_ORDER: (node, visitFn) => {
if (node !== null) {
console.log('left', node.left && node.left.key)
TRAVERSALS.IN_ORDER(node.left, visitFn)
console.log('call', node.key)
visitFn(node)
console.log('right', node.right && node.right.key)
TRAVERSALS.IN_ORDER(node.right, visitFn)
}
},
PRE_ORDER: (node, visitFn) => {
if (node !== null) {
visitFn(node)
TRAVERSALS.PRE_ORDER(node.left, visitFn)
TRAVERSALS.PRE_ORDER(node.right, visitFn)
}
},
POST_ORDER: (node, visitFn) => {
if (node !== null) {
TRAVERSALS.POST_ORDER(node.left, visitFn)
TRAVERSALS.POST_ORDER(node.right, visitFn)
visitFn(node)
}
}
} function createBinaryTree(rootKey) {
const root = createBinaryNode(rootKey) return {
root,
print(traversalType = 'IN_ORDER') {
let result = '' const visit = node => {
result += result.length === 0 ? node.key : ` => ${node.key}`
} TRAVERSALS[traversalType](this.root, visit) return result
}
}
} const tree = createBinaryTree('a')
const b = tree.root.addLeft('b')
const c = tree.root.addRight('c')
const d = b.addLeft('d')
const e = b.addRight('e')
const f = c.addLeft('f')
const g = c.addRight('g')
const h = d.addLeft('h')
const i = d.addRight('i') console.log('IN_ORDER: ', tree.print())// IN_ORDER: h => d => i => b => e => a => f => c => g
//console.log('PRE_ORDER: ', tree.print('PRE_ORDER')) // PRE_ORDER: a => b => d => h => i => e => c => f => g
//console.log('POST_ORDER: ', tree.print('POST_ORDER')) // POST_ORDER: h => i => d => e => b => f => g => c => a
exports.createBinaryNode = createBinaryNode
exports.createBinaryTree = createBinaryTree
Time complexity: O(n),
Space Complexity O(h) for average cases; h = logN -- this is because we need to stack all the function calls. worse cases: O(n)
[Algorithms] Build a Binary Tree in JavaScript and Several Traversal Algorithms的更多相关文章
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
随机推荐
- OpenStack 计算服务 Nova计算节点部署 (九)
如果使用vmware虚拟机进行部署,需要开启虚拟化:如果是服务器需要在bios上开启. Nova Compute nova-compute 一般运行在计算节点上,通过Messages Queue接收并 ...
- MySQL常用的数学函数
在使用mysql自带的函数要慎重,说是会影响数据执行效率,代价太大.这个也要区分开,区分快软件的引用范畴,比如说内部系统业务逻辑比较复杂,功能点很细,但是并发量不是很大,这个时候用MySQL自带的函数 ...
- 【cocos2d-js网络教程篇】cocos2d-js http网络请求
前言 刚入手cocos2d-js,看到网上的JS的http网络请求,大部分都是错的.原因在于,js-tests里面的网络请求实例没有给出加载完成事件.正确的加载完成事件如下: var xhr = cc ...
- ngCordova安装配置使用教程
ngCordova是什么 ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...
- OOX之间的关系
OOA,OOD,OOP三者关系OOA的分析结果可以作为OOD的需求模型OOD的设计结果作为OOP的指导蓝图OOP负责最终实现目标系统
- 洛谷P1730最小密度路径
题目传送门; 首先理解题目,究其本质就是一个最短路问题,而且数据范围贼水,用floyd完全没问题,但是题目有变化,要求出路径边权值与边数之比,这里就可以考虑在把floyd中的二维数组变为三维,f[ i ...
- mysql查询优化以及面试小结
mysql面试小结: 1.mysql的基本架构 2.mysql的索引 btree+的原理 3.mysql的索引优化 4.mysql的sql查询优化 慢查询日志 Show prodile 全局查询日志 ...
- Luogu P4148 简单题(K-D Tree)
题面 题解 因为强制在线,所以我们不能$cdq$分治,所以考虑用$KDT$,$KDT$维护一个矩阵,然后询问的时候如果当前矩形在询问区间内,直接记贡献,否则判断当前点是否在矩阵内,然后左右分别递归下去 ...
- 写的模块和方法 wap 和 pc
createjs 画了一个曲线功能 rem 的适配方式 $.fn.stop 方法, zepto 没有的, 对于 2d的旋转 变形 还有 移动都可以停下来, 做动画的属性存储, getComputedS ...
- RMQ入门
注:为方便描述算法 便于记忆 所以ST的代码用Pascal书写 见谅 RMQ,即Range Minimum/Maximum Query问题,给定一个区间,询问不同子区间的最值问题. 当询问次数较少时, ...