[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 ...
随机推荐
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- appium安装,和遇到的问题
https://www.cnblogs.com/fnng/p/4540731.html Appium环境搭建时在cmd中输入appium-doctor命令,提示’appium-doctor’ 不是内部 ...
- 九度oj 题目1357:疯狂地Jobdu序列
题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...
- js 抓取页面数据
数据抓取 主要思路和原理 在根节点document中监听所有需要抓取的事件 在元素事件传递中,捕获阶段获取事件信息,进行埋点 通过getBoundingClientRect() 方法可获取元素的大小和 ...
- activemq的安装启动
Activemq安装和启动 官网:http://activemq.apache.org/ 安装启动: $ tar -zxvf apache-activemq-5.11.1-bin.tar.gz ...
- 【bzoj3698】XWW的难题 有上下界最大流
题目描述 XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A ...
- 【Luogu】P3205合唱队(区间DP)
题目链接 通过这题我发现我已经不会DP了 区间DP,f[i][j]是从左面转移来的,d[i][j]是从右面转移来的 然后DP方程是 ]) f[i][j]+=f[i+][j]; ][j]; f[i][j ...
- TypeToken获取运行时泛型类型
最近正好使用到了Guava的TypeToken来获取泛型的类型信息 比如,泛型父类需要获取其子类定义的泛型类型时: public abstract class GenericClazz<V> ...
- Tomcat和JVM的性能调优总结
Tomcat性能调优: 找到Tomcat根目录下的conf目录,修改server.xml文件的内容.对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创 ...
- docker基础——自定义镜像、创建私有仓库、查看 docker 运行状态
一.自定义镜像 1,案例1 要求:请自定义一个 docker 镜像,基于 hub.c.163.com/library/centos,要求创建出来的镜像在生成容器的时候,可以直接使用 ifconfig ...