Skip to content

Commit aee4840

Browse files
committed
Few more algorithms are documented. MaxHeap is added
1 parent 9d27a9f commit aee4840

File tree

4 files changed

+168
-30
lines changed

4 files changed

+168
-30
lines changed

src/data-structures/heap.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Constructor function of maximum heap
3+
*
4+
* @public
5+
*/
6+
function MaxHeap() {
7+
this._heap = [];
8+
}
9+
10+
/**
11+
* Exchange indexes with start index given as argument
12+
* to turn the heap into valid maxheap. On a single call
13+
* this method maintains only a single "branch" of the heap
14+
*
15+
* @private
16+
* @param {number} index The parent
17+
*/
18+
MaxHeap.prototype._heapify = function (index) {
19+
var max = index,
20+
left = 2 * index + 1,
21+
right = 2 * index + 2,
22+
temp;
23+
24+
if (left < this._heap.length && this._heap[left] > this._heap[index])
25+
max = left;
26+
27+
if (right < this._heap.length && this._heap[right] > this._heap[index])
28+
max = right;
29+
30+
if (index !== max) {
31+
temp = this._heap[index];
32+
this._heap[index] = this._heap[max];
33+
this._heap[max] = temp;
34+
this._heapify(max);
35+
}
36+
37+
}
38+
39+
/**
40+
* Increases the key for give index
41+
*
42+
* @public
43+
* @param {number} index Index which key should be increased
44+
* @param {number} value New value of the key
45+
* @returns {number} parent The new position of the element
46+
*/
47+
MaxHeap.prototype.increaseKey = function (index, value) {
48+
var elem = this._heap[index],
49+
parent = Math.floor(index / 2),
50+
temp;
51+
if (elem && elem <= value) {
52+
while (parent >= 0 && elem > this._heap[parent]) {
53+
temp = this._heap[parent];
54+
this._heap[parent] = elem;
55+
this._heap[index] = temp;
56+
index = parent;
57+
parent = Math.floor(parent / 2);
58+
}
59+
}
60+
return parent;
61+
}
62+
63+
/**
64+
* Adds new element to the heap
65+
*
66+
* @public
67+
* @param {number} value The new value which will be inserted
68+
* @returns {number} The index of the inserted value
69+
*/
70+
MaxHeap.prototype.add = function (value) {
71+
this._heap.push(value);
72+
return this.increaseKey(this._heap.length - 1, value);
73+
}
74+
75+
/**
76+
* Gets the current value which is on the top of the heap
77+
*
78+
* @public
79+
* returns {numner} The current largest value which is on the top of the heap
80+
*/
81+
MaxHeap.prototype.max = function () {
82+
return this._heap[0];
83+
}
84+
85+
/**
86+
* Remove and return the current maximum value which is on the top of the heap
87+
*
88+
* @public
89+
* @returns {number} max Extracted value
90+
*/
91+
MaxHeap.prototype.extractMax = function () {
92+
if (!this._heap.length)
93+
throw new 'The heap is already empty!';
94+
95+
var max = this._heap.shift();
96+
this._heapify(0);
97+
return max;
98+
}
99+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
var array = [5, 8, 53, 56, 123, 322, 400, 2356, 8000, 23333];
22

3+
/**
4+
* Searchs for specific element in given array using the binary search algorithm.
5+
* It's complexity is O(log n)
6+
*
7+
* @public
8+
* @param {array} array Input array
9+
* @param {number} key The key of the element which index we should find
10+
* @returns {number} index The index of the element or -1 if not found
11+
*/
312
function binarySearch(array, key) {
413
var middle = Math.round(array.length / 2),
514
left = 0,

src/searching/recursive-binary-search.js

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var array = [5, 8, 53, 56, 123, 322, 400, 2356, 8000, 23333];
2+
3+
4+
/**
5+
* Recursive version of binary search. It's complexity is O(log n).
6+
*
7+
* @public
8+
*/
9+
var binarySearch = (function () {
10+
11+
/**
12+
* Binary search.
13+
*
14+
* @pivate
15+
* @param {array} array Given array where we should find the index of the element
16+
* @param {number} key Key of the element which index should be found
17+
* @param {number} left Left index
18+
* @param {number} right Right index
19+
* @returns {number} index The index of the element or -1 if not found
20+
*
21+
*/
22+
function recursiveBinarySearch(array, key, left, right) {
23+
if (left > right)
24+
return -1;
25+
var middle = Math.floor((right + left) / 2);
26+
if (array[middle] === key)
27+
return middle;
28+
else if (array[middle] > key)
29+
return recursiveBinarySearch(array, key, left, middle - 1);
30+
else
31+
return recursiveBinarySearch(array, key, middle + 1, right);
32+
}
33+
34+
/**
35+
* Calls the binary search function with it's initial values.
36+
*
37+
* @param {array} array The input array
38+
* @param {number} key The key of the element which index should be found
39+
* @returns {number} index The index of the element or -1 if not found
40+
*/
41+
return function (array, key) {
42+
return recursiveBinarySearch(array, key, 0, array.length);
43+
}
44+
45+
}());
46+
47+
48+
49+
console.log(array);
50+
console.log(5, binarySearch(array, 5));
51+
console.log(8, binarySearch(array, 8));
52+
console.log(53, binarySearch(array, 53));
53+
console.log(56, binarySearch(array, 56));
54+
console.log(123, binarySearch(array, 123));
55+
console.log(322, binarySearch(array, 322));
56+
console.log(400, binarySearch(array, 400));
57+
console.log(2356, binarySearch(array, 2356));
58+
console.log(8000, binarySearch(array, 8000));
59+
console.log(8001, binarySearch(array, 8001));
60+
console.log(23333, binarySearch(array, 23333));

0 commit comments

Comments
 (0)