-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminHeap2-spec.js
75 lines (68 loc) · 2 KB
/
minHeap2-spec.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
function MinHeap(){
this.dataStore = [null];
}
MinHeap.prototype.insert = function(val){
this.dataStore.push(val);
let current_index = this.dataStore.length - 1;
let current = this.dataStore[current_index];
this.bubbleUp(current_index, current);
return this;
}
MinHeap.prototype.bubbleUp = function(current_index, current){
while(current_index > 1){
let parent_index = Math.floor(current_index / 2);
let parent = this.dataStore[parent_index];
if(current < parent){
this.dataStore[parent_index] = current;
this.dataStore[current_index] = parent;
current_index = parent_index;
} else {
break;
}
}
}
MinHeap.prototype.removeSmallest = function(){
let output = this.dataStore[1];
let new_root = this.dataStore.pop();
if(this.dataStore.length > 2){
this.dataStore[1] = new_root;
this.bubbleDown(1, this.dataStore[1]);
}
return output;
}
MinHeap.prototype.bubbleDown = function(current_index, current){
if(current_index < this.dataStore.length){
let children = this.dataStore.slice(current_index * 2, current_index * 2 + 2);
let min = Math.min(...children);
while(min < current){
let target = this.dataStore.indexOf(min);
this.swap(current_index, target);
current_index = target;
children = this.dataStore.slice(current_index * 2, current_index * 2 + 2);
min = Math.min(...children);
}
}
}
MinHeap.prototype.swap = function(i, j){
let temp = this.dataStore[i];
this.dataStore[i] = this.dataStore[j];
this.dataStore[j] = temp;
}
function rand(min, max){
return Math.floor(Math.random() * (max - min + 1) + min);
}
describe('minheap', function(){
it('keeps the min value at the front of the heap', function(){
let heap = new MinHeap();
for(let i = 1; i < rand(50, 100); i++){
heap.insert(rand(1, 100000))
}
while(heap.dataStore.length > 1){
let true_min = Math.min(...heap.dataStore.slice(1));
expect(heap.dataStore[1]).toBe(true_min);
expect(heap.removeSmallest()).toBe(true_min);
}
expect(heap.dataStore.length).toBe(1);
console.log(heap);
})
})