项目作者: praneshr

项目描述 :
Javascript implementation of Binary Search Tree
高级语言: JavaScript
项目地址: git://github.com/praneshr/BST.JS.git
创建时间: 2018-05-21T05:39:57Z
项目社区:https://github.com/praneshr/BST.JS

开源协议:

下载


BST.JS

Javascript implementation of Binary Search Tree with helper methods

Install

  1. yarn add @praneshravi/bst.js
  2. #or
  3. npm i @praneshravi/bst.js

Available Methods

  • insertValue
  • getRootValue
  • hasValue
  • getMinValue
  • getMaxValue
  • getDepth
  • getTree
  • traverseTree
  • getPath
  • getNode
  • getParent

Examples

  1. const BST = require('@praneshravi/bst.js')
  2. const bst = new BST()
  3. // constructor takes optional paramaters
  4. //
  5. // new BST(tree, valueKey, leftKey, rightKey)
  6. //
  7. // tree = existing BST (default: null)
  8. // valueKey = key used for value (default: 'value')
  9. // leftKey = key used for left edge (default: 'left')
  10. // rightKey = key used for right edge (default: 'right')
  11. //
  12. bst.insertValue(25)
  13. bst.insertValue(15)
  14. bst.insertValue(50)
  15. bst.insertValue(10)
  16. bst.insertValue(22)
  17. bst.insertValue(24)
  18. bst.insertValue(4)
  19. bst.insertValue(12)
  20. bst.insertValue(18)
  21. bst.insertValue(35)
  22. bst.insertValue(70)
  23. bst.insertValue(31)
  24. bst.insertValue(44)
  25. bst.insertValue(66)
  26. bst.insertValue(90)
  27. bst.getRootValue()
  28. // prints 25
  29. bst.hasValue(4)
  30. // true
  31. bst.hasValue(100)
  32. // false
  33. bst.getMinValue()
  34. // 4
  35. bst.getMaxValue()
  36. // 90
  37. bst.getDepth()
  38. // 4
  39. JSON.stringify(bst.getTree(), null, 4)
  40. // {
  41. // "value": 25,
  42. // "right": {
  43. // "value": 50,
  44. // "right": {
  45. // "value": 70,
  46. // "right": {
  47. // "value": 90,
  48. // "right": null,
  49. // "left": null
  50. // },
  51. // "left": {
  52. // "value": 66,
  53. // "right": null,
  54. // "left": null
  55. // }
  56. // },
  57. // "left": {
  58. // "value": 35,
  59. // "right": {
  60. // "value": 44,
  61. // "right": null,
  62. // "left": null
  63. // },
  64. // "left": {
  65. // "value": 31,
  66. // "right": null,
  67. // "left": null
  68. // }
  69. // }
  70. // },
  71. // "left": {
  72. // "value": 15,
  73. // "right": {
  74. // "value": 22,
  75. // "right": {
  76. // "value": 24,
  77. // "right": null,
  78. // "left": null
  79. // },
  80. // "left": {
  81. // "value": 18,
  82. // "right": null,
  83. // "left": null
  84. // }
  85. // },
  86. // "left": {
  87. // "value": 10,
  88. // "right": {
  89. // "value": 12,
  90. // "right": null,
  91. // "left": null
  92. // },
  93. // "left": {
  94. // "value": 4,
  95. // "right": null,
  96. // "left": null
  97. // }
  98. // }
  99. // }
  100. // }
  101. bst.traverseTree('inorder')
  102. // [ 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90 ]
  103. bst.traverseTree('preorder')
  104. // [ 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90 ]
  105. bst.traverseTree('postorder')
  106. // [ 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25 ]
  107. bst.getPath(90)
  108. // 'direction' means the direction in which the tree was traversed to reach the specified value
  109. // [
  110. // { value: 25, direction: 'right' },
  111. // { value: 50, direction: 'right' },
  112. // { value: 70, direction: 'right' },
  113. // { value: 90 }
  114. // ]
  115. bst.getNode(10)
  116. // {
  117. // value: 10,
  118. // right: {
  119. // value: 12
  120. // right: null,
  121. // left: null,
  122. // },
  123. // left: {
  124. // value: 4,
  125. // right: null,
  126. // left: null,
  127. // }
  128. // }
  129. bst.getParent(4)
  130. // {
  131. // value: 10,
  132. // right: {
  133. // value: 12
  134. // right: null,
  135. // left: null,
  136. // },
  137. // left: {
  138. // value: 4,
  139. // right: null,
  140. // left: null,
  141. // }
  142. // }

Development

  1. npm install
  2. npm run build:watch

License

MIT