2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-12 07:26:57 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2023-11-15 10:17:44 +00:00
|
|
|
import { descend } from "./comparators.ts";
|
2023-09-12 07:26:57 +00:00
|
|
|
|
|
|
|
/** Swaps the values at two indexes in an array. */
|
|
|
|
function swap<T>(array: T[], a: number, b: number) {
|
2024-01-10 20:18:34 +00:00
|
|
|
const temp = array[a];
|
|
|
|
array[a] = array[b]!;
|
|
|
|
array[b] = temp!;
|
2023-09-12 07:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the parent index for a child index. */
|
|
|
|
function getParentIndex(index: number) {
|
|
|
|
return Math.floor((index + 1) / 2) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A priority queue implemented with a binary heap. The heap is in descending
|
|
|
|
* order by default, using JavaScript's built-in comparison operators to sort
|
|
|
|
* the values.
|
|
|
|
*
|
|
|
|
* | Method | Average Case | Worst Case |
|
|
|
|
* | ----------- | ------------ | ---------- |
|
|
|
|
* | peek() | O(1) | O(1) |
|
|
|
|
* | pop() | O(log n) | O(log n) |
|
|
|
|
* | push(value) | O(1) | O(log n) |
|
|
|
|
*
|
2024-05-27 05:30:26 +00:00
|
|
|
* @example Usage
|
2023-09-12 07:26:57 +00:00
|
|
|
* ```ts
|
|
|
|
* import {
|
|
|
|
* ascend,
|
|
|
|
* BinaryHeap,
|
|
|
|
* descend,
|
2024-04-29 02:57:30 +00:00
|
|
|
* } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-09-12 07:26:57 +00:00
|
|
|
*
|
|
|
|
* const maxHeap = new BinaryHeap<number>();
|
|
|
|
* maxHeap.push(4, 1, 3, 5, 2);
|
|
|
|
* assertEquals(maxHeap.peek(), 5);
|
|
|
|
* assertEquals(maxHeap.pop(), 5);
|
|
|
|
* assertEquals([...maxHeap], [4, 3, 2, 1]);
|
|
|
|
* assertEquals([...maxHeap], []);
|
|
|
|
*
|
|
|
|
* const minHeap = new BinaryHeap<number>(ascend);
|
|
|
|
* minHeap.push(4, 1, 3, 5, 2);
|
|
|
|
* assertEquals(minHeap.peek(), 1);
|
|
|
|
* assertEquals(minHeap.pop(), 1);
|
|
|
|
* assertEquals([...minHeap], [2, 3, 4, 5]);
|
|
|
|
* assertEquals([...minHeap], []);
|
|
|
|
*
|
|
|
|
* const words = new BinaryHeap<string>((a, b) => descend(a.length, b.length));
|
|
|
|
* words.push("truck", "car", "helicopter", "tank");
|
|
|
|
* assertEquals(words.peek(), "helicopter");
|
|
|
|
* assertEquals(words.pop(), "helicopter");
|
|
|
|
* assertEquals([...words], ["truck", "tank", "car"]);
|
|
|
|
* assertEquals([...words], []);
|
|
|
|
* ```
|
2024-05-22 05:55:34 +00:00
|
|
|
*
|
|
|
|
* @typeparam T The type of the values stored in the binary heap.
|
2023-09-12 07:26:57 +00:00
|
|
|
*/
|
|
|
|
export class BinaryHeap<T> implements Iterable<T> {
|
|
|
|
#data: T[] = [];
|
2024-05-22 21:08:37 +00:00
|
|
|
#compare: (a: T, b: T) => number;
|
2024-05-22 05:55:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct an empty binary heap.
|
|
|
|
*
|
|
|
|
* @param compare A custom comparison function to sort the values in the heap. By default, the values are sorted in descending order.
|
|
|
|
*/
|
2024-05-22 21:08:37 +00:00
|
|
|
constructor(compare: (a: T, b: T) => number = descend) {
|
|
|
|
if (typeof compare !== "function") {
|
|
|
|
throw new TypeError(
|
2024-08-21 07:15:14 +00:00
|
|
|
"Cannot construct a BinaryHeap: the 'compare' parameter is not a function, did you mean to call BinaryHeap.from?",
|
2024-05-22 21:08:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
this.#compare = compare;
|
|
|
|
}
|
2024-05-22 05:55:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the underlying cloned array in arbitrary order without sorting.
|
|
|
|
*
|
|
|
|
* @example Getting the underlying array
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.toArray(), [ 5, 4, 3, 1, 2 ]);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns An array containing the values in the binary heap.
|
|
|
|
*/
|
2023-12-19 00:26:13 +00:00
|
|
|
toArray(): T[] {
|
2023-09-12 07:26:57 +00:00
|
|
|
return Array.from(this.#data);
|
|
|
|
}
|
2024-05-22 05:55:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new binary heap from an array like, an iterable object, or an
|
|
|
|
* existing binary heap.
|
|
|
|
*
|
|
|
|
* A custom comparison function can be provided to sort the values in a
|
|
|
|
* specific order. By default, the values are sorted in descending order,
|
|
|
|
* unless a {@link BinaryHeap} is passed, in which case the comparison
|
|
|
|
* function is copied from the input heap.
|
|
|
|
*
|
|
|
|
* @example Creating a binary heap from an array like
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-22 05:55:34 +00:00
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Creating a binary heap from an iterable object
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-22 05:55:34 +00:00
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from((function*() { yield* [4, 1, 3, 5, 2]; })());
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Creating a binary heap from an existing binary heap
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-22 05:55:34 +00:00
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
|
|
|
* const copy = BinaryHeap.from(heap);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Creating a binary heap from an array like with a custom comparison function
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-22 05:55:34 +00:00
|
|
|
* import { BinaryHeap, ascend } from "@std/data-structures";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2], { compare: ascend });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @typeparam T The type of the values stored in the binary heap.
|
|
|
|
* @param collection An array like, an iterable object, or an existing binary heap.
|
|
|
|
* @param options An optional options object to customize the comparison function.
|
|
|
|
* @returns A new binary heap containing the values from the passed collection.
|
|
|
|
*/
|
2024-05-21 14:57:14 +00:00
|
|
|
static from<T>(
|
|
|
|
collection: ArrayLike<T> | Iterable<T> | BinaryHeap<T>,
|
2024-05-22 05:55:34 +00:00
|
|
|
options?: {
|
2023-09-12 07:26:57 +00:00
|
|
|
compare?: (a: T, b: T) => number;
|
|
|
|
},
|
|
|
|
): BinaryHeap<T>;
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Creates a new binary heap from an array like, an iterable object, or an
|
|
|
|
* existing binary heap.
|
|
|
|
*
|
|
|
|
* A custom mapping function can be provided to transform the values before
|
|
|
|
* inserting them into the heap.
|
|
|
|
*
|
|
|
|
* A custom comparison function can be provided to sort the values in a
|
|
|
|
* specific order. By default, the values are sorted in descending order,
|
|
|
|
* unless a {@link BinaryHeap} is passed, in which case the comparison
|
|
|
|
* function is copied from the input heap. The comparison operator is used to
|
|
|
|
* sort the values in the heap after mapping the values.
|
|
|
|
*
|
|
|
|
* @example Creating a binary heap from an array like with a custom mapping function
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-05-22 05:55:34 +00:00
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2], { map: (value) => value * 2 });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @typeparam T The type of the values in the passed collection.
|
|
|
|
* @typeparam U The type of the values stored in the binary heap.
|
|
|
|
* @typeparam V The type of the `this` value when calling the mapping function. Defaults to `undefined`.
|
|
|
|
* @param collection An array like, an iterable object, or an existing binary heap.
|
|
|
|
* @param options The options object to customize the mapping and comparison functions. The `thisArg` property can be used to set the `this` value when calling the mapping function.
|
|
|
|
* @returns A new binary heap containing the mapped values from the passed collection.
|
|
|
|
*/
|
|
|
|
static from<T, U, V = undefined>(
|
2023-09-12 07:26:57 +00:00
|
|
|
collection: ArrayLike<T> | Iterable<T> | BinaryHeap<T>,
|
|
|
|
options: {
|
|
|
|
compare?: (a: U, b: U) => number;
|
|
|
|
map: (value: T, index: number) => U;
|
|
|
|
thisArg?: V;
|
|
|
|
},
|
|
|
|
): BinaryHeap<U>;
|
|
|
|
static from<T, U, V>(
|
|
|
|
collection: ArrayLike<T> | Iterable<T> | BinaryHeap<T>,
|
|
|
|
options?: {
|
|
|
|
compare?: (a: U, b: U) => number;
|
|
|
|
map?: (value: T, index: number) => U;
|
|
|
|
thisArg?: V;
|
|
|
|
},
|
|
|
|
): BinaryHeap<U> {
|
|
|
|
let result: BinaryHeap<U>;
|
|
|
|
let unmappedValues: ArrayLike<T> | Iterable<T> = [];
|
|
|
|
if (collection instanceof BinaryHeap) {
|
|
|
|
result = new BinaryHeap(
|
2024-05-22 21:08:37 +00:00
|
|
|
options?.compare ?? (collection as unknown as BinaryHeap<U>).#compare,
|
2023-09-12 07:26:57 +00:00
|
|
|
);
|
|
|
|
if (options?.compare || options?.map) {
|
|
|
|
unmappedValues = collection.#data;
|
|
|
|
} else {
|
|
|
|
result.#data = Array.from(collection.#data as unknown as U[]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = options?.compare
|
|
|
|
? new BinaryHeap(options.compare)
|
|
|
|
: new BinaryHeap();
|
|
|
|
unmappedValues = collection;
|
|
|
|
}
|
|
|
|
const values: Iterable<U> = options?.map
|
|
|
|
? Array.from(unmappedValues, options.map, options.thisArg)
|
|
|
|
: unmappedValues as U[];
|
|
|
|
result.push(...values);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* The count of values stored in the binary heap.
|
|
|
|
*
|
|
|
|
* The complexity of this operation is O(1).
|
|
|
|
*
|
|
|
|
* @example Getting the length of the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.length, 5);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns The count of values stored in the binary heap.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
get length(): number {
|
|
|
|
return this.#data.length;
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Get the greatest value from the binary heap without removing it, or
|
|
|
|
* undefined if the heap is empty.
|
|
|
|
*
|
|
|
|
* The complexity of this operation is O(1).
|
|
|
|
*
|
|
|
|
* @example Getting the greatest value from the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.peek(), 5);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Getting the greatest value from an empty binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = new BinaryHeap<number>();
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.peek(), undefined);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns The greatest value from the binary heap, or undefined if it is empty.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
peek(): T | undefined {
|
|
|
|
return this.#data[0];
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Remove the greatest value from the binary heap and return it, or return
|
|
|
|
* undefined if the heap is empty.
|
|
|
|
*
|
|
|
|
* @example Removing the greatest value from the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.pop(), 5);
|
|
|
|
* assertEquals([...heap], [4, 3, 2, 1]);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The complexity of this operation is on average and worst case O(log n),
|
|
|
|
* where n is the count of values stored in the binary heap.
|
|
|
|
*
|
|
|
|
* @example Removing the greatest value from an empty binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = new BinaryHeap<number>();
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.pop(), undefined);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns The greatest value from the binary heap, or undefined if the heap is empty.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
pop(): T | undefined {
|
|
|
|
const size: number = this.#data.length - 1;
|
|
|
|
swap(this.#data, 0, size);
|
|
|
|
let parent = 0;
|
|
|
|
let right: number = 2 * (parent + 1);
|
|
|
|
let left: number = right - 1;
|
|
|
|
while (left < size) {
|
2024-01-10 20:18:34 +00:00
|
|
|
const greatestChild = right === size ||
|
2024-05-22 21:08:37 +00:00
|
|
|
this.#compare(this.#data[left]!, this.#data[right]!) <= 0
|
2024-01-10 20:18:34 +00:00
|
|
|
? left
|
|
|
|
: right;
|
2024-05-22 21:08:37 +00:00
|
|
|
if (this.#compare(this.#data[greatestChild]!, this.#data[parent]!) < 0) {
|
2023-09-12 07:26:57 +00:00
|
|
|
swap(this.#data, parent, greatestChild);
|
|
|
|
parent = greatestChild;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
right = 2 * (parent + 1);
|
|
|
|
left = right - 1;
|
|
|
|
}
|
|
|
|
return this.#data.pop();
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Add one or more values to the binary heap, returning the new length of the
|
|
|
|
* heap.
|
|
|
|
*
|
|
|
|
* The complexity of this operation is O(1) on average and O(log n) in the
|
|
|
|
* worst case, where n is the count of values stored in the binary heap.
|
|
|
|
*
|
|
|
|
* @example Adding values to the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 2]);
|
|
|
|
* heap.push(5);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals([...heap], [5, 4, 3, 2, 1]);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param values The values to add to the binary heap.
|
|
|
|
* @returns The new length of the binary heap.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
push(...values: T[]): number {
|
|
|
|
for (const value of values) {
|
|
|
|
let index: number = this.#data.length;
|
|
|
|
let parent: number = getParentIndex(index);
|
|
|
|
this.#data.push(value);
|
|
|
|
while (
|
2024-05-22 21:08:37 +00:00
|
|
|
index !== 0 &&
|
|
|
|
this.#compare(this.#data[index]!, this.#data[parent]!) < 0
|
2023-09-12 07:26:57 +00:00
|
|
|
) {
|
|
|
|
swap(this.#data, parent, index);
|
|
|
|
index = parent;
|
|
|
|
parent = getParentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.#data.length;
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Remove all values from the binary heap.
|
|
|
|
*
|
|
|
|
* @example Clearing the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
|
|
|
* heap.clear();
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals([...heap], []);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
clear() {
|
|
|
|
this.#data = [];
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Check if the binary heap is empty.
|
|
|
|
*
|
|
|
|
* @example Checking if the binary heap is empty
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = new BinaryHeap<number>();
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.isEmpty(), true);
|
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* heap.push(42);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(heap.isEmpty(), false);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns true if the binary heap is empty, otherwise false.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
isEmpty(): boolean {
|
|
|
|
return this.#data.length === 0;
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Create an iterator that retrieves values from the binary heap in order
|
|
|
|
* from greatest to least. The binary heap is drained in the process.
|
|
|
|
*
|
|
|
|
* To avoid draining the binary heap, create a copy using
|
2024-07-12 02:46:19 +00:00
|
|
|
* {@link BinaryHeap.from} and then call {@link BinaryHeap.prototype.drain}
|
|
|
|
* on the copy.
|
2024-05-22 05:55:34 +00:00
|
|
|
*
|
|
|
|
* @example Draining the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals([...heap.drain()], [ 5, 4, 3, 2, 1 ]);
|
|
|
|
* assertEquals([...heap.drain()], []);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns An iterator for retrieving and removing values from the binary heap.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
*drain(): IterableIterator<T> {
|
|
|
|
while (!this.isEmpty()) {
|
|
|
|
yield this.pop() as T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 05:55:34 +00:00
|
|
|
/**
|
|
|
|
* Create an iterator that retrieves values from the binary heap in order
|
|
|
|
* from greatest to least. The binary heap is drained in the process.
|
|
|
|
*
|
|
|
|
* @example Getting an iterator for the binary heap
|
|
|
|
* ```ts
|
|
|
|
* import { BinaryHeap } from "@std/data-structures";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
2024-05-22 05:55:34 +00:00
|
|
|
* const heap = BinaryHeap.from([4, 1, 3, 5, 2]);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals([...heap], [ 5, 4, 3, 2, 1 ]);
|
|
|
|
* assertEquals([...heap], []);
|
2024-05-22 05:55:34 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns An iterator for retrieving and removing values from the binary heap.
|
|
|
|
*/
|
2023-09-12 07:26:57 +00:00
|
|
|
*[Symbol.iterator](): IterableIterator<T> {
|
|
|
|
yield* this.drain();
|
|
|
|
}
|
|
|
|
}
|