资讯专栏INFORMATION COLUMN

javascript数据结构

desdik / 846人阅读

摘要:数据结构栈一种遵从先进后出原则的有序集合队列遵从先进先出原则的有序项优先队列修改版的队列,设置优先级循环队列基于队列,克服假溢出想象的队列。这种数据结构非常方便,提供了一个便利的语法来访问它的元素。

javascript数据结构

一种遵从先进后出原则的有序集合

队列

遵从先进先出原则的有序项

优先队列

修改版的队列,设置优先级

循环队列

基于队列,克服‘假溢出’想象的队列。例如队列长度为5,取第6个数据时,会取第一个数据

链表

要存储多个元素,数组(或列表)可能是最常用的数据结构

每种语言都实现了数组。这种数据结构非常方便,提供了一个便利的[]语法来访问它的元素。

然后,这种数据结构有一个缺点:在大多数语言中,数组的大小是固定的,从数组的起点或中间插入或移除项的成本很高,因需要移动元素。

尽管javascript中的Array类方法可以帮我们做这些事,但背后的处理机制同样如此。

链表储存有序的元素集合,但不同于数组,链表中的元素在内存中不是连续放置的。每个元素由一个储存元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成

相对于传统的数组,链表的一个好处在于,添加或移除元素的时候不需要移动其他元素。然而,链表需要使用指针,因此实现链表时需要额外注意。

数组的另一个细节是可以直接访问任意位置的任何元素,而要想访问链表中间的一个元素,需要从起点(表头)开始迭代列表直到找到所需的元素

</>复制代码

  1. // 链表节点
  2. class Node {
  3. constructor(element) {
  4. this.element = element
  5. this.next = null
  6. }
  7. }
  8. // 链表
  9. class LinkedList {
  10. constructor() {
  11. this.head = null
  12. this.length = 0
  13. }
  14. // 追加元素
  15. append(element) {
  16. const node = new Node(element)
  17. let current = null
  18. if (this.head === null) {
  19. this.head = node
  20. } else {
  21. current = this.head
  22. while(current.next) {
  23. current = current.next
  24. }
  25. current.next = node
  26. }
  27. this.length++
  28. }
  29. // 任意位置插入元素
  30. insert(position, element) {
  31. if (position >= 0 && position <= this.length) {
  32. const node = new Node(element)
  33. let current = this.head
  34. let previous = null
  35. let index = 0
  36. if (position === 0) {
  37. this.head = node
  38. } else {
  39. while (index++ < position) {
  40. previous = current
  41. current = current.next
  42. }
  43. node.next = current
  44. previous.next = node
  45. }
  46. this.length++
  47. return true
  48. }
  49. return false
  50. }
  51. // 移除指定位置元素
  52. removeAt(position) {
  53. // 检查越界值
  54. if (position > -1 && position < length) {
  55. let current = this.head
  56. let previous = null
  57. let index = 0
  58. if (position === 0) {
  59. this.head = current.next
  60. } else {
  61. while (index++ < position) {
  62. previous = current
  63. current = current.next
  64. }
  65. previous.next = current.next
  66. }
  67. this.length--
  68. return current.element
  69. }
  70. return null
  71. }
  72. // 寻找元素下标
  73. findIndex(element) {
  74. let current = this.head
  75. let index = -1
  76. while (current) {
  77. if (element === current.element) {
  78. return index + 1
  79. }
  80. index++
  81. current = current.next
  82. }
  83. return -1
  84. }
  85. // 删除指定文档
  86. remove(element) {
  87. const index = this.indexOf(element)
  88. return this.removeAt(index)
  89. }
  90. isEmpty() {
  91. return !this.length
  92. }
  93. size() {
  94. return this.length
  95. }
  96. // 转为字符串
  97. toString() {
  98. let current = this.head
  99. let string = ""
  100. while (current) {
  101. string += ` ${current.element}`
  102. current = current.next
  103. }
  104. return string
  105. }
  106. }
双向链表

与普通链表的区别在于,双向链表的链接是双向的,一个链向下一个元素,一个链向上一个元素。

双向链表提供了两种迭代列表的方法,从头到尾或反过来。在单向链表中,如果迭代列表时错过了要找的元素,就需要回到列表起点,重新开始迭代。

循环链表

循环链表可以是单向链表一样只有单向引用,也可以向双向链表有双向引用。循环链表和链表之间唯一的区别在于,最后元素指向下一个元素的指针(tail.next)不是引用null,而是指向第一个元素(head)

链表相比数组最重要的优点,就是无需移动链表中的元素,就能轻松地添加移除元素。因此,当你需要添加移除很多元素时,最好的选择就是链表,而不是数组

集合

集合是由一组无序且唯一(不能重复)的项组成的。这个数据结构使用了与优先集合相同的数学概念,但应用在计算机科学的数据结构中

</>复制代码

  1. class Set {
  2. constructor() {
  3. this.items = {}
  4. }
  5. has(value) {
  6. return this.items.hasOwnProperty(value)
  7. }
  8. add(value) {
  9. if (!this.has(value)) {
  10. this.items[value] = value
  11. return true
  12. }
  13. return false
  14. }
  15. remove(value) {
  16. if (this.has(value)) {
  17. delete this.items[value]
  18. return true
  19. }
  20. return false
  21. }
  22. get size() {
  23. return Object.keys(this.items).length
  24. }
  25. get values() {
  26. return Object.keys(this.items)
  27. }
  28. }
字典

集合,字典,散列表都可以存储不重复的数据。字典和集合很像,集合是以{ value: value }的形式存储数据,而字典是以{ key: value}的形式存储数据,字典也称为映射。

object对象便是字典在javascript中的实现

一个树结构包含一系列存在父子关系的节点。每个节点都有一个父节点(除顶部的第一个节点)以及零个或者多个子节点

二叉树和二叉搜索树

二叉树中的节点最多只能有两个子节点:一个是左侧子节点,另一个是右侧子节点。二叉树在计算机科学中应用非常广泛

二叉搜索树(BST)是二叉树的一种,但是它只允许你在左侧节点存储比父节点小的值,右侧节点存储比父节点大的值

下图展示二叉搜索树的组织结构方式

代码实现二叉搜索树

</>复制代码

  1. class Node {
  2. constructor(key) {
  3. this.key = key
  4. this.left = null
  5. this.right = null
  6. }
  7. }
  8. class BinarySearchTree {
  9. constructor() {
  10. this.root = null
  11. }
  12. insert(key) {
  13. const newNode = new Node(key)
  14. const insertNode = (node, newNode) => {
  15. if (newNode.key < node.key) {
  16. if (node.left === null) {
  17. node.left = newNode
  18. } else {
  19. insertNode(node.left, newNode)
  20. }
  21. } else {
  22. if (node.right === null) {
  23. node.right = newNode
  24. } else {
  25. insertNode(node.right, newNode)
  26. }
  27. }
  28. }
  29. if (!this.root) {
  30. this.root = newNode
  31. } else {
  32. insertNode(this.root, newNode)
  33. }
  34. }
  35. }

使用二叉搜索树

</>复制代码

  1. const tree = new BinarySearchTree()
  2. tree.insert(11)
  3. tree.insert(7)
  4. tree.insert(5)
  5. tree.insert(3)
  6. tree.insert(9)
  7. tree.insert(8)
  8. tree.insert(10)
  9. tree.insert(13)
  10. tree.insert(12)
  11. tree.insert(14)
  12. tree.insert(20)
  13. tree.insert(18)
  14. tree.insert(25)

构建的树如下图:

树的遍历

遍历一颗树是指访问树的每一个节点并对它们进行某种操作的过程。

访问树的所有节点有三种方式:中序、先序、后序

中序遍历

中序遍历是一种以上行顺序访问BST所有节点的遍历方式,也就是以从小到最大的顺序访问所有的节点。中序遍历的一种应用就是对树进行排序操作。实现如下:

</>复制代码

  1. inOrderTraverse(callback) {
  2. const inOrderTraverseNode = (node, callback) => {
  3. if (node !== null) {
  4. inOrderTraverseNode(node.left, callback)
  5. callback(node.key)
  6. inOrderTraverseNode(node.right, callback)
  7. }
  8. }
  9. inOrderTraverseNode(this.root, callback)
  10. }

inOrderTraverse方法接受一个回调函数作为参数,回掉函数用来定义我们对便利到的每个节点进行的操作,这也叫做访问者模式

在之前展示的树上执行下面的方法:

</>复制代码

  1. tree.inOrderTraverse(value => { console.log(value) })

控制台将会按照顺序输出:3 5 6 7 8 9 10 11 12 13 14 15 18 20 25

下图描述了inOrderTraverse方法的访问路径

先序遍历

先序遍历是以优先于后代节点的顺序访问每个节点的,先序遍历的一种应用是打印一个结构化的文档。

</>复制代码

  1. preOrderTraverse(callback) {
  2. const preOrderTraverseNode = (node, callback) => {
  3. if (node !== null) {
  4. callback(node.key)
  5. preOrderTraverseNode(node.left, callback)
  6. preOrderTraverseNode(node.right, callback)
  7. }
  8. }
  9. preOrderTraverseNode(this.root, callback)
  10. }

下面的图描绘了 preOrderTraverse 方法的访问路径:

后序遍历

后序遍历是先访问节点的后代节点,再访问节点本身。后续遍历的一种应用是计算一个目录和它的子目录所有文件所占空间的大小

</>复制代码

  1. postOrderTraverse(callback) {
  2. const postOrderTraverseNode = (node, callback) => {
  3. if (node !== null) {
  4. postOrderTraverseNode(node.left, callback)
  5. postOrderTraverseNode(node.right, callback)
  6. callback(node.key)
  7. }
  8. }
  9. postOrderTraverseNode(this.root, callback)
  10. }

这个例子中,后续遍历会先访问左侧节点,然后是右侧节点,最后是父节点本身。

中序遍历、先序遍历、后续遍历的实现方式相似的,唯一不同是三行代码的执行顺序。

下图描绘postOrderTraverse方法的访问路径

三种遍历访问顺序的不同

先序遍历:节点本身 => 左侧子节点 => 右侧子节点

中序遍历:左侧子节点 => 节点本身 => 右侧子节点

后序遍历:左侧子节点 => 节点本身 => 右侧子节点

搜索树中的值

在树中,有三种经常执行的搜索顺序:

最大值

最小值

搜索特定值

搜索最大值和最小值

用下图的树作为示例

实现方法:

</>复制代码

  1. min(node) {
  2. const minNode = node => {
  3. return node ? (node.left ? minNode(node.left) : node) : null
  4. }
  5. return minNode(node || this.root)
  6. }
  7. max(node) {
  8. const maxNode = node => {
  9. return node ? (node.right ? maxNode(node.right) : node) : null
  10. }
  11. return maxNode(node || this.root)
  12. }

搜索一个特定的值

</>复制代码

  1. search(key) {
  2. const searchNode = (node, key) => {
  3. if (node === null) return false
  4. if (node.key === key) return node
  5. return searchNode((key < node.key) ? node.left : node.right, key)
  6. }
  7. return searchNode(root, key)
  8. }

移除一个节点

</>复制代码

  1. remove(key) {
  2. const removeNode = (node, key) => {
  3. if (node === null) return false
  4. if (node.key === key) {
  5. console.log(node)
  6. if (node.left === null && node.right === null) {
  7. let _node = node
  8. node = null
  9. return _node
  10. } else {
  11. console.log("key", key)
  12. }
  13. } else if (node.left !== null && node.key > key) {
  14. if (node.left.key === key) {
  15. node.left.key = this.min(node.left.right).key
  16. removeNode(node.left.right, node.left.key)
  17. return node.left
  18. } else {
  19. return removeNode(node.left, key)
  20. }
  21. } else if (node.right !== null && node.key < key) {
  22. if (node.right.key === key) {
  23. node.right.key = this.min(node.right.right).key
  24. removeNode(node.right.right, node.right.key)
  25. return node.right
  26. } else {
  27. return removeNode(node.right, key)
  28. }
  29. } else {
  30. return false
  31. }
  32. return removeNode((key < node.key) ? node.left : node.right, key)
  33. }
  34. return removeNode(this.root, key)
  35. }

完整写法:

</>复制代码

  1. var removeNode = function(node, key){
  2. if (node === null){ //{2}
  3. return null;
  4. }
  5. if (key < node.key){ //{3}
  6. node.left = removeNode(node.left, key); //{4}
  7. return node; //{5}
  8. } else if (key > node.key){ //{6}
  9. node.right = removeNode(node.right, key); //{7}
  10. return node; //{8}
  11. } else { //键等于node.key
  12. //第一种情况——一个叶节点
  13. if (node.left === null && node.right === null){ //{9}
  14. node = null; //{10}
  15. return node; //{11}
  16. }
  17. //第二种情况——一个只有一个子节点的节点
  18. if (node.left === null){ //{12}
  19. node = node.right; //{13}
  20. return node; //{14}
  21. } else if (node.right === null){ //{15}
  22. node = node.left; //{16}
  23. return node; //{17}
  24. }
  25. //第三种情况——一个有两个子节点的节点
  26. var aux = findMinNode(node.right); //{18}
  27. node.key = aux.key; //{19}
  28. node.right = removeNode(node.right, aux.key); //{20}
  29. return node; //{21}
  30. }
  31. };
  32. var findMinNode = function(node){
  33. while (node && node.left !== null) {
  34. node = node.left;
  35. }
  36. return node;
  37. };
自平衡二叉搜索树和红黑树

TODO

javascript中的闭包、访问器、工厂模式、构造函数模式、原型模式、动态原型模式

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/99634.html

相关文章

  • JS程序

    摘要:设计模式是以面向对象编程为基础的,的面向对象编程和传统的的面向对象编程有些差别,这让我一开始接触的时候感到十分痛苦,但是这只能靠自己慢慢积累慢慢思考。想继续了解设计模式必须要先搞懂面向对象编程,否则只会让你自己更痛苦。 JavaScript 中的构造函数 学习总结。知识只有分享才有存在的意义。 是时候替换你的 for 循环大法了~ 《小分享》JavaScript中数组的那些迭代方法~ ...

    melody_lql 评论0 收藏0
  • 什么是 JAVASCRIPT

    摘要:,微软发布,同时发布了,该语言模仿同年发布的。,公司在浏览器对抗中没落,将提交给国际标准化组织,希望能够成为国际标准,以此抵抗微软。同时将标准的设想定名为和两类。,尤雨溪发布项目。,正式发布,并且更名为。,发布,模块系统得到广泛的使用。 前言 作为程序员,技术的落实与巩固是必要的,因此想到写个系列,名为 why what or how 每篇文章试图解释清楚一个问题。 这次的 why w...

    ephererid 评论0 收藏0
  • 前端每周清单半年盘点之 JavaScript

    摘要:前端每周清单专注前端领域内容,以对外文资料的搜集为主,帮助开发者了解一周前端热点分为新闻热点开发教程工程实践深度阅读开源项目巅峰人生等栏目。背后的故事本文是对于年之间世界发生的大事件的详细介绍,阐述了从提出到角力到流产的前世今生。 前端每周清单专注前端领域内容,以对外文资料的搜集为主,帮助开发者了解一周前端热点;分为新闻热点、开发教程、工程实践、深度阅读、开源项目、巅峰人生等栏目。欢迎...

    Vixb 评论0 收藏0
  • javascript功能插件大集合,写前端的亲们记得收藏

    摘要:一个专注于浏览器端和兼容的包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。完全插件化的工具,能在中识别和记录模式。健壮的优雅且功能丰富的模板引擎。完整的经过充分测试和记录数据结构的库。 【导读】:GitHub 上有一个 Awesome – XXX 系列的资源整理。awesome-javascript 是 sorrycc 发起维护的 JS 资源列表...

    cfanr 评论0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:转载来源包管理器管理着库,并提供读取和打包它们的工具。能构建更好应用的客户端包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。很棒的组件集合。隐秘地使用和用户数据。 转载来源:https://github.com/jobbole/aw... 包管理器管理着 javascript 库,并提供读取和打包它们的工具。•npm – npm 是 javasc...

    netmou 评论0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:转载来源包管理器管理着库,并提供读取和打包它们的工具。能构建更好应用的客户端包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。很棒的组件集合。隐秘地使用和用户数据。 转载来源:https://github.com/jobbole/aw... 包管理器管理着 javascript 库,并提供读取和打包它们的工具。•npm – npm 是 javasc...

    Hydrogen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<