Rudolf Bayer developed the red-black trees system back in the early 1970s. The name of this was given to her by L. Gimpas and R. Sedgwick.
What red-black trees
It is worth noting that they are one of the types of self-balancing binary trees that provide a countable height size from the number of nodes and produce primary and basic processes of the search tree in a short time. Such operations include joining, exclusion, and searching for a node. Balance is provided on the basis of the addition of the attribute of the applied designation of the node, color. This property is taken over by one of the probable concepts and is indicated by one of the mentioned colors.
The number of black units on a branch from the beginning (root) to the final (leaf) is called the black height of the tree.
The emergence of the term
Describing the self-balancing search tree in their work, the authors of the article probably did not even assume that they would become the founders of the new term. Nevertheless, fate decreed that only two colors were available in the printing house. They denoted each bit that joins the subsequent node.
Application
In computer science, red-black trees are used to form comparable data, which may include a variety of excerpts and parts of inscriptions or numbers.
You can create a red-black tree in Actionscript, Python, C ++ and almost any other programming language. It is very simple. The red-black Java tree is also quite widespread.
Features
Black and red trees are search trees in a binary coordinate system. In these systems, any node has a specific color value. It can take on one of the above designations. In addition to all the conditions applicable to binary trees, to the varieties we are considering, the following rules are also used:
- The color of the knot is exclusively one of the two above. There are no other options, this is also reflected in the name of the term.
- The root of the tree should always be painted black. Exceptions are possible, but this deviation from the rule adds the risk that the tree will self-balance.
- All leaves have a zero value (NIL) and are indicated in black.
- It must be ensured that the two offspring of each red parent node are black.
- Any light course from a particular node to any leaf-based child node provides exactly the same number of black structural units.
Sometimes red-black trees are interpreted as commonplace binary search trees. Their differences are determined only by the fact that instead of certain colored nodes, edges are colored in the above values.
Why choose red-black trees
Black and red trees are one of the most common variants of self-balancing binary search trees, which are most often accessed in practical terms.
What explains their popularity? Practice is lazy, and it's worth recognizing. Everything that is too cumbersome and difficult to use and at the same time gives a comparatively similar result when applying simpler methods, dies or goes into the background. This prevalence among the people of red-black trees is explained by the fact that they most often provide the optimal balance between the quality and level of balance and the intricacies of its maintenance.
For example, if we compare them with perfect trees in their balance, then a situation may arise when it is observed that the βidealβ representatives impose too irreconcilable requirements. And in the context of the implementation of actions of exclusion from a tree or a turn, too much time and effort is spent on stabilizing the balance necessary in a given situation.
The processes
The process of subtracting black and red binary trees is almost the same for all other binary search branches. This is true, since any black-red tree is one of the private versions of the classic binary search tree.
However, when working with them, one should take into account the high probability that the direct production of the action of including or excluding data can cause damage to the structure of black-red trees. A huge advantage is that for the reconstruction of properties, a relatively small number of actions, such as changing colors and often less than three turns of the tree, are necessary. In fact, all these operations do not take much time.
Itβs worth starting an action of inserting or including an element by incrementing the next node. This function is similar in all binary search trees. The next step is to colorize the node in red. The only difference can be considered as the fact that while inserting in the binary search tree the first thing we add is a sheet, then in black and red the latter do not carry any information. Therefore, instead of them, an internal node is added that takes on a red color and its two black descendants.
Our further actions are directly determined by the color of the adjacent nodes. The term "uncle" is used for them. Direct analogy with the family tree. Consequently:
- The characteristic that all leaves retain a black color should always be realized.
- The sequence that two derivatives of each red node retain a black color may be interrupted. But this happens only when adding a red node, when changing the color of black to red or when the whole tree is turned.
- Also note that the sequence from the node to the sheet, including the same number of black nodes, may be violated. This happens only when the black node is turned on, the red element changes to black, and also in the opposite situation, repainting black to red. The same can be done when turning the tree.
Having studied all of the above, it is easy to figure out how the search is carried out in a red-black tree.
An interpretation of such a simple concept as a tree is interesting, with a description of its color - red-black or black-brown. Now you are aware of this.