简体   繁体   English

红黑树的应用

[英]Applications of red-black trees

What are the applications of red-black (RB) trees? 红黑(RB)树有哪些应用? Is there any application where only RB Trees can be used and no other data structures? 是否有任何应用程序只能使用RB树而没有其他数据结构?

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. AVL树是平衡二叉搜索树的另一种变体。 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). 它们更加仔细平衡,左右子树的高度之间的最大差异为1(RB树最多保证两倍)。 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. 所以红黑树肯定是好的,但不是这个应用的唯一选择。

Red Black Trees are from a class of self balancing BSTs and as answered by others, any such self balancing tree can be used. 红黑树来自一类自平衡BST,并且如其他人所回答的,可以使用任何这种自平衡树。 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 . Java:java.util.TreeMap,java.util.TreeSet。
  • C++ STL: map, multimap, multiset. C ++ STL:map,multimap,multiset。
  • Linux kernel: completely fair scheduler, linux/rbtree.h Linux内核:完全公平的调度程序,linux / rbtree.h

Unless you have very specific performance requirements, an RB tree could be replaced by some other self-balancing binary tree, for example an AVL tree. 除非您有非常具体的性能要求,否则RB树可以被其他一些自平衡二叉树替换,例如AVL树。 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. 因此,如果你仔细地或者只是偶然地提出你的要求,你最终可能会有一个“足够快”供你使用,而另一个则没有。 RB offers slightly faster insertion than AVL, at the cost of slightly slower lookup. RB提供的插入速度比AVL略快,但查找速度稍慢。

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.. 没有像红黑这样的规则只能在特定情况下使用,它取决于应用程序,例如当你必须只构建一次树而你必须多次查询它然后你可以去AVL树因为在AVL树搜索是相当快的..但它是严格平衡的,所以插入和删除可能需要一些时间AVl树可用于语言字典,你必须只建立一次数据结构,并在完全使用红黑树现在的Linux内核中使用的Fair Scheduler现在已经有几天了..

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM