API Docs for: 0.0.4
Show:

File: Datastructures/Heap.ts

  1. /**
  2. * The Heap class provides the main functionality of a Heap.
  3. *
  4. * @class Heap
  5. */
  6. class Heap {
  7.  
  8. /**
  9. * Binary tree storage array
  10. *
  11. * @property _tree
  12. * @type Array
  13. * @private
  14. */
  15. private _tree:Array<any> = [];
  16.  
  17. /**
  18. *
  19. * @method _parent
  20. * @param n
  21. * @return number
  22. * @private
  23. */
  24. private _parent(n:number):number {
  25. return Math.floor(n - 1 / 2);
  26. }
  27.  
  28. /**
  29. *
  30. * @method _child
  31. * @param n
  32. * @return number
  33. * @private
  34. */
  35. private _child(n:number):number {
  36. return 2 * n + 1;
  37. }
  38.  
  39. /**
  40. *
  41. * @method shiftUp
  42. * @param i
  43. * @private
  44. */
  45. private shiftUp(i:number):void {
  46. while (i > 0) {
  47. if (this._tree[i] <= this._tree[this._parent(i)]) {
  48.  
  49. var swap = this._tree[i];
  50. this._tree[i] = this._tree[this._parent(i)];
  51. this._tree[this._parent(i)] = swap;
  52.  
  53. } else {
  54. break;
  55. }
  56.  
  57. i = this._parent(i);
  58. }
  59. }
  60.  
  61. /**
  62. *
  63. * @method shiftDown
  64. * @param i
  65. * @private
  66. */
  67. private shiftDown(i:number):void {
  68. while (i > 0) {
  69. if (this._tree[i] <= this._tree[this._parent(i)]) {
  70.  
  71. var swap = this._tree[i];
  72. this._tree[i] = this._tree[this._parent(i)];
  73. this._tree[this._parent(i)] = swap;
  74.  
  75. } else {
  76. //return;
  77. }
  78.  
  79. i = this._parent(i);
  80. }
  81. }
  82.  
  83. /**
  84. * Extracts a node from top of the heap and sift up
  85. *
  86. * @method extract
  87. * @return any The value of the extracted node.
  88. */
  89. public extract():any {
  90. var extracted:any = this._tree[0];
  91. this._tree[0] = this._tree.pop();
  92. return extracted;
  93. }
  94.  
  95. /**
  96. * Inserts an element in the heap by sifting it up
  97. *
  98. * @method insert
  99. * @param value The value to insert.
  100. * @return void
  101. */
  102. public insert(value:any):void {
  103. this._tree.push(value);
  104. this.shiftUp(this._tree.length);
  105. }
  106.  
  107. /**
  108. * Peeks at the node from the top of the heap
  109. *
  110. * @method top
  111. * @return any The value of the node on the top.
  112. */
  113. public top():any {
  114. return this._tree[0];
  115. }
  116.  
  117. /**
  118. * Counts the number of elements in the heap
  119. *
  120. * @method count
  121. * @return number the number of elements in the heap.
  122. */
  123. public count():number {
  124. return this._tree.length;
  125. }
  126.  
  127. /**
  128. * Checks whether the heap is empty
  129. *
  130. * @method isEmpty
  131. * @return boolean whether the heap is empty.
  132. */
  133. public isEmpty():boolean {
  134. return (this._tree.length === 0);
  135. }
  136.  
  137. /**
  138. * Rewind iterator back to the start (no-op)
  139. *
  140. * @method rewind
  141. * @return void
  142. */
  143. public rewind():void {
  144. }
  145.  
  146. /**
  147. * Return current node pointed by the iterator
  148. *
  149. * @method current
  150. * @return any The current node value.
  151. */
  152. public current():any {
  153. }
  154.  
  155. /**
  156. * Return current node index
  157. *
  158. * @method key
  159. * @return any The current node index.
  160. */
  161. public key():any {
  162. }
  163.  
  164. /**
  165. * Move to the next node
  166. *
  167. * @method next
  168. * @return void
  169. */
  170. public next():void {
  171. }
  172.  
  173. /**
  174. * Check whether the heap contains more nodes
  175. *
  176. * @method valid
  177. * @return boolean true if the heap contains any more nodes, false otherwise.
  178. */
  179. public valid():boolean {
  180. return false;
  181. }
  182.  
  183. /**
  184. * Recover from the corrupted state and allow further actions on the heap.
  185. *
  186. * @method recoverFromCorruption
  187. * @return void
  188. */
  189. public recoverFromCorruption():void {
  190. }
  191.  
  192. /**
  193. * Compare elements in order to place them correctly in the heap while sifting up.
  194. *
  195. * @method compare
  196. * @param first The value of the first node being compared.
  197. * @param second The value of the second node being compared.
  198. * @return number Result of the comparison, positive integer if value1 is greater than value2, 0 if they are equal, negative integer otherwise.
  199. * Having multiple elements with the same value in a Heap is not recommended. They will end up in an arbitrary relative position.
  200. */
  201. public compare(first:any, second:any):number {
  202. return 1;
  203. }
  204.  
  205. /**
  206. *
  207. * @method _displayNode
  208. * @param node
  209. * @param prefix
  210. * @param last
  211. * @return String
  212. * @private
  213. */
  214. private _displayNode(node, prefix = '', last = true) {
  215.  
  216. var line = prefix + (last ? (prefix ? '└──' : ' ') : '├──') + this._tree[node];
  217.  
  218. if (last) {
  219. prefix += ' ';
  220. } else {
  221. prefix = prefix + '│ ';
  222. }
  223.  
  224. if (this._tree[this._child(node)]) {
  225. line += '\n' + this._displayNode(this._child(node), prefix, false);
  226. }
  227. if (this._tree[this._child(node) + 1]) {
  228. line += '\n' + this._displayNode(this._child(node) + 1, prefix, true);
  229. }
  230.  
  231. return line;
  232. }
  233.  
  234. /**
  235. * Serializes the heap to string
  236. *
  237. * @method toString
  238. * @return string The serialized string.
  239. */
  240. public toString():string {
  241. // start with root and recursively goes to each node
  242. return this._displayNode(0);
  243. }
  244. }
  245.  
  246. export = Heap;