You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected bubbleUp(index: number = this.size() - 1): void {
let parentIndex
while (index > 0) {
Expand DownExpand Up
@@ -111,7 +108,7 @@ export abstract class Heap<T> {
}
public check(): void {
return this._check()
this._check()
}
private _check(index: number = 0): void {
Expand All
@@ -122,41 +119,34 @@ export abstract class Heap<T> {
if (
this.heap[leftChildIndex] &&
!this.isRightlyPlaced(leftChildIndex, index)
)
) {
throw new Error('Heap does not adhere to heap invariant')
}
if (
this.heap[rightChildIndex] &&
!this.isRightlyPlaced(rightChildIndex, index)
)
) {
throw new Error('Heap does not adhere to heap invariant')
}
this._check(leftChildIndex)
this._check(rightChildIndex)
}
}
export class MinHeap<T> extends Heap<T> {
constructor(
compare = (a: T, b: T) => {
return a < b
}
) {
constructor(compare: (a: T, b: T) => boolean = (a: T, b: T) => a < b) {
super(compare)
}
}
export class MaxHeap<T> extends Heap<T> {
constructor(
compare = (a: T, b: T) => {
return a > b
}
) {
constructor(compare: (a: T, b: T) => boolean = (a: T, b: T) => a > b) {
super(compare)
}
}
// Priority queue that supports increasePriority() in O(log(n)). The limitation is that there can only be a single element for each key, and the max number or keys must be specified at heap construction. Most of the functions are wrappers around MinHeap functions and update the keys array.
export class PriorityQueue<T> extends MinHeap<T> {
// Maps from the n'th node to its index within the heap.
private keys: number[]
Expand All
@@ -166,38 +156,36 @@ export class PriorityQueue<T> extends MinHeap<T> {
constructor(
keys_index: (a: T) => number,
num_keys: number,
compare = (a: T, b: T) => {
return a < b
}
compare: (a: T, b: T) => boolean = (a: T, b: T) => a < b
public increasePriority(idx: number, value: T): void {
if (this.keys[idx] === -1) {
// If the key does not exist, insert the value.
this.insert(value)
return
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.