UNPKG

avl-tree-typed

Version:
41 lines (40 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MaxHeap = void 0; const heap_1 = require("./heap"); /** * @template E * @template R * Max-oriented binary heap. * Notes and typical use-cases are documented in {@link Heap}. * * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible. * 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children. * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree. * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)). * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required. * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks. * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements. * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance. * @example */ class MaxHeap extends heap_1.Heap { /** * Create a max-heap. For objects, supply a custom comparator. * @param elements Optional initial elements. * @param options Optional configuration. */ constructor(elements = [], options) { super(elements, Object.assign({ comparator: (a, b) => { if (typeof a === 'object' || typeof b === 'object') { throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`); } if (a < b) return 1; if (a > b) return -1; return 0; } }, options)); } } exports.MaxHeap = MaxHeap;