True understanding of the red and black trees (a large amount of data structures used in the Linux kernel are often asked by the second party)

Source: Internet
Author: User

True understanding of the red and black trees (a large amount of data structures used in the Linux kernel are often asked by the second party)
As a data structure, the red and black trees are not simple, because various propaganda makes it too mysterious. I have collected a lot of articles about the red and black trees on the Internet, which are similar, introducing concepts and analyzing performance, paste the code and give it a sin sentence, how is the worst case...

Unfortunately, the task was not completed, but we finally raised two questions. If we solve these two problems, all the problems will be solved.

To solve these two problems, it will undoubtedly involve the parent node of node P and the upstream node. There are two possibilities:
Probability 1: P's parent node PP is a binary Node


Problem 2 is solved.

Possibility 2: P's parent node PP is a three-way Node


Finally, we found that in the recursion process, we either encountered P .. P is a binary node. In this case, the value of the current node is directly referred to P according to the solution of problem 2... in P, the Child tree is reduced to a height, offset the increased height, maintain the balance, and end recursively, or recursively reach the root node. At this time, only one split operation is needed to end the process perfectly!



As you can see, the red node is derived from 2-3 trees. To maintain a binary tree instead of 2-3 trees, you must change the three-way node to a binary node. This is a return of width and height, that is, the height is changed to the width. Of course, the price is no longer the perfect balance.

Based on the above transformation, try it by yourself. Can two consecutive red nodes be generated? NO! Are you still struggling with the concept of the red/black tree? After reading its evolution, you will find that the complex concepts of many red and black trees and the performance that people don't have a clue are natural. Next, let's take a look at what is the worst case.

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.