[Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more than one parent nor point to any siblings.
The most common tree structure you see is a web page. The underlying structure is often called the "DOM tree". The html element forms the root of our tree, with children of head and body, so on and so forth. In this lesson, we'll create a quick example of a DOM tree with our tree data structure.
function crateNode (key) {
let children = [];
return {
key,
children,
addChild (cKey) {
const childNode = crateNode(cKey)
this.children.push(childNode)
return childNode;
}
}
}
function createTree (rootKey) {
const root = crateNode(rootKey);
function print () {
let result = '';
function traverse (node, visitFn, depth) {
visitFn(node, depth);
if (node.children.length) {
node.children.forEach(n => traverse(n, visitFn, depth + 1))
}
}
function addKeyToResult(node, depth) {
result +=
result.length === 0
? node.key
: `\n${' '.repeat(depth * 2)}${node.key}`
}
traverse(root, addKeyToResult, 0)
return result;
}
return {
root,
print
}
}
const dom = createTree('html')
const head = dom.root.addChild('head')
const body = dom.root.addChild('body')
const title = head.addChild('title - egghead Tree Lesson')
const header = body.addChild('header')
const main = body.addChild('main')
const footer = body.addChild('footer')
const h1 = header.addChild('h1 - Tree Lesson')
const p = main.addChild('p - Learn about trees!')
const copyright = footer.addChild(`Copyright ${new Date().getFullYear()}`)
console.log(dom.print())
/*
html
head
title - egghead Tree Lesson
body
header
h1 - Tree Lesson
main
p - Learn about trees!
footer
Copyright 2018
*/
[Algorithms] Tree Data Structure in JavaScript的更多相关文章
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- 树状结构 Tree data structure in C#
delegate void TreeVisitor<T>(T nodeData); class NTree<T> { private T data; private Linke ...
- [Algorithm] Linked List Data Structure in JavaScript
A linked list is a collection of items where each item points to the next one in the list. Because o ...
- [Algorithom] Stack Data Structure in JavaScript
A stack is a collection of items that obeys the principle of "last in, first out". Like a ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- [Algorithm] JavaScript Graph Data Structure
A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
随机推荐
- grunt-nodemon参数配置
grunt-nodemon参数配置 nodemon0.2.0版本后参数名称做了较大改动,调整了下nodemon的参数配置,有需要的同学可以参考下: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- linux dd命令创建一定大小的文件
http://www.cnblogs.com/jikexianfeng/p/6103500.html
- URAL 1106 Two Teams二分图
S - Two Teams Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- Docker Caffe部署
Caffe是一个清晰而高效的深度学习框架,纯粹的C++/CUDA架构,支持命令行.Python和MATLAB接口:可以在CPU和GPU直接无缝切换 Caffe的优势 上手快:模型与相应优化都是以文本形 ...
- 【bzoj2563】阿狸和桃子的游戏 贪心
题目描述 阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进行的,设节点权值为w(v),边权为c(e).游戏规则是这样的:1. 阿狸和桃子轮流将图中的顶点染色,阿狸会将顶点染成红色,桃子 ...
- 【Luogu】P3155叶子的染色(树形DP)
题目链接 树形DP水题qwq. 设f[i][j]是以i为根的子树,染成j色,且满足内部需求的最少染色节点数. 设to是x的子节点,那么状态转移方程如此设计: 1.f[i][0] 这个状态表示i不染色, ...
- bzoj 2795 [Poi2012]A Horrible Poem hash+线性筛
题目大意 bzoj 2795 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节. 如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. n<=500 ...
- foj Problem 2275 Game
Problem D Game Accept: 145 Submit: 844Time Limit: 1000 mSec Memory Limit : 262144 KB Problem D ...
- java8 函数式接口——Function/Predict/Supplier/Consumer
Function 我们知道Java8的最大特性就是函数式接口.所有标注了@FunctionalInterface注解的接口都是函数式接口,具体来说,所有标注了该注解的接口都将能用在lambda表达式上 ...
- 【CF500D】New Year Santa Network(树上统计)
..]of longint; z:..]of extended; n,i,m,tot,x1:longint; ans,fenmu,y1:extended; procedure add(a,b:long ...