-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHeap.html
More file actions
172 lines (150 loc) · 4.46 KB
/
Heap.html
File metadata and controls
172 lines (150 loc) · 4.46 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../Sort/SortTestHelper.js"></script>
</head>
<body>
<script>
// 最大堆
class maxHeap {
constructor() {
this.data = [];
this.head = 1;
}
initArr(arr) {
this.data = arr;
this.data.unshift(null);
this.heaPiFy();
}
add(el) {
if (!el) return
this.data.push(el);
this.shiftUp()
}
parentK(num) {
return (num / 2) >> 1
}
shiftUp() {
let count = this.data.length - 1;
while (count > 1 && this.data[count] > this.data[this.parentK(count)]) {
swap(this.data, count, this.parentK(count));
count = this.parentK(count);
}
}
shiftDown(k) {
swap(this.data, 1, this.data.length - 1);
this.data.pop();
let count = this.data.length;
while (2 * k < count) {
let j = 2 * k;
if (j + 1 <= count - 1 && this.data[j + 1] > this.data[j]) {
j += 1;
}
if (this.data[k] >= this.data[j]) return;
swap(this.data, k, j);
k = j;
}
console.log(this.data)
}
heaPiFyShiftDown(k) {
let count = this.data.length;
while (2 * k < this.data.length) {
let j = 2 * k;
if (j + 1 <= count - 1 && this.data[j + 1] > this.data[j]) {
j += 1;
}
if (this.data[k] >= this.data[j]) return
swap(this.data, k, j);
k = j;
}
}
heaPiFy(arr) {
for (let i = 0; i < arr.length; i++) {
this.data[i + 1] = arr[i]
}
console.log(this.data)
let n = this.data.length;
for (let i = Math.floor(n / 2); i >= 1; i--) {
this.heaPiFyShiftDown(i)
}
console.log(this.data)
}
//原地堆排序
heapSort(arr) {
this.data = this.data.concat(arr);
let len = arr.length;
for (let i = Math.floor((len - 1) / 2); i > 0; i--) {
this._shiftDown(arr, len, i)
}
for (let i = len - 1; i > 0; i--) {
swap(arr, 1, i);
this._shiftDown(arr, i, 0);
}
}
_shiftDown(arr, n, k) {
while (2 * k < n) {
let j = 2 * k; //在此轮循环中,arr[k]和arr[j]交换位置
if (j + 1 < n && arr[j + 1] > arr[j]) {
j += 1;
}
if (arr[k] >= arr[j]) {
break
}
swap(arr, k, j);
k = j;
}
}
findMax() {
if (!this.data.length) {
throw new TypeError("the data is null")
}
return this.data[this.head];
}
extraMax() {
let res = this.findMax();
this.shiftDown(this.head);
return res
}
replace(e) {
this.data[1] = e;
this.heaPiFyShiftDown(this.head);
}
}
new maxHeap().heaPiFy([3, 1, 5, 7, 2, 4, 9, 6, 10, 8]);
// maxHeap.add(8.5)
// maxHeap.shiftDown(1);
// maxHeap.heapSort([9,8,7,6,5,4,3,2,1,0])
// 最小堆
class minHeap {
constructor(arr) {
this.data = arr.slice(0);
this.data.unshift(null);
this.count = this.data.length - 1;
for (let i = this.count >> 1; i >= 1; i--) {
this.shiftDown(i)
}
}
shiftDown(k) {
while (2 * k <= this.count) {
let j = 2 * k;
if (j + 1 <= this.count && this.data[j] > this.data[j + 1]) {
j += 1
}
if (this.data[k] > this.data[j]) {
[this.data[k], this.data[j]] = [this.data[j], this.data[k]]
}
k = j
}
}
shiftUp(k) {
while (k > 1 && this.data[k] < this.data[k >> 1]) {
[this.data[k], this.data[k >> 1]] = [this.data[k >> 1], this.data[k]];
k = k >> 1
}
}
}
</script>
</body>
</html>