mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
Add build script for JS and TS codes.
This commit is contained in:
@@ -270,23 +270,11 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="my_heap.cpp"
|
||||
// 使用动态数组,这样无需考虑扩容问题
|
||||
vector<int> maxHeap;
|
||||
[class]{MaxHeap}-[func]{left}
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
[class]{MaxHeap}-[func]{right}
|
||||
|
||||
/* 获取右子结点索引 */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点索引 */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2; // 向下取整
|
||||
}
|
||||
[class]{MaxHeap}-[func]{parent}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -330,62 +318,21 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="my_heap.js"
|
||||
#maxHeap;
|
||||
[class]{MaxHeap}-[func]{#left}
|
||||
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
constructor(nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
this.#maxHeap = nums === undefined ? [] : [...nums];
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
for (let i = this.#parent(this.size() - 1); i >= 0; i--) {
|
||||
this.#siftDown(i);
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{#right}
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
#left(i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子结点索引 */
|
||||
#right(i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点索引 */
|
||||
#parent(i) {
|
||||
return Math.floor((i - 1) / 2); // 向下整除
|
||||
}
|
||||
[class]{MaxHeap}-[func]{#parent}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="my_heap.ts"
|
||||
private maxHeap: number[];
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
constructor(nums?: number[]) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
this.maxHeap = nums === undefined ? [] : [...nums];
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
for (let i = this.parent(this.size() - 1); i >= 0; i--) {
|
||||
this.siftDown(i);
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{left}
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
private left(i: number): number {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
[class]{MaxHeap}-[func]{right}
|
||||
|
||||
/* 获取右子结点索引 */
|
||||
private right(i: number): number {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点索引 */
|
||||
private parent(i: number): number {
|
||||
return Math.floor((i - 1) / 2); // 向下整除
|
||||
}
|
||||
[class]{MaxHeap}-[func]{parent}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@@ -466,19 +413,13 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="my_heap.js"
|
||||
/* 访问堆顶元素 */
|
||||
peek() {
|
||||
return this.#maxHeap[0];
|
||||
}
|
||||
[class]{MaxHeap}-[func]{peek}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="my_heap.ts"
|
||||
/* 访问堆顶元素 */
|
||||
public peek(): number {
|
||||
return this.maxHeap[0];
|
||||
}
|
||||
[class]{MaxHeap}-[func]{peek}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@@ -587,53 +528,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="my_heap.js"
|
||||
/* 元素入堆 */
|
||||
push(val) {
|
||||
// 添加结点
|
||||
this.#maxHeap.push(val);
|
||||
// 从底至顶堆化
|
||||
this.#siftUp(this.size() - 1);
|
||||
}
|
||||
[class]{MaxHeap}-[func]{push}
|
||||
|
||||
/* 从结点 i 开始,从底至顶堆化 */
|
||||
#siftUp(i) {
|
||||
while (true) {
|
||||
// 获取结点 i 的父结点
|
||||
const p = this.#parent(i);
|
||||
// 当“越过根结点”或“结点无需修复”时,结束堆化
|
||||
if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;
|
||||
// 交换两结点
|
||||
this.#swap(i, p);
|
||||
// 循环向上堆化
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{#siftUp}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="my_heap.ts"
|
||||
/* 元素入堆 */
|
||||
public push(val: number): void {
|
||||
// 添加结点
|
||||
this.maxHeap.push(val);
|
||||
// 从底至顶堆化
|
||||
this.siftUp(this.size() - 1);
|
||||
}
|
||||
[class]{MaxHeap}-[func]{push}
|
||||
|
||||
/* 从结点 i 开始,从底至顶堆化 */
|
||||
private siftUp(i: number): void {
|
||||
while (true) {
|
||||
// 获取结点 i 的父结点
|
||||
const p = this.parent(i);
|
||||
// 当“越过根结点”或“结点无需修复”时,结束堆化
|
||||
if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;
|
||||
// 交换两结点
|
||||
this.swap(i, p);
|
||||
// 循环向上堆化
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{siftUp}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@@ -795,72 +700,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="my_heap.js"
|
||||
/* 元素出堆 */
|
||||
poll() {
|
||||
// 判空处理
|
||||
if (this.isEmpty()) throw new Error("堆为空");
|
||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||
this.#swap(0, this.size() - 1);
|
||||
// 删除结点
|
||||
const val = this.#maxHeap.pop();
|
||||
// 从顶至底堆化
|
||||
this.#siftDown(0);
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
[class]{MaxHeap}-[func]{poll}
|
||||
|
||||
/* 从结点 i 开始,从顶至底堆化 */
|
||||
#siftDown(i) {
|
||||
while (true) {
|
||||
// 判断结点 i, l, r 中值最大的结点,记为 ma
|
||||
const l = this.#left(i),
|
||||
r = this.#right(i);
|
||||
let ma = i;
|
||||
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
|
||||
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
|
||||
// 若结点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
// 交换两结点
|
||||
this.#swap(i, ma);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{#siftDown}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="my_heap.ts"
|
||||
/* 元素出堆 */
|
||||
public poll(): number {
|
||||
// 判空处理
|
||||
if (this.isEmpty()) throw new RangeError("Heap is empty.");
|
||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||
this.swap(0, this.size() - 1);
|
||||
// 删除结点
|
||||
const val = this.maxHeap.pop();
|
||||
// 从顶至底堆化
|
||||
this.siftDown(0);
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
[class]{MaxHeap}-[func]{poll}
|
||||
|
||||
/* 从结点 i 开始,从顶至底堆化 */
|
||||
private siftDown(i: number): void {
|
||||
while (true) {
|
||||
// 判断结点 i, l, r 中值最大的结点,记为 ma
|
||||
const l = this.left(i), r = this.right(i);
|
||||
let ma = i;
|
||||
if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;
|
||||
if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;
|
||||
// 若结点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
// 交换两结点
|
||||
this.swap(i, ma);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{siftDown}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@@ -968,29 +818,13 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="my_heap.js"
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
constructor(nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
this.#maxHeap = nums === undefined ? [] : [...nums];
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
for (let i = this.#parent(this.size() - 1); i >= 0; i--) {
|
||||
this.#siftDown(i);
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{constructor}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="my_heap.ts"
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
constructor(nums?: number[]) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
this.maxHeap = nums === undefined ? [] : [...nums];
|
||||
// 堆化除叶结点以外的其他所有结点
|
||||
for (let i = this.parent(this.size() - 1); i >= 0; i--) {
|
||||
this.siftDown(i);
|
||||
}
|
||||
}
|
||||
[class]{MaxHeap}-[func]{constructor}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
Reference in New Issue
Block a user