-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathindex.js
110 lines (89 loc) · 2.67 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* 堆
*
* @export
* @class Heap
*/
export default class Heap {
constructor(compare = defaultCompre) {
this.arr = [];
this.compare = compare;
}
add(elem) {
this.arr.push(elem);
if (this.arr.length === 1) return;
this._upHeapAdjust((this.arr.length >> 1) - 1);
}
remove() {
if (!this.arr.length) return;
let heap = this.arr;
// 出队列操作,弹出数据头元素
let data = heap[0];
// 用尾元素填充头元素
heap[0] = heap[heap.length - 1];
// 删除尾节点
heap.pop();
//然后从根节点下滤堆
if (heap.length > 1)
this._downHeapAdjust(0);
return data;
}
clear() {
this.arr.length = 0;
}
// 对堆进行上滤操作,使得满足堆性质
_upHeapAdjust(parent) {
let heap = this.arr;
let len = heap.length;
while (parent >= 0) {
let leftChild = 2 * parent + 1;
let rightChild = leftChild + 1;
let max = leftChild;
if (rightChild < len) {
max = this.compare(heap[leftChild], heap[rightChild]) < 0
? rightChild : leftChild;
}
// 如果parent节点小于它的某个子节点的话,此时筛选操作
if (this.compare(heap[parent], heap[max]) < 0) {
let temp = heap[parent];
heap[parent] = heap[max];
heap[max] = temp;
// 继续进行更上一层的过滤
parent = Math.ceil(parent / 2) - 1;
} else break;
}
}
// 对堆进行下滤操作,使得满足堆性质
_downHeapAdjust(parent) {
let heap = this.arr;
let len = heap.length;
while (2 * parent + 1 < len) {
let leftChild = 2 * parent + 1;
let rightChild = leftChild + 1;
let max = leftChild;
if (rightChild < len) {
max = this.compare(heap[leftChild], heap[rightChild]) < 0
? rightChild : leftChild;
}
if (this.compare(heap[parent], heap[max]) < 0) {
let temp = heap[parent];
heap[parent] = heap[max];
heap[max] = temp;
parent = max;
} else break;
}
}
};
function defaultCompre(a, b) {
return a - b;
}
// var arr = [1, 0, 5, 4, 3];
// var heap = new Heap();
// for (let ele of arr) {
// heap.add(ele);
// }
// console.log(`heap: ${heap.arr.join(',')}`);
// for (let ele of arr) {
// heap.remove();
// console.log(`heap: ${heap.arr.join(',')}`);
// }