Applications of red-black trees

后端 未结 4 1888
夕颜
夕颜 2021-01-30 06:35

What are the applications of red-black (RB) trees? Is there any application where only RB Trees can be used and no other data structures?

4条回答
  •  -上瘾入骨i
    2021-01-30 07:32

    A red-black tree is a particular implementation of a self-balancing binary search tree, and today it seems to be the most popular choice of implementation.

    Binary search trees are used to implement finite maps, where you store a set of keys with associated values. You can also implement sets by only using the keys and not storing any values.

    Balancing the tree is needed to guarantee good performance, as otherwise the tree could degenerate into a list, for example if you insert keys which are already sorted.

    The advantage of search trees over hash tables is that you can traverse the tree efficiently in sort order.

    AVL-trees are another variant of balanced binary search trees. They were popular before red-black trees were known. They are more carefully balanced, with a maximal difference of one between the heights of the left and right subtree (RB trees guarantee at most a factor of two). Their main drawback is that rebalancing takes more effort.

    So red-black trees are certainly a good but not the only choice for this application.

提交回复
热议问题