Linux kernel to red-black tree and AVL tree

Source: Internet
Author: User

Why did Linux use the AVL tree earlier and then prefer the red-black tree?
In fact, this is a result of the usefulness of the red and black trees, this essay is still a metaphysical point of view. Red and black trees can be exported directly from 2-3 trees. We can not mention the red and black trees, but only 2-3 trees. Since the operation of the 2-3 tree is too simple. In addition, whatever the operation and characteristics of the red-black tree can be mapped to 2-3 trees. So the comparison between the red and black and AVL trees is more than 2-3 trees and AVL trees.


What's the difference between the two of them? The balance of the 2-3 tree is perfectly balanced. But the number of branches can be 3, and the AVL tree almost points to the perfect balance of the standard binary tree, it only agreed that the height of the sub-tree is the difference of up to 1. Thus, 2-3 trees are more balanced than AVL trees, but 2-3 trees are converted to two forks. That is, when the red black tree, it can no longer maintain the perfect balance, because the three fork node to cut out a red node, so that the subtree height plus 1, so that the red and black trees in strict sense there is no AVL tree balance!
The AVL tree maintains its "close balance" every time it is inserted and deleted. The red and black tree is only required to not disturb the black node can be, in terms of 2-3 trees, it is after all sacrificed the standard characteristics of the two fork tree to maintain the balance of the three-fork tree. Visible, the red-black tree's insert/delete overhead is much smaller than the AVL tree, for query overhead. Theoretically, more balanced AVL trees are better than red-black trees (due to the 2-3 trees. You need to compare 2 times when encountering a three-prong node. However, the red black tree twice times the height of the unbalanced state is a small probability event!

So for normal situations, you can think that the query cost of the AVL tree and the red-black tree is the same, in short, the red-black tree is better than the AVL tree in general.


The AVL tree is ideal, and the data structure in the Linux kernel. In particular, the virtual memory management module, specifically the CFS Scheduler's task pair columns, are frequently inserted and deleted. So I chose the red and black tree instead of the AVL tree.

Linux kernel to red-black tree and AVL tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.