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. win7 64位旗舰版下载

    http://www.itqnh.com/deepin/win7-64.html mac   windows https://help.apple.com/bootcamp/assistant/6.0 ...

  2. 浅谈我所见的CSS命名风格

    在两年工作中,总结一下我所见的css命名风格. 1.单一class命名 .header { width: 500px; } .item { text-indent: 20%; } 优点:简单,渲染效率 ...

  3. 大数据学习——akka学习

    架构图 重要类介绍 ActorSystem 在Akka中,ActorSystem是一个重量级的结构,他需要分配多个线程,所以在实际应用中,ActorSystem通常是一个单例对象,我们可以使用这个Ac ...

  4. CRM知识点汇总(未完💩💩💩💩💩)

    一:项目中每个类的作用 StarkSite 对照admin中的AdminSite,相当于一个容器,用来存放类与类之间的关系. 先实例化对象,然后执行该对象的register方法.将注册类添加到_reg ...

  5. Python学习-day10 进程

    学习完线程,学习进程 进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大. 模块是threading 和 multiprocessing 多进程multiprocessing multi ...

  6. 第五部分 linux 软件安装RPM SRPM与YUM

    第五部分  linux  软件安装RPM    SRPM与YUM   软件管理员简介 RPM与DPKG两大主流 rpm: redhat       centos     suse    命令:yum ...

  7. Leetcode 447.回旋镖的数量

    回旋镖的数量 给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找 ...

  8. 测试openssl_encrypt

    <?php //$string = 'It works ? Or not it works ?'; //$pass = '1234'; //$method = 'aes128'; // // / ...

  9. BZOJ1150 [CTSC2007]数据备份Backup 【堆 + 链表】

    题目 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏的 ...

  10. 【bzoj5110】Yazid的新生舞会

    这里是 $THUWC$ 选拔时间 模拟赛的时候犯 $SB$ 了,写了所有的部分分,然后直接跑过了 $4$ 个大样例(一个大样例是一个特殊情况)…… 我还以为我非常叼,部分分都写对了,于是就不管了…… ...