Heap
A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is less than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.
Ultify's heap is a wrapper around Javascript array with a standard heap interface.
Types
@ultify/datastructure also comes with some implementations of Heap datastructure.
Operations
- insert: Adds elements to the heap.
- extractRoot: Removes and returns the root element from the heap.
property
root: Returns the element of the heap without removing it.- isValid: Check ifs the heap is valid.
- IsEmpty: Checks if the queue is empty.
- toArray: Convert a heap to an array.
static
getLeftChildIndex: Returns the left child's index of a given index.static
getRightChildIndex: Returns the right child's index of a given index.static
getParentIndex: Returns the parent's index of a given index.
APIs
Constructor
By default, a Heap will be created as a Max Heap.
To create another type of Heap, you can provide the compareFn
to create the Heap accordingly.
Ultify's heap time complexity to create a Heap from an array is O().
compareFn
- Type:
<T>(a: T, b: T) => number
- Description: Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise.
fromArray (static)
Ultify's heap time complexity to create a Heap from an array is O().
toArray
Insert
Ultify's heap time complexity to insert a single value to a Heap is O().
Ultify's heap time complexity to insert multiple values to a Heap is O().