Search code examples
binary-treebinary-search-treered-black-tree2-3-tree

What kind of decisions to consider while choosing the type of a self-balanced BST?


For instance I know the concept and ideas of 2-3 tree and Red-black tree, but could you give me some situations where one of them is better then the other? What questions should I be asking to myself?

As the question is not just about 2-3 tree and red-black tree, feel free to give examples from other self-balanced trees as well, I'm gonna be looking into them. Just wanted to ask the question from a general perspective for easy googling to others who wonders a similar or the same question.


Solution

  • The main consideration is the number of inserts/erases versus the number of lookups. And usually the choice comes down to either red-black or AVL.

    AVL maintains much stricter balance at the cost of more complicated balance operations. AVL also requires a tiny bit more information per node but both RB and AVL can usually be implemented so that the needed information is hidden in already present members (i.e. in the low bits of buffers allocated at aligned addresses).

    And note if your tree isn't going to be huge it likely doesn't matter what tree you choose (or even a tree at all, a list or even array would likely be fine if you only have a dozen items). And if your tree isn't going to be tens of thousands even very rough balance will likely be 'good enough'.

    If you are going to go to the work of a tree with multiple values in some larger node (as in 2-3 tree it would probably make more sense to go to a full b-tree and use much larger factors. I've only ever encountered 2-3 and 2-3-4 trees during instruction on the way to more useful structures.