Red-black tree based on:
Modifications include:
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.
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
This package is dedicated to the public domain under CC0 1.0.