1+ /**
2+ * A binary heap is a complete binary tree which satisfies the heap ordering property.
3+ *
4+ * @example
5+ * var Heap = require('path-to-algorithms/src/data-structures/heap').Heap;
6+ *
7+ * var heap = new Heap(function(a, b) {
8+ * return a.birthyear - b.birthyear;
9+ * });
10+ *
11+ * heap.add({
12+ * name: 'John',
13+ * birthyear: 1981
14+ * });
15+ * heap.add({
16+ * name: 'Pavlo',
17+ * birthyear: 2000
18+ * });
19+ * heap.add({
20+ * name: 'Garry',
21+ * birthyear: 1989
22+ * });
23+ * heap.add({
24+ * name: 'Derek',
25+ * birthyear: 1990
26+ * });
27+ * heap.add({
28+ * name: 'Ivan',
29+ * birthyear: 1966
30+ * });
31+ *
32+ * console.log(heap.extract()); // { name: 'Pavlo', birthyear: 2000 }
33+ * console.log(heap.extract()); // { name: 'Derek', birthyear: 1990 }
34+ * console.log(heap.extract()); // { name: 'Garry', birthyear: 1989 }
35+ * console.log(heap.extract()); // { name: 'John', birthyear: 1981 }
36+ * console.log(heap.extract()); // { name: 'Ivan', birthyear: 1966 }
37+ *
38+ * @module data-structures/heap
39+ */
140( function ( exports ) {
41+
242 'use strict' ;
343
444 /**
5- * Constructor function of minimum heap
45+ * Minimum heap constructor.
646 *
747 * @public
8- * @param {function } Function used for comparition between the elements
48+ * @constructor
49+ * @param {Function } cmp Function used for comparition between the elements.
950 */
10- function Heap ( cmp ) {
51+ exports . Heap = function ( cmp ) {
1152 this . _heap = [ ] ;
1253 if ( typeof cmp === 'function' ) {
1354 this . _cmp = cmp ;
2162 /**
2263 * Exchange indexes with start index given as argument
2364 * to turn the tree into a valid heap. On a single call
24- * this method maintains only a single "branch" of the heap.
25- * Complexity O(log n)
65+ * this method maintains only a single "branch" of the heap.<br><br>
66+ *
67+ * Time complexity: O(log N).
2668 *
2769 * @private
28- * @param {Number } index The parent
70+ * @param {Number } index The parent.
2971 */
30- Heap . prototype . _heapify = function ( index ) {
72+ exports . Heap . prototype . _heapify = function ( index ) {
3173 var extr = index ,
3274 left = 2 * index + 1 ,
3375 right = 2 * index + 2 ,
5294 } ;
5395
5496 /**
55- * Changes the key for give index. Complexity O(log n).
56- *
97+ * Changes the key.<br><br>
98+ * Complexity: O(log N).
99+ *
57100 * @public
58- * @param {Number } index Index which key should be changed
59- * @param {Number } value New value of the key
60- * @returns {Number } parent The new position of the element
101+ * @param {Number } index Index of the value which should be changed.
102+ * @param {Number|Object } value New value according to the index.
103+ * @return {Number } New position of the element.
61104 */
62- Heap . prototype . changeKey = function ( index , value ) {
105+ exports . Heap . prototype . changeKey = function ( index , value ) {
63106 this . _heap [ index ] = value ;
64107 var elem = this . _heap [ index ] ,
65108 parent = Math . floor ( index / 2 ) ,
77120 } ;
78121
79122 /**
80- * Updates given node. This operation is useful
123+ * Updates a given node. This operation is useful
81124 * in algorithms like Dijkstra, A* where we need
82- * to decrease/increase the value of givne node.
125+ * to decrease/increase value of the given node.
126+ *
127+ * @public
128+ * @param {Number|Object } node Node which should be updated.
83129 */
84- Heap . prototype . update = function ( node ) {
130+ exports . Heap . prototype . update = function ( node ) {
85131 var idx = this . _heap . indexOf ( node ) ;
86132 if ( idx >= 0 ) {
87133 this . changeKey ( idx , node ) ;
88134 }
89135 } ;
90136
91137 /**
92- * Adds new element to the heap. Complexity O(log n).
138+ * Adds new element to the heap.<br><br>
139+ * Complexity: O(log N).
93140 *
94141 * @public
95- * @param {Number } value The new value which will be inserted
96- * @returns {Number } The index of the inserted value
142+ * @param {Number|Object } value Value which will be inserted.
143+ * @return {Number } Index of the inserted value.
97144 */
98- Heap . prototype . add = function ( value ) {
145+ exports . Heap . prototype . add = function ( value ) {
99146 this . _heap . push ( value ) ;
100147 return this . changeKey ( this . _heap . length - 1 , value ) ;
101148 } ;
102149
103150 /**
104- * Gets the current value which is on the top of the heap. Complexity O(1).
151+ * Returns current value which is on the top of the heap.<br><br>
152+ * Complexity: O(1).
105153 *
106154 * @public
107- * returns {Number} The current top value.
155+ * @return {Number|Object } Current top value.
108156 */
109- Heap . prototype . top = function ( ) {
157+ exports . Heap . prototype . top = function ( ) {
110158 return this . _heap [ 0 ] ;
111159 } ;
112160
113161 /**
114162 * Removes and returns the current extremum value
115- * which is on the top of the heap.
116- * Complexity O(log n ).
163+ * which is on the top of the heap.<br><br>
164+ * Complexity: O(log N ).
117165 *
118166 * @public
119- * @returns {Number } The extremum value
167+ * @returns {Number|Object } The extremum value.
120168 */
121- Heap . prototype . extract = function ( ) {
169+ exports . Heap . prototype . extract = function ( ) {
122170 if ( ! this . _heap . length ) {
123171 throw 'The heap is already empty!' ;
124172 }
125-
126173 var extr = this . _heap . shift ( ) ;
127174 this . _heapify ( 0 ) ;
128175 return extr ;
129176 } ;
130177
131- Heap . prototype . getCollection = function ( ) {
178+ exports . Heap . prototype . getCollection = function ( ) {
132179 return this . _heap ;
133180 } ;
134181
135- Heap . prototype . isEmpty = function ( ) {
182+ /**
183+ * Checks or heap is empty.
184+ *
185+ * @public
186+ * @returns {Boolean } Returns true if heap is empty.
187+ */
188+ exports . Heap . prototype . isEmpty = function ( ) {
136189 return ! this . _heap . length ;
137190 } ;
138191
139- exports . Heap = Heap ;
140-
141- } ( typeof exports === 'undefined' ? window : exports ) ) ;
192+ } ) ( typeof window === 'undefined' ? module . exports : window ) ;
0 commit comments