Where can I find one ready for use? Or for that matter, a good collection of "standard" data structures, if you know of any?
-
Why would you need a red-black tree when javascript object literals do the same thing and is likely implemented as a red-black tree in C anyway? (could also be implemented as a hash table which would have similar performance characteristics). – slebetman Nov 17 '10 at 17:13
-
2To be a little pedantic: red-black trees have guaranteed log behavior, even in the worst case, but hash tables don't provide that guarantee. One other difference is that red-black trees can be made to work functionally, which might be useful depending on the application. – dyoo Nov 08 '11 at 17:58
5 Answers
I wrote a red-black tree in javascript, available here: https://github.com/vadimg/js_bintrees or as bintrees
in npm. Unlike the other implementations, it has unit tests.

- 592
- 7
- 7
A quick check o' the Interwebs turned up a ready-to-use implementation from Kevin Lindsey (scroll down to Red-Black Trees):
Unfortunately I don't know of a site that has a repository of ready made complex data structures.
I'm guessing they're a tad rare since people rarely use JavaScript for the kind of heavy lifting that would necessitate those kinds of complex structures...but I could be wrong.

- 242,243
- 40
- 408
- 536
-
I'm wondering why they are rare, though, considering how ubiquitous Javascript is in general... – Hamster Nov 17 '10 at 13:04
-
3This implementation is actually of an AVL-Tree, and wrongly labled as Red-Black-tree! Still O(log n), though. – smilingthax Dec 18 '10 at 05:14
Just adding my own implementation for reference. I've created a package called scl
that contains many different data structures. It's fully TypeScript-compatible and the API of different collections is largely the same. Though not perfect, there are some automated unit tests to make sure things keep working.
import { RBTreeIndex } from "scl"
interface Person {
name: string;
email: string;
age: number;
}
const people = new RBTreeIndex<Person, number>([
{
name: 'Bob',
email: 'thebobman@gmail.com',
age: 45,
},
{
name: 'Fred',
email: 'fred@outlook.com',
age: 33,
},
{
name: 'Lisa',
email: 'lisa.turner@gmail.com',
age: 37,
}
]);
// Lisa is the oldest person who is at the very most 40 years old.
const lisa = people.getGreatestLowerBound(40);
// Bob is the youngest person older than Lisa
const bob = lisa.next();
// No one is older than Bob
assert(bob.next() === null);
You can find the repository here and the source code of the Red/Black tree implementation here. The package is also on npm over here.

- 1,934
- 1
- 18
- 26
A javascript standard data structure library with reference to C++ STL, the full name is javascript standard data structure library
github link: https://github.com/ZLY201/js-sdsl
npm link: https://npmjs.com/js-sdsl
Contains various data structures such as Set, Map and hash table implemented using RB-tree, with extremely complete unit tests and performance tests and complete api documentation
It supports CommonJS and ES modules, and supports the introduction of browser script tags. It is written in typescript and has rigorous type inference, making development more efficient.

- 54
- 4