The difference between red-black trees and AVL trees

Can someone explain what are the main differences between the two data structures? I tried to find a source on the Internet that emphasizes differences / similarities, but I did not find anything too informative. In what cases is preferable to others? What practical situations make it better to use than others?

+71
language-agnostic data-structures tree avl-tree red-black-tree
Apr 27 '13 at 23:02
source share
9 answers

AVL trees maintain a tighter balance than red-black trees. The path from the root to the deepest leaf in the AVL tree is no more than ~ 1.44 lg (n + 2), and in red black trees it is no more than ~ 2 lg (n + 1).

As a result, searching the AVL tree is usually faster, but this is due to the slower insertion and deletion due to the greater number of rotation operations. Therefore, use the AVL tree if you expect the number of searches to dominate the number of updates for the tree.

+90
Jul 23 '13 at 23:32
source share

For small data :

insert : the RB tree and the avl tree have a constant maximum rotation number, but the RB tree will be faster because on average the RB tree uses less rotation.

search : the AVL tree is faster because the AVL tree has less depth.

delete . The RB tree has a constant maximum rotation number, but the AVL tree can have O (log N) times the rotation as the worst. and on average, the RB tree also has fewer revolutions, so the RB tree is faster.

for big data :

insert : AVL tree is faster. because before inserting you need to find a specific node. since you have more data, the time difference when searching for a particular node grows proportionally to O (log N). but the AVL tree and RB tree still need a constant number of rotations in the worst case. Thus, the neck of the bottle will become the time when you look for this particular node.

Search : AVL tree is faster. (same as with small data)

delete : the AVL tree is on average faster, but in the worst case, the RB tree is faster. because you also need to look for a very deep node to swap before deleting (similar to the reason for insertion). on average, both trees have a constant number of rotations. but the RB tree has a constant upper bound for rotation.

+39
Mar 04 '15 at 4:11
source share

Quote from this: The difference between AVL and red-black trees

RB trees, as well as AVL trees, are self-balancing. Both provide O (log n) search and insertion performance. The difference is that RB trees guarantee O (1) rotations per insert operation. This is what performance in real implementations actually costs. Simplified, RB-trees get this advantage from the conceptual creation of 2-3 trees that do not carry the overhead of dynamic node structures. Physically, RB trees are implemented as binary trees, red / black flags mimic 2-3 behavior.

by definition, each AVL is, therefore, a subset of Red-Black. You need to be able to color any AVL tree without restructuring or rotation in order to turn it into a Red-Black tree.

+8
Apr 27 '13 at 23:06
source share

Maximum tree height is paramount for maintaining balance. It is almost equal to 1.44 * log(n) for AVL, but for the RB tree it is 2 * log(n) . Thus, we can conclude that it is better to use AVL when the problem is an incentive to search. Another question is important for the AVL and RB tree. The RB tree is better than AVL when it encounters random insertion at a lower cost of rotation, but an AVL that is good for inserting upstream or downstream data. Therefore, if the problem is the incentive for implementation, we can use the RB tree.

+3
Jun 15 '14 at 4:00
source share

AVL trees are often compared to red-black trees because they support the same set of operations and take O(log n) time for basic operations. For search-intensive applications, AVL trees are faster than red-black trees because they are more tightly balanced. Like red-black trees, AVL trees are balanced in height. Both of them, as a rule, are not balanced by weight, but μ-balanced for any μ ≤ ½; that is, sibling nodes can have an extremely different number of descendants.

From Wikipedia article on AVL Trees

+2
Dec 07 '13 at 22:33
source share

From what I saw, it seems that the AVL trees perform as many turns (sometimes recursively up the tree) as needed to get the desired AVL tree height (Log n). This makes it more tightly balanced.

For red black trees, there are 5 sets of rules necessary for you to remain in insert and delete mode, which you can find here http://en.wikipedia.org/wiki/Red-black_tree .

The main thing that can help you for red black trees is that, depending on these five rules, you can recursively color the tree to the root if the uncle is red. If Uncle is black, you will need to make a maximum of two turns to fix any problems that you have, but after these two or two turns YOU ARE MADE. Pack it and say good night, because this is the end of the manipulation you need to do.

The big rule is number 5 ... "Each simple path from a given node to any of its descendants leaves the same number of black nodes."

This will cause most of the twists you need for the tree to work, and this causes the tree to not go too far from the balance.

+1
Jul 23 '13 at 22:56
source share

In short: AvlTrees are a little better balanced than RedBlackTrees. Both trees take O (log n) the total time to search, insert, and delete, but to insert and delete the former requires O (log n) rotations, while the latter only accepts O (1) rotations.

Because rotations mean writing to memory, and writing to memory is expensive, RedBlackTrees in practice updates faster than AvlTrees

+1
May 11 '15 at 17:18
source share

The fact that RedBlack trees have fewer revolutions can make them faster when inserting / deleting, however ..... Because they are usually a bit deeper, they can also be slower when inserting and deleting. Each time you move from one level to a tree to the next, there is a big change that the requested information is not in the cache and must be extracted from RAM. Thus, the time spent on a smaller number of revolutions can already be lost, since it must move deeper and, therefore, it is necessary to update its cache more often. The ability to work with the cache or not is of great importance. If the relevant information is in the cache, then you can perform several rotation operations during the time required to move to an additional level, and the information of the next level is not in the cache. Thus, in cases where RedBlack develops faster, looking only at the necessary operations, in practice this can be slower due to misses in the cache.

+1
Jan 08 '16 at 15:25
source share

To get an idea of ​​how the AVL tree works, this> interactive visualization helps.

AVL, as well as RedBlack trees, are height-balanced tree data structures. They are very similar, and the real difference lies in the number of rotation operations performed during any add / delete operation - higher in the case of AVL, in order to maintain an overall more uniform balance.

Both implementations scale as O(lg N) , where N is the number of leaves, but in practice the AVL tree works faster with intensive search tasks: taking advantage of better balancing, tree walks are shorter on average. On the other hand, inserting and deleting is wise, the AVL tree is slower: it requires a higher number of revolutions for the correct correction of the data structure during modification.

For general-purpose implementations (that is, it is a-priori unclear whether searches are predominant for operations), RedBlack Trees are preferred: they are easier to implement and faster in normal cases - wherever the data structure changes as often in the search. For example, TreeMap and TreeSet in Java use the RedBlack background tree.

0
Dec 17 '15 at
source share



All Articles