@prelude/rb-tree

Red-black tree module.

Usage no npm install needed!

<script type="module">
  import preludeRbTree from 'https://cdn.skypack.dev/@prelude/rb-tree';
</script>

README

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 '@preluce/cmp'

const rb = RbTree.of(Cmp.strings)
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

MIT License

Copyright 2021 Mirek Rusin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.