Search code examples
algorithmdata-structuresbinary-treebinary-search-treered-black-tree

How to save the memory when storing color information in Red-Black Trees?


I've bumped into this question at one of Coursera algorithms course and realized that I have no idea how to do that. But still, I have some thoughts about it. The first thing that comes into my mind was using optimized bit set (like Java's BitSet) to get mapping node's key -> color. So, all we need is to allocate a one bit set for whole tree and use it as color information source. If there is no duplicatate elements in the tree - it should work.

Would be happy to see other's ideas about this task.


Solution

  • Use the least significant bit of one of the pointers in the node to store the color information. The node pointers should contain even addresses on most platforms. See details here.