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的更多相关文章

  1. Python: tree data structure

    # 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...

  2. 树状结构 Tree data structure in C#

    delegate void TreeVisitor<T>(T nodeData); class NTree<T> { private T data; private Linke ...

  3. [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 ...

  4. [Algorithom] Stack Data Structure in JavaScript

    A stack is a collection of items that obeys the principle of "last in, first out". Like a ...

  5. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  6. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  7. 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  ...

  8. [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. ...

  9. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

随机推荐

  1. Redhat7配置yum源(本地源和网络源)

    Redhat7配置yum源(本地源和网络源)   目录 一:配置本地yum源 二:配置网络yum源 YUM(Yellow dog Updater Modified): yum是基于RPM包构建的软件更 ...

  2. [小技巧]使用set对列表去重,并保持列表原来顺序

  3. API生命周期第三阶段:API实施模式,以及结合swagger和项目现状的最佳模式

    这篇博客,主要是宏观介绍一下开发模式,尤其是针对于目前公司前后分离的项目! 一.API实施模式概述 API实施模式,主要是三个,其中API-First又是作为一种指导思想的一种,所以,简单来说事实实施 ...

  4. A - 装箱问题

    Problem Description 一个工厂生产的产品形状都是长方体,高度都是h,主要有1*1,2*2,3*3,4*4,5*5,6*6等6种.这些产品在邮寄时被包装在一个6*6*h的长方体包裹中. ...

  5. 【bzoj2476】战场的数目 矩阵乘法优化dp

    题目描述 (战场定义为对于最高的一列向两边都严格不增的“用积木搭成”的图形) 输入 输入文件最多包含25组测试数据,每个数据仅包含一行,有一个整数p(1<=p<=109),表示战场的图形周 ...

  6. 用echarts.js制作中国地图,点击对应的省市链接到指定页面

    这里使用的是ECharts 2,因为用EChart 3制作的地图上的省市文字标识会有重叠,推测是引入的地图文件china.js,绘制文字的坐标方面的问题,所以,这里还是使用老版本. ECharts 2 ...

  7. hihoCoder #1343 Stable Members

    题目大意$\newcommand{\SD}{\mathrm{SD}}$ 给定一个 $n+1$ 个点的有向无环图,点从 $0$ 开始编号.无重边.自环,且从每个点 $u$ 都能到达 $0$ 号点.如果每 ...

  8. P1023 税收与补贴问题 (模拟)

    题目链接 Solution 比较恶心的模拟题(主要是难看懂题意其实) 题意戳这里 然后根据一些简单的数学常识,可以知道这是一个二次函数. 所以我们每次枚举一个值,然后判定政府给出的价格是否是顶点即可. ...

  9. jenkins配置发送邮件

    1.打开系统管理->系统设置,找到邮件设置,如下: 2.SMTP或者其他方式的发送邮件,可自行配置,一下列出了qq邮箱和163邮箱设置的地方,如下图: qq邮箱: 往下拉,找到如下图: 163邮 ...

  10. HTTP DNS

    试用地址 https://www.dnspod.cn/httpdns/demo 哪些人适合使用HTTP DNS 1.希望降低访问延迟.减少跨网访问的资讯.游戏类APP: 2.希望降低连接失败率,提升业 ...