[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 leftand 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 ...
随机推荐
- 微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——下篇
一.独立验证器 我上篇中我将AndCompositeValidator和OrCompositeValidator归为独立验证器,这2个验证器主要是为了第一类验证服务,可以进行多种验证组合在一起进行复杂 ...
- mysql ERROR 1366
mysql ERROR 1366 mysql> INSERT INTO tb_room VALUES ('9101','9','1',300,'9101',0,1,'超级豪华间','public ...
- Unable to find a qt build, to solve this problem specify a qt build
可能路径设置不对,比如大小写错误导致找不到qmake编译器,点击VS工具栏的QT菜单,选择options,指定qt Build所在的路径(qt安装路径),然后点击ok. 这是修改过默认安装路径的
- Spring Security静态资源访问
在使用Spring Security时要求所有请求都需要授权访问,此时会定义过滤规则如下 protected void configure(HttpSecurity http) throws Exce ...
- Word Ladder(LintCode)
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- Hydra 8.4/8.5新增功能
Hydra 8.4/8.5新增功能 Kali Linux 2017.1自带的Hydra为8.3,现在Hydra升级到8.5,新增以下功能. (1)为输出文件选项-o,添加一个配套选项-b,允许 ...
- phonegap安卓视频播放解决方案
使用phonegap开发的时候,视频播放很多人一开始选择用html5的Video标签作为备选方案,实际上这种方案在真实环境下常常是无法满足需求的,因为目前HTML5的Video标签只支持MP4,OGG ...
- 将Electron桌面应用转化为Win10的UWP应用
如果有小伙伴不知道如何打包Electron桌面应用的,请戳我. 微软提供了一款快捷工具:electron-windows-store,用于将electron桌面应用打包成Windows10系统上.ap ...
- 【DFS】Gym - 100781A - Adjoin the Networks
给你一个森林,让你把它连接成一颗树,使得直径最小. 就求出每颗树的重心以后,全都往直径最大的那个的重心上连,一般情况是最大/2+次大/2+1,次大/2+第三大/2+2 中取较大者. 还有些特殊情况要特 ...
- convirt介绍
convirt2.0是一款使用python和jquery结合编写的其于web的集中管理xen服务的程序.该程序在xen 社区项目,管理项目中被使用的量很高,convirt开发有开源版本与企业版本,企业 ...