Lens Scripting API
    Preparing search index...

    A generic binary heap implementation that supports efficient priority operations.

    This data structure maintains elements in a partially ordered heap structure, enabling O(log n) push, pop, and remove operations. The ordering is determined by the provided comparison function.

    The type parameter T must be suitable for use as a key in a JavaScript Map:

    • If T is a primitive (string, number, symbol), values are compared by value.
    • If T is an object, values are compared by reference (object identity).
    • Each element must be uniquely identifiable - two objects with identical properties are considered different keys if they are different object references.

    Duplicate values (by reference or primitive value) are not supported; only the most recently added instance will be tracked.

    Example usage:

    const heap = new BinaryHeap<number>((a, b) => a - b)
    

    Type Parameters

    • T

      The type of elements stored in the heap.

    Index

    Constructors

    • Creates a new binary heap.

      Type Parameters

      • T

      Parameters

      • compareFn: (a: T, b: T) => number

        A function that defines the heap ordering. Should return a negative number if a should be higher priority than b, a positive number if a should be lower priority than b, or zero if they have equal priority.

      Returns BinaryHeap<T>

    Accessors

    • get size(): number

      Gets the number of elements in the heap.

      Returns number

      The number of elements in the heap.

    Methods

    • Builds a heap from an array of items in O(n) time.

      Parameters

      • items: T[]

        The array of items to build the heap from.

      Returns void

    • Removes all elements from the heap.

      Returns void

    • Checks if a value exists in the heap.

      Parameters

      • value: T

        The value to check for.

      Returns boolean

      True if the value exists in the heap, false otherwise.

    • Returns the highest priority element without removing it.

      Returns T

      The highest priority element in the heap.

    • Removes and returns the highest priority element from the heap.

      Returns T

      The highest priority element in the heap.

    • Adds a new element to the heap.

      Parameters

      • value: T

        The element to add to the heap.

      Returns void

    • Rebalances an element in the heap after its value or comparison-affecting properties have changed.

      Parameters

      • value: T

        The element to rebalance in the heap

      Returns boolean

      True if the element was found and rebalanced, false otherwise.

    • Removes a specific element from the heap.

      Parameters

      • value: T

        The element to remove.

      Returns boolean

      True if the element was found and removed, false otherwise.