Skip to content

Commit

Permalink
update pq
Browse files Browse the repository at this point in the history
  • Loading branch information
yk committed Apr 25, 2024
1 parent c1b8e44 commit e53ef11
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 166 deletions.
111 changes: 62 additions & 49 deletions content/posts/algorithm/data structure/优先队列.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: '优先队列'
date: 2022-09-23T14:43:36+08:00
lastmod: 2024-04-25
series: [data structure]
categories: [algorithm]
---
Expand All @@ -9,66 +10,78 @@ JavaScript 中没有内置优先队列这个数据结构,需要自己来实现

```javascript
class PriorityQueue {
constructor(data, comp) {
this.data = data
this.comp = comp
for (let i = data.length >> 1; i >= 0; --i) {
this.down(i)
constructor(data, cmp) {
this.data = data
this.cmp = cmp
for (let i = data.length >> 1; i >= 0; --i) {
this.down(i)
}
}
}
down(i) {
let left = 2 * i + 1
while (left < this.size()) {
let min = left + 1 && this.comp(this.data[left + 1], this.data[left]) ? left + 1 : left // 细节,left+1 在前
min = this.comp(this.data[i], this.data[min]) ? i : min
if (min === i) {
break
}
this.swap(i, min)
i = min
left = 2 * i + 1
down(i) {
let left = 2 * i + 1
while (left < this.data.length) {
let temp
if (left + 1) {
temp = this.cmp(this.data[left + 1], this.data[i]) ? left + 1 : i
}
temp = this.cmp(this.data[temp], this.data[left]) ? temp : left
if (temp === i) {
break
}
this.swap(this.data, temp, i)
i = temp
left = 2 * i + 1
}
}
}
up(i) {
while (i >= 0 && this.comp(this.data[(i - 1) >> 1], this.data[i])) {
this.swap((i - 1) >> 1, i)
i = (i - 1) >> 1
up(i) {
while (i >= 0) {
const parent = (i - 1) >> 1
if (this.cmp(this.data[i], this.data[parent])) {
this.swap(this.data, parent, i)
i = parent
} else {
break
}
}
}
push(val) {
this.up(this.data.push(val) - 1)
}
poll() {
this.swap(this.data, 0, this.data.length - 1)
const top = this.data.pop()
this.down(0)
return top
}

swap(data, i, j) {
const temp = data[i]
data[i] = data[j]
data[j] = temp
}
}
push(val) {
this.up(this.data.push(val) - 1)
}
poll() {
this.swap(0, this.size() - 1)
const top = this.data.pop()
this.down(0)
return top
},
size() {
return this.data.length
}
swap(i, j) {
const temp = this.data[i]
this.data[i] = this.data[j]
this.data[j] = temp
}
}
```

<!--
之前的版本 过于精炼,今天重新写个语义比较简单的
自己重新写的时候,差点就写成递归版本了,请记住,迭代一版是要使用 while 的,通过 while 条件来控制循环
第二点就是 比较器内,down i 在后,那么 up 里 i 就在前,一般互斥
-->

测试:

```js
const pq = new PriorityQueue([4, 2, 3, 5, 6, 1, 7, 8, 9], (a, b) => a - b > 0)
console.log('📌📌📌 ~ pq', pq)
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.pop())
console.log(pq.poll())
console.log(pq.poll())
console.log(pq.poll())
pq.push(10)
pq.push(20)
console.log(pq.poll())
console.log(pq.poll())
console.log(pq.poll())
console.log(pq.poll())
```

---
Expand Down
Loading

0 comments on commit e53ef11

Please sign in to comment.