Applications of red-black trees

核能气质少年 提交于 2019-12-02 15:08:27
starblue

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.

Nikunj Banka

Red Black Trees are from a class of self balancing BSTs and as answered by others, any such self balancing tree can be used. I would like to add that Red-black trees are widely used as system symbol tables. For example they are used in implementing the following:

  • Java: java.util.TreeMap , java.util.TreeSet .
  • C++ STL: map, multimap, multiset.
  • Linux kernel: completely fair scheduler, linux/rbtree.h

Unless you have very specific performance requirements, an R-B tree could be replaced by some other self-balancing binary tree, for example an AVL tree. Choosing between the two of them is basically a performance optimization - they offer the same basic operations.

Not that either of them is definitively "faster" than the other, just that they're different enough that specific uses of them will tend to have slightly different performance, all else being equal. So if you draw your requirements carefully enough, or just by chance, you could end up with one of them being "fast enough" for your use, and the other not. R-B offers slightly faster insertion than AVL, at the cost of slightly slower lookup.

aman Verma

There is no such rule like red black can only be used in a particular case it depends upon the application in cases like when You have to build the tree only once and you have to query it many times then you can go for a AVL tree because in AVL tree searching is quite fast.. But it is strictly balanced so insertion and deletion may take some time AVl tree may be used for language dictionery where You have to build the data structure just once and the red black tree is used in the Completely Fair Scheduler used in current Linux kernels now a days..

the constraints applied on the red black tree also enforce the point that that that the path from the root to the furthest leaf is no more than twice as long as the path from the root to the nearest leaf.

BTW you can look for the various seach and insert etc time required for a red black tree down here

        Average     Worst case

Space   O(n)        O(n) 

Search  O(log n)    O(log n)

Insert  O(log n)    O(log n)

Delete  O(log n)    O(log n)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!