-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap.js
90 lines (74 loc) · 1.74 KB
/
Heap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
exports.Heap = function () {
var functions = {};
var priorityQueue = function (compareFunction) {
var self = this;
var heap = [];
var GetChildrenIndex = function (index) {
var left;
var right;
if (index >= 0) {
left = index * 2 + 1;
right = index * 2 + 2;
}
return [left, right];
}
var getParentIndex = function (index) {
return Math.ceil((2 * index / 2) - 1);
}
var swap = function (i, j) {
var temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
self.Add = function (item) {
heap.push(item);
var index = heap.length - 1;
var parentIndex = getParentIndex(index);
while (index > 0 &&
compareFunction(heap[index], heap[parentIndex]) < 0) {
swap(index, parentIndex);
index = parentIndex;
parentIndex = getParentIndex(index)
}
}
self.Peek = function () {
return heap[0];
}
self.Remove = function () {
var currIndex = 0
do {
var children = GetChildrenIndex(currIndex);
var left = children[0];
var right = children[1];
if (compareFunction(heap[left], heap[right]) > 0) {
heap[currIndex] = heap[right];
currIndex = right;
} else {
heap[currIndex] = heap[left];
currIndex = left;
}
} while (heap[currIndex]);
var emptyIndex = getParentIndex(getParentIndex(currIndex));
heap.splice(emptyIndex, 1);
}
self.Length = function () {
return heap.length;
}
return self;
};
functions.MinHeap = function () {
return priorityQueue(function (a, b) {
if (a < b) return -1;
if (a == b) return 0;
if (a > b) return 1;
});
};
functions.MaxHeap = function () {
return priorityQueue(function (a, b) {
if (a < b) return 1;
if (a == b) return 0;
if (a > b) return -1;
});
}
return functions;
}