Skip to content

Latest commit

 

History

History
466 lines (260 loc) · 8.89 KB

skewheap.md

File metadata and controls

466 lines (260 loc) · 8.89 KB

dastal - v5.0.0 / SkewHeap

Class: SkewHeap<T>

A skew heap is a heap implemented as a binary tree (source).

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φ(n) where φ is the golden ratio. This is approximately 1.44*log2(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)

Type parameters

Name
T

Implements

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new SkewHeap<T>(compareFn, elements?)

Instantiate a heap.

Type parameters

Name
T

Parameters

Name Type Description
compareFn CompareFn<T> The function to determine the order of elements.
elements? Iterable<T> A set of elements to initialize the heap with.

Defined in

src/heap/skewHeap.ts:47

Accessors

size

get size(): number

The number of elements in the collection.

Returns

number

Implementation of

Heap.size

Defined in

src/heap/skewHeap.ts:168

Methods

[iterator]

[iterator](): Iterator<T, any, undefined>

Receive an iterator through the list.

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterator<T, any, undefined>

An iterator through the list

Implementation of

Heap.[iterator]

Defined in

src/heap/skewHeap.ts:194


addAll

addAll(elements): number

Insert a set of elements into the heap.

Parameters

Name Type
elements Iterable<T>

Returns

number

The new size of the list.

Implementation of

Heap.addAll

Defined in

src/heap/skewHeap.ts:60


clear

clear(): void

Removes all elements.

Returns

void

Implementation of

Heap.clear

Defined in

src/heap/skewHeap.ts:75


comparator

comparator(): CompareFn<T>

Returns

CompareFn<T>

The function with which elements are sorted

Implementation of

Heap.comparator

Defined in

src/heap/skewHeap.ts:80


contains

contains(element): boolean

Check if an element is in the heap.

Parameters

Name Type
element T

Returns

boolean

true if the element was found, otherwise false.

Implementation of

Heap.contains

Defined in

src/heap/skewHeap.ts:84


delete

delete(element): boolean

Delete an element from the heap.

Parameters

Name Type
element T

Returns

boolean

true if the element was found and deleted, otherwise false.

Implementation of

Heap.delete

Defined in

src/heap/skewHeap.ts:93


merge

merge(heap): SkewHeap<T>

Join with a different heap and modify the existing heap to contain elements of both. Does not modify the input.

Parameters

Name Type
heap Heap<T>

Returns

SkewHeap<T>

The heap.

Implementation of

Heap.merge

Defined in

src/heap/skewHeap.ts:118


peek

peek(): undefined | T

Retrieves, but does not remove, the top of the heap.

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Implementation of

Heap.peek

Defined in

src/heap/skewHeap.ts:133


pop

pop(): undefined | T

Remove the top of the heap (AKA extract).

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Implementation of

Heap.pop

Defined in

src/heap/skewHeap.ts:137


push

push(value): number

Inserts an element into the heap (AKA insert, add).

Parameters

Name Type
value T

Returns

number

The new size of the heap.

Implementation of

Heap.push

Defined in

src/heap/skewHeap.ts:147


pushPop

pushPop(value): T

Insert an element and then remove the top of the heap.

Parameters

Name Type
value T

Returns

T

The element at the top of the heap.

Implementation of

Heap.pushPop

Defined in

src/heap/skewHeap.ts:152


replace

replace(value): undefined | T

Remove the top of the heap and then insert a new element (AKA popPush).

Parameters

Name Type
value T

Returns

undefined | T

The element at the top of the heap or undefined if empty.

Implementation of

Heap.replace

Defined in

src/heap/skewHeap.ts:157


sorted

sorted(): Iterable<T>

Iterate through the heap in sorted order.

Note: Unexpected behavior can occur if the collection is modified during iteration.

Returns

Iterable<T>

Implementation of

Heap.sorted

Defined in

src/heap/skewHeap.ts:172


update

update(curElement, newElement): boolean

Update a specific element.

Parameters

Name Type
curElement T
newElement T

Returns

boolean

true if curElement was found and updated, otherwise false.

Implementation of

Heap.update

Defined in

src/heap/skewHeap.ts:200