Where can I find one ready for use? Or for that matter, a good collection of "standard" data structures, if you know of any?
To implement the red-black tree, besides the child nodes left and right , we need a reference to the parent node and the node's color. We store the color in a boolean , defining red as false and black as true . We implement the red-black tree in the RedBlackTree class.
Now, the question arises that why do we require a Red-Black tree if AVL is also a height-balanced tree. The Red-Black tree is used because the AVL tree requires many rotations when the tree is large, whereas the Red-Black tree requires a maximum of two rotations to balance the tree.
The bookkeeping invariant: all paths through the tree have the same number of black nodes. A relaxed balance invariant: there is at most one red node that has a red parent.
Questions on red-black trees are very frequently asked in interview questions. Red-black trees are specialized binary search trees which are always balanced, and hence overcomes the short coming of binary search trees which can become unbalanced, resulting in degraded efficiency of search operations.
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.
This library has red-black trees
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With