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.