Skip to content

Commit 615c32d

Browse files
committed
Docs.
1 parent dba2b32 commit 615c32d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# Red-black tree module
22

3-
Based on:
3+
Red-black tree based on:
44
- [Chris Okasaki. Red-black trees in a functional setting. J. Funct. Program., 9(4):471–477, 1999.](https://www.cs.tufts.edu/comp/150FP/archive/chris-okasaki/redblack99.pdf)
55
- [Kimball Germane and Matthew Might (2014). "Deletion: The curse of the red-black tree." Journal of Functional Programming, 24(4), pp 423-433. July 2014.](https://matt.might.net/papers/germane2014deletion.pdf)
66

7+
Modifications include:
8+
1. keeping track of number elements to support multiset (aka. bag)
9+
2. keeping track of number of elements in children – to answer range queries on counts (ie. percentile on multiset)
10+
11+
Keeping track of those counts doesn't increase complexity of operations.
12+
This is due to functional/immutable implementation.
13+
Updating counts doesn't require any traversal.
14+
Node creation has to keep sum of their immediate children, which is constant time operation.
15+
716
# Usage
817

918
```bash

prelude.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
/** Red. */
12
export const R = 1
3+
4+
/** Black. */
25
export const B = 2
6+
7+
/** Black-black. */
38
export const BB = 3
49

10+
/** Color. */
511
export type P = typeof R | typeof B | typeof BB
612

13+
/** Nil node. */
714
export const E = undefined
15+
16+
/** Black-black nil node. */
817
export const EE = null
18+
19+
/** Node. */
920
export type N<T> = typeof E | typeof EE | {
1021
c: P,
1122
l: N<T>,
@@ -15,9 +26,13 @@ export type N<T> = typeof E | typeof EE | {
1526
s: number // n + l.s + r.s
1627
}
1728

29+
/** Non nil node. */
1830
export type M<T> = NonNullable<N<T>>
31+
32+
/** Node with explicit type for left and optionally right child. */
1933
export type O<T, L, R = N<T>> = { c: P, l: L, v: T, n: number, r: R, s: number }
2034

35+
/** @returns node. */
2136
export const mk =
2237
<T>(c: P, l: N<T>, v: T, n: number, r: N<T>): M<T> => {
2338
const s = n + (l?.s ?? 0) + (r?.s ?? 0)

rb-tree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ export type RbTree<T, K> = {
2121

2222
export type t<T, K> = RbTree<T, K>
2323

24+
/** Range query. */
2425
export type Query<K> =
2526
| undefined
2627
| ({} | { $l: K } | { $le: K } & ({} | { $r: K } | { $re: K }))
2728

29+
/** @returns Red-black tree based on element-to-key mapping function and comparision over keys function. */
2830
export const of =
2931
<T, K>(cmp: Cmp.t<K>, key: ((value: T) => K)): RbTree<T, K> => ({
3032
cmp,

0 commit comments

Comments
 (0)