prelude

@prelude/rb-tree · v1.0.1

SourceAPI referenceChangelognpm

Red-black tree module

Red-black tree based on:

Modifications include:

  1. keeping track of number elements to support multiset (aka. bag)
  2. keeping track of number of elements in children – to answer range queries on counts (ie. percentile on multiset)

Keeping track of those counts doesn't increase complexity of operations. This is due to functional/immutable implementation. Updating counts doesn't require any traversal. Node creation has to keep sum of their immediate children, which is constant time operation.

Usage

npm i -E @prelude/rb-tree
import * as RbTree from '@prelude/rb-tree'
import * as Cmp from '@prelude/cmp'

const rb = RbTree.of(Cmp.string, (_: string) => _)
RbTree.insert(rb, 'foo')
RbTree.insert(rb, 'bar')
console.log(RbTree.has(rb, 'foo')) // true
console.log(RbTree.has(rb, 'baz')) // false
for (const _ of RbTree.each(rb)) {
  console.log(_)
}
// bar
// foo

License

This package is dedicated to the public domain under CC0 1.0.