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. 如何从Maven中央存储库下载?

    根据 Apache Maven说明: 下载时由项目的 pom.xml 文件的依赖来决定,目前不在本地存储库触发(当中央存储库包含了一个更新).默认情况下,Maven将从中央存储库下载. 在Maven中 ...

  2. webdriver高级应用- 启动带有用户配置信息的firefox浏览器窗口

    由于WebDriver启动FireFox浏览器时会启用全新的FireFox浏览器窗口,导致当前机器的FireFox浏览器已经配置的信息在测试中均无法生效,例如已经安装的浏览器插件.个人收藏夹等.为了解 ...

  3. day03_06 变量详解

    print ("hello world") print("alex") print("jinxing") print("3乘以4= ...

  4. equal(),hashcode(),toString()方法的作用

    equal(),hashcode(),toString()方法的作用 这三个方法都是java.lang.Object的方法. equal();判断两对象是否相等hashcode();为对象在容器中添加 ...

  5. 九度oj 题目1355:扑克牌顺子

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  6. 第一次使用HTML

    1.第一次使用HTML <title>第一次使用HTML</title></head><body>hello,HTML2.文本处理 <title& ...

  7. 【基础操作】2-sat

    $2-sat$ 是一个很不怎么考的内容($NOI2017$ 除外) 例题

  8. Foj 2148 二维几何(点是否在三角形内)

    题目大意:给n个坐标(不存在三点共线的点),求能够组成多少个凸四边形. #include<iostream> #include<cstdio> #include<cmat ...

  9. 【CF173B】Chamber of Secrets(二分图,最短路)

    题意:给你一个n*m的地图,现在有一束激光从左上角往右边射出,每遇到‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个‘#’往四个方向射出才能使关系在n行往右边射出. 思路:将每一 ...

  10. 計算 battery impedence

    Origin 一顆電池被拉載後,會產生電流及電壓如下圖, 如何計算其電池內阻呢 其公式為 R = |delta(V) / delta(I)| 公式推導如下: V1 = 10 - I1R --- 左圖 ...