UNPKG

@figliolia/data-structures

Version:

Efficient data structures for every day programming

34 lines (33 loc) 965 B
import { Heap } from "./Heap.js"; /** * Max Heap * * A heap that remains sorted descendingly * * ```typescript * import { MaxHeap } from "@figliolia/data-structures"; * * const heap = new MaxHeap<number>(value => value); * heap.push(3); * heap.push(2); * heap.push(1); * heap.pop() // 3 * * const complexDataHeap = new MaxHeap<{ id: number, name: string }>(value => value.id); * complexDataHeap.push({ id: 3, name: "Jeff" }); * complexDataHeap.push({ id: 2, name: "Steve" }); * complexDataHeap.push({ id: 1, name: "Dave" }); * complexDataHeap.pop() // { id: 3, name: "Jeff" } * ``` */ export class MaxHeap extends Heap { nextChild(left, right) { return right < this.length && this.extract(this.storage[right]) > this.extract(this.storage[left]) ? right : left; } comparer(index1, index2) { return (this.extract(this.storage[index1]) <= this.extract(this.storage[index2])); } }