-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.ts
More file actions
184 lines (150 loc) · 3.74 KB
/
Copy pathheap.ts
File metadata and controls
184 lines (150 loc) · 3.74 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
173
174
175
176
177
178
179
180
181
182
183
184
class MinHeap {
private elements: number[];
constructor(elements) {
this.elements = elements;
}
public length() {
return this.elements.length;
}
public print(): void {
console.log(this.elements)
}
public push(n: number) {
this.elements.push(n);
this.up_update(this.elements.length - 1);
}
// delete 堆顶
public pop() {
var p = this.elements[0]
this.elements[0] = this.elements[this.elements.length - 1];
this.elements.pop();
this.down_update(0)
return p
}
public top() {
return this.elements[0];
}
private down_update(idx: number) {
let sonIdx = 2 * idx + 1;
if (sonIdx <= this.elements.length - 1) {
if (sonIdx + 1 <= this.elements.length - 1 && this.elements[sonIdx] > this.elements[sonIdx + 1]) {
sonIdx += 1;
}
if (this.elements[sonIdx] < this.elements[idx]) {
const temp = this.elements[sonIdx];
this.elements[sonIdx] = this.elements[idx];
this.elements[idx] = temp;
this.down_update(sonIdx)
}
}
}
private up_update(idx: number) {
if (idx > 0) {
const fatherIdx = Math.floor((idx - 1) / 2);
if (this.elements[fatherIdx] > this.elements[idx]) {
const temp = this.elements[fatherIdx];
this.elements[fatherIdx] = this.elements[idx];
this.elements[idx] = temp;
this.up_update(fatherIdx)
}
}
}
}
export class MaxHeap {
private elements: number[];
constructor(elements) {
this.elements = elements;
}
public length() {
return this.elements.length;
}
public print(): void {
console.log(this.elements)
}
public push(n: number) {
this.elements.push(n);
this.up_update(this.elements.length - 1);
}
// delete 堆顶
public pop() {
var p = this.elements[0];
this.elements[0] = this.elements[this.elements.length - 1];
this.elements.pop();
this.down_update(0)
return p
}
public top() {
return this.elements[0];
}
private down_update(idx: number) {
let sonIdx = 2 * idx + 1;
if (sonIdx <= this.elements.length - 1) {
if (sonIdx + 1 <= this.elements.length - 1 && this.elements[sonIdx] < this.elements[sonIdx + 1]) {
sonIdx += 1;
}
if (this.elements[sonIdx] > this.elements[idx]) {
const temp = this.elements[sonIdx];
this.elements[sonIdx] = this.elements[idx];
this.elements[idx] = temp;
this.down_update(sonIdx)
}
}
}
private up_update(idx: number) {
if (idx > 0) {
const fatherIdx = Math.floor((idx - 1) / 2);
if (this.elements[fatherIdx] < this.elements[idx]) {
const temp = this.elements[fatherIdx];
this.elements[fatherIdx] = this.elements[idx];
this.elements[idx] = temp;
this.up_update(fatherIdx)
}
}
}
}
// const heap = new MinHeap([3, 4, 9, 5, 6, 11, 23]);
// heap.print()
// heap.push(5)
// heap.print()
// heap.push(1)
// heap.print()
// while (heap.length() > 0) {
// heap.pop()
// heap.print()
// }
// use maxHeap and minHeap to obtain medium quickly
var minH = new MinHeap([])
var maxH = new MaxHeap([])
function insert(i) {
if (!minH.top()) {
minH.push(i)
return;
}
if (i >= minH.top()) {
if (minH.length() > maxH.length()) {
maxH.push(minH.pop());
}
minH.push(i)
} else {
if (minH.length() < maxH.length()) {
minH.push(maxH.pop());
}
maxH.push(i)
}
}
function medium() {
// minH.print()
// maxH.print()
if (minH.length() > maxH.length()) {
return minH.top()
} else if (minH.length() === maxH.length()) {
return (minH.top() + maxH.top()) / 2
} else {
return maxH.top()
}
}
// test
for (var i of [1, 2, 5, 3, 6, 7, 4, 6, 5, 8, 9, 8, 4]) {
insert(i)
console.log(medium())
}