by Yung L. Leung

梁永良

Linear data structures are simple in direction. A linked list is a list of nodes (each containing their own data) that are linked from one node to the next (and to the previous, for a doubly linked list). A stack builds upward like a tower of data. Each node stacking atop another, and shortens in a last in first out (LIFO) manner. A queue is a line of nodes that elongate from the end of the line and shortens in a first in first out (FIFO) mechanism.

线性数据结构的方向很简单。 链表是节点列表(每个节点都包含自己的数据),这些节点从一个节点链接到下一个节点(对于双链表,则链接到前一个节点)。 堆栈像数据塔一样向上构建。 每个节点堆叠在一起,并以后进先出( LIFO )的方式缩短。 队列是从行尾开始延伸并以先进先出( FIFO )机制缩短的一排节点。

Binary data structures are like a fork in a road of data. The nodes build like the branches of a tree or a heap of rocks.

二进制数据结构就像一条数据之路的分支。 节点的构建就像一棵树的树枝一堆岩石一样。

树木 (Trees)

A binary search tree is made up of nodes that branch off to no more than two nodes (binary). A parent node can have left and right child nodes. By convention, left child nodes contain values less than the parent. Whereas right child nodes contain greater values (left is less, right is greater). All trees begin with a single root node.

二进制搜索树由分支到不超过两个节点(二进制)的节点组成。 父节点可以具有左右子节点。 按照惯例,左子节点包含的值小于父节点。 而右子节点包含更大的值( left较小,right较大 )。 所有树都以单个根节点开头。

To insert a value requires creating a new node, comparing its value to the root & its descendant values, while deciding to search further leftward (insertion of lesser value) or rightward (insertion of greater value). Once an available position is found, the node is inserted in place.

插入值,需要创建一个新节点 ,将其值与及其后代值进行比较,同时决定进一步向左(插入较小的值)或向右(插入较大的值)进行搜索。 找到可用位置后,将节点插入到位。

To find a value is similar to the insertion of a value. You are performing the search for a stored value and returning the node containing it.

查找值类似于插入值。 您正在搜索存储的值并返回包含它的节点。

To make a breadth-first search of values requires storing a root value. Then proceeding with the left child, then the right child and so forth.

要进行广度优先的值搜索 ,需要存储一个根值。 然后继续处理左孩子,然后是右孩子,依此类推。

To make a depth-first search (pre-order) of values requires storing a root value. Then proceeding with all leftward descendants, before the rightward descendants.

要进行值的深度优先搜索(预定) ,需要存储一个根值。 然后继续处理所有向左的后代,再向右的后代。

Since inserting & finding a node of some value are relatively similar processes (insertion inserts a node whereas finding returns a node), it is of no surprise that their complexity is the same, O(log n).

由于插入查找某个值的节点是相对相似的过程(插入会插入一个节点,而查找会返回一个节点),因此其复杂度相同( O(log n) )也就不足为奇了。

For a 3 node binary search tree, to find 5 requires two steps:

对于3节点的二叉搜索树 ,要找到 5则需要两个步骤:

  • Is 5 greater than or less than 3? Proceed rightward.

    5是否大于或小于3? 向右前进。

  • Is 5 equal to the value being searched? Return node.

    5等于要搜索的值吗? 返回节点。

Similarly to insert a node with value 6 requires two steps:

类似地, 插入值为6的节点需要两个步骤:

  • Is 6 greater than or less than 3? Proceed rightward.

    6是大于还是小于3? 向右前进。

  • Is 6 greater than or less than 5? Insert the right side.

    6是大于还是小于5? 插入右侧。

堆 (Heaps)

A binary heap is a pyramidal structure of nodes whose nodes can stack upward with rows of decreasing values toward a minimum (minimum binary heap) or with rows of increasing values toward a maximum (maximum binary heap). Like the tree, each parent node can extend up to two child nodes. Unlike the tree, each parent can contain a lesser value than its children (minimum binary heap) or a greater value (maximum binary heap).

二进制堆是节点的金字塔结构,其节点可以向上堆叠,而递减值的行朝着最小值( 最小二进制堆 ),或者具有递增值的行朝着最大值( 最大二进制堆 )。 像树一样,每个父节点最多可以扩展到两个子节点。 与树不同,每个父级可以包含比其子级更小的值( 最小二进制堆 )或更大的值( 最大二进制堆 )。

For a max binary heap, to insert a value at the base of the pyramid requires comparing it to parent nodes and bubbling up the larger value.

对于最大二进制堆 ,要在金字塔的底部插入一个值,需要将其与父节点进行比较,然后冒泡较大的值。

To extract a max value requires removing the apex value and sinking down a value from the pyramid’s base. This involves finding the higher of the two children nodes.

提取最大值,需要删除顶点值,并从金字塔的底下沉一个值。 这涉及到找到两个子节点中较高的一个。

For a max binary heap, insertion of a node & extraction of a node with the max value both has a complexity of O(log n).

对于最大二进制堆插入节点和提取具有最大值的节点的复杂度均为O(log n)

For a 3 node max binary heap, insertion of a node with value 6 requires two steps.

对于3个节点的最大二进制堆 ,插入值为6的节点需要两个步骤。

  • Upon attaching node of value 6 to a new row (below 4), is 6 greater than or less than 4? Swap.

    在将值6的节点附加到新行(在4以下)时, 6是大于还是小于4? 交换。

  • Is 6 greater than or less than 5? Swap.

    6是大于还是小于5? 交换。

Similarly, after the removal of a node with a max value & replacing it with a node of value 1, two steps remain.

类似地,在删除具有最大值的节点并将其替换为值为1的节点后,剩下两个步骤。

  • Is 1 greater than or less than 5? Swap.

    1是否大于或小于5? 交换。

  • Is 1 greater than or less than 4? Swap.

    1是大于还是小于4? 交换。

Thank you for reading!

感谢您的阅读!

参考: (Reference:)

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

翻译自: https://www.freecodecamp.org/news/binary-data-structures-an-intro-to-trees-and-heaps-in-javascript-962ab536cb42/

二进制数据结构:JavaScript中的树和堆简介相关推荐

  1. Java数据结构之链表、树、堆、图手写双向非循环链表

    数据结构.手写双向非循环链表 文章目录 数据结构.手写双向非循环链表 链表 1.链表的分类 2.链表的特点 二.手写双向非循环链表 2.1方法总结 2.2 环境搭建 2.3 add 添加结点 2.3. ...

  2. javascript中函数的全解简介

    来源:itelite <script  language="javascript"> //切记特殊的两种函数声明方式 /* //Function 构造 var f=ne ...

  3. JavaScript中栈内存与堆内存分别是什么?

    在js引擎中对变量的存储主要有两个位置,堆内存和栈内存.栈内存主要用于存储各种基本类型的变量,包括Boolean.Number.String.Undefined.Null,以及对象变量的指针(地址值) ...

  4. 数据结构与算法之美笔记——基础篇(中):树,二叉树,二叉查找树,平衡二叉查找树,红黑树,递归树,堆

    树: A 节点就是 B 节点的父节点,B 节点是 A 节点的子节点.B.C.D 这三个节点的父节点是同一个节点,所以它们之间互称为兄弟节点.我们把没有父节点的节点叫作根节点,也就是图中的节点 E.我们 ...

  5. 现实世界的数据结构:JavaScript中的表格和图形

    by Yung L. Leung 梁永良 现实世界的数据结构:JavaScript中的表格和图形 (Real world data structures: tables and graphs in J ...

  6. JavaScript中的常见数据结构

    JS中的数据结构 1.Queue 队列 2.Stack 栈 3.Linked List 链表 4.集合 5.树 6.堆 1.Queue 队列 JavaScript中没有队列这个数据结构,但是可以用数组 ...

  7. JavaScript中有关数据结构和算法的最佳书籍

    If you're trying to learn about data structures or algorithms, you're in luck - there are a lot of r ...

  8. JavaScript中的数据结构和算法

    JavaScript不仅是一门用于网页交互的脚本语言,还可以用于编写高效的数据结构和算法.在本文中,我们将介绍JavaScript中可用的数据结构和常见的算法,并说明它们在实际应用中的用途和性能. 数 ...

  9. 抽象语法树在 JavaScript 中的应用

    抽象语法树是什么 在计算机科学中,抽象语法树(abstract syntax tree 或者缩写为 AST),或者语法树(syntax tree),是源代码的抽象语法结构的树状表现形式,这里特指编程语 ...

最新文章

  1. 从零开始 Code Review,两年实战经验分享!
  2. Unity C# Sting.Format的学习
  3. 网易有道2017内推编程题
  4. CFileDialog 在使用sdk 后出现异常 Access violation
  5. SRE(Simple Rule Engine) Document
  6. Z.ExtensionMethods 一个强大的开源扩展库
  7. android adb apk包名,ADB命令简单使用--查看包名、activity等
  8. 机器学习—K-means聚类、密度聚类、层次聚类理论与实战
  9. 2022计算机Java二级考试四十五套题真题【收藏版】(一周裸考计划)
  10. keli下使用断点调试
  11. 贪吃蛇c语言代码vc,纯C语言实现贪吃蛇游戏(VC6.0)
  12. keil5 下载器配置
  13. Java将指定文件/文件夹压缩成zip、rar压缩文件--解決中文乱码
  14. java逻辑与或非_Java基础——逻辑运算符与或非
  15. 写点什么好呢2? 钱、事业、婚姻、人生意义
  16. 检测和寻找木马隐藏的位置的方法
  17. SQL Server 2005无日志文件附加数据库
  18. 午夜与element-ui邂逅
  19. 固态硬盘安装--系统迁移--设置引导启动项
  20. matlab simulink 参数设置,matlab simulink 中 gateway 参数设置

热门文章

  1. 谷歌上线数据搜索引擎 Dataset Search
  2. 事务相关命令 mysql
  3. 对象流 ObjectOutputStream java
  4. 当前目标 1614868689
  5. 随机数的生成 java
  6. flask-01-http通信的回顾
  7. 普惠金融在印尼:GoPay会是东南亚的蚂蚁金服吗?
  8. MetaMask以太坊钱包插件
  9. 大量开发者会将访问token和API密钥硬编码至Android应用
  10. 【转】去掉换行符的几个方法