Constructor
new Heap()
Example
var heap = new Heap ();
Methods
(static) clear()
This method is used to nullify this heap and make all variables initial.
Example
var h = new Heap ();
h.pushMin(1);
h.pushMin(2);
h.clear();
h.state();
(static) isEmpty() → {Boolean}
This method is used to check if this heap is empty.
Example
var h = new Heap ();
var ret1 = h.isEmpty(); // ret1 = true
h.pushMin(1);
var ret2 = h.isEmpty(); // ret2 = false
Returns:
This method returns ‘true’ if this heap is empty or ‘false’ if this heap is not empty.
- Type
- Boolean
(static) makeMaxHeap(array)
This method makes normal array into max-heap.
Example
var heap = new Heap ();
var arr = [1,2,3];
h.makeMaxHeap(arr) // arr [1,2,3] will be changed arr[3,1,2]
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array | The array is to be reconstructed to max-heap. |
Throws:
This method returns 'false' if inserted array is empty.
(static) makeMinHeap(array)
This method makes normal array into min-heap.
Example
var h = new Heap ();
var arr = [3,2,1];
h.makeMinHeap(arr) // arr [3,2,1] will be changed arr[1,3,2]
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array | The array is to be reconstructed to min-heap. |
Throws:
This method returns 'false' if inserted array is empty.
(static) popMax() → {undefined}
This method is used to remove the very first value from this heap.
This method is used in max-heap which its values are ordered by ASC.
Example
var h = new Heap ();
h.pushMax(1);
h.pushMax(2);
h.popMax(); // 2 will be removed.
Throws:
This method returns null if heap is empty.
Returns:
This method returns the first value from this heap.
- Type
- undefined
(static) popMin() → {undefined}
This method is used to remove the very first value from this heap.
This method is used in min-heap which its values are ordered by DESC.
Example
var h = new Heap ();
h.pushMin(1);
h.pushMin(2);
h.popMin(); // 1 will be removed.
Throws:
This method returns null if heap is empty.
Returns:
This method returns the first value from this heap.
- Type
- undefined
(static) pushMax(value)
This method is used to insert the specified value into this heap.
This method is used in max-heap which its values are ordered by ASC.
Example
var h = new Heap ();
h.pushMax(1);
h.pushMax(2);
Parameters:
| Name | Type | Description |
|---|---|---|
value |
undefined | The value to be inserted to this heap. |
Throws:
This method returns 'false' if parameter is missing.
(static) pushMin(value)
This method is used to insert the specified value into this heap.
This method is used in min-heap which its values are ordered by DESC.
Example
var h = new Heap ();
h.pushMin(1);
h.pushMin(2);
Parameters:
| Name | Type | Description |
|---|---|---|
value |
Undefined | The value to be inserted to this heap. |
Throws:
This method returns 'false' if parameter is missing.
(static) size() → {Number}
This method is used to get the number of values in this heap.
Example
var h = new Heap ();
h.pushMin(1);
h.pushMin(2);
var size = h.size(); // size = 2
Returns:
This method returns the number of values in this heap.
- Type
- Number
(static) state()
This method shows state of the heap.
Example
var h = new Heap ();
h.pushMin(4);
h.pushMin(2);
h.pushMin(3);
h.pushMin(1);
h.state(); // [1,2,3,4]