UNPKG

dastal

Version:

Data Structures & Algorithms implementations

73 lines (72 loc) 2.61 kB
import { BinaryTreeNode } from 'src/tree/binaryTreeNode'; import { CompareFn } from '..'; import { Heap } from './heap'; /** * A skew heap is a heap implemented as a binary tree * ([source](https://en.wikipedia.org/wiki/Skew_heap)). * * A skew heap is a self-adjusting heap which attempts to maintain balance * by unconditionally swapping all nodes in the merge path when merging two heaps. Every * operation that modifies the heap (e.g. push, pop, merge) is considered a merge and is done * by using a skew heap merge. * * Skew heaps can merge more quickly than binary heaps. This can seem contradictory, since * skew heaps have no structural constraints and no guarantee that the height of the tree is * logarithmic (i.e. balanced). However, amortized complexity analysis can demonstrate that * all operations on a skew heap can be done in O(log(n). More specifically, the * amortized complexity is known to be log<sub>φ</sub>(n) where φ is the golden ratio. This is * approximately 1.44*log<sub>2</sub>(n). * * #### Complexity * * | Property | Average | Worst | * | :------- | :------ | :---- | * | Space | O(n) | O(n) * | Push | O(log n) | O(log n) * | Peek | O(1) | O(1) * | Pop | O(log n) | O(log n) * | Search | O(n) | O(n) */ export declare class SkewHeap<T> implements Heap<T> { /** * The function to determine the order of elements. */ protected compare: CompareFn<T>; /** * The number of elements in the list. */ protected length: number; /** * The node at the "top" of the heap. */ protected root: BinaryTreeNode<T> | undefined; /** * Instantiate a heap. * * @param compareFn - The function to determine the order of elements. * @param elements - A set of elements to initialize the heap with. */ constructor(compareFn: CompareFn<T>, elements?: Iterable<T>); addAll(elements: Iterable<T>): number; clear(): void; comparator(): CompareFn<T>; contains(element: T): boolean; delete(element: T): boolean; merge(heap: Heap<T>): this; peek(): T | undefined; pop(): T | undefined; push(value: T): number; pushPop(value: T): T; replace(value: T): T | undefined; get size(): number; sorted(): Iterable<T>; /** * Receive an iterator through the list. * * **Note:** Unexpected behavior can occur if the collection is modified during iteration. * * @returns An iterator through the list */ [Symbol.iterator](): Iterator<T>; update(curElement: T, newElement: T): boolean; }