carraysdata-structuresred-black-tree

Can I represent a red black tree as an array?


Is it worth representing a red black tree as an array to eliminate the memory overhead. Or will the array take up more memory since the array will have empty slots?


Solution

  • It will have both positive and negative sides. This answer is applicable for C [since you mentioned this is what you will use]

    Positive sides

    1. Lets assume you have created an array as pool of objects that you will use for red-black tree. Deleting an element or initializing a new element when the position is found will be a little fast, because you probably will use the memory pool you have created yourself.

    Negative sides

    1. Yes the array will most probably end up taking more memory since the array will have empty slots sometimes.
    2. You have to be sure about the MAX size of the red-black trees in this case. So there is a limitation of size.
    3. You are not using the benefit of sequential memory space, so that might be a waste of resource.