A simple version of a binary tree

foreword

      The red-black tree is a rather troublesome name for many people. When searching for information on the Internet, it is rare to explain the source of its evolution. Most of them will give you five definitions as soon as they come up. The distance between red, black, and the root node Equal and so on, and then start to rotate, insert, delete these operations. After one operation, even how the red and black came from, what they mean, and what effect they have are all in the dark. It's weird to be able to figure it out.

      This article introduces the red-black tree. It does not involve any code for the time being. It just helps you understand the evolution source of the red-black tree and the specific meaning of red and black in the tree structure. After you understand it, it will be much clearer to look at what is rotated and inserted. . In other words, understanding what this article is about to describe is the basis for understanding red-black trees from the code level.

      Before starting, I still implore you to be patient and read it step by step carefully, if you are impetuous, you really can't do anything.

text     

      The origin of the red-black tree is naturally a binary search tree. This tree structure starts from the root node, the left child node is smaller than it, and the right child node is greater than it. Every node conforms to this property, so it is easy to find, and it is a good data structure. But it has a problem, that is, it is easy to be biased to one side, which is like a linked list structure, losing the advantages of the tree structure, and the search time will be bad.

      So we all want the tree structure to be chunky, like this:


      instead of something like this:

     Under this demand, the concept of balanced tree came into being.

      The red-black tree is a kind of balanced tree. It can ensure that the binary tree basically conforms to the short and fat structure, but before understanding the red-black tree, you must first understand another kind of tree, called the 2-3 tree. The logic behind the red-black tree is it.

    Well, let's look at 2-3 trees.

    2-3 tree is a variant of binary search tree. 2 and 3 in the tree represent two kinds of nodes, which are represented as 2-node and 3-node below.

    2-Node is a normal node: contains one element and two child links.

    3-node is an extended version, containing 2 elements and three links: two elements A, B, the left link points to a node less than A, the middle link points to a node between A and B values, the right link Points to nodes greater than B.

2-Node:              3-Node: 

                    

      With the cooperation of these two kinds of nodes, the 2-3 tree can ensure that the distance from any leaf node to the root node is the same in the process of inserting values. The goal of being chunky and chunky was fully achieved. How to cooperate? Let's look at the construction process of the 2-3 tree.

      The so-called construction is the insertion of a node-by-node from scratch.

      In a binary search tree, the insertion process starts from the root node, and if the value of the node is smaller than the value of the node, it continues to compare with the left child node, and if it is greater than the value of the node, it continues to compare with the right child node, until the left or right child node of a node is empty, and the value is inserted. go in. This cannot avoid the bias problem. In a 2-3 tree, the insertion process is like this.

      If the value is inserted into a 2-node, the 2-node is expanded into a 3-node.

      If the value is inserted into a 3-node, it is divided into the following cases.

      (1).3-The node has no parent node, that is, the whole tree has only one three nodes. At this point, the 3-node is expanded into a 4-node, a node containing three elements, and then decomposed into a binary tree.


      At this point, the binary tree remains balanced.

      (2). The 3-node has a 2-node parent node. The operation at this time is to expand the 3-node into a 4-node, then decompose the 4-node, and then integrate the parent node of the decomposed new tree into the 2-node. -The node goes to the parent node.


      (3). The 3-node has a 3-node parent node. At this time, the operation is: the 3-node is expanded into a 4-node, and then the 4-node is decomposed. The new tree parent node is merged upward, and the above 3-node continues to expand. , fusion, decomposition, the new tree continues to fuse upward until the parent node is a 2-node, if the root node is 3-node up to the root node, expand the root node to 4-node, and then decompose into a new tree, so far, the whole tree Add a layer and still keep the balance.

      The third case is a little more complicated. In order to facilitate intuitive understanding, now we will build a 2-3 tree from scratch, including all the above cases. After reading all the steps, you can also draw a picture yourself.

      We insert the values ​​in {7,8,9,10,11,12} into the 2-3 tree in turn, and draw its process:


      所以,2-3树的设计完全可以保证二叉树保持矮矮胖胖的状态,保持其性能良好。但是,将这种直白的表述写成代码实现起来并不方便,因为要处理的情况太多。这样需要维护两种不同类型的节点,将链接和其他信息从一个节点复制到另一个节点,将节点从一种类型转换为另一种类型等等。

      因此,红黑树出现了,红黑树的背后逻辑就是2-3树的逻辑,但是由于用红黑作为标记这个小技巧,最后实现的代码量并不大。(但是,要直接理解这些代码是如何工作的以及背后的道理,就比较困难了。所以你一定要理解它的演化过程,才能真正的理解红黑树)

      我们来看看红黑树和2-3树的关联,首先,最台面上的问题,红和黑的含义。红黑树中,所有的节点都是标准的2-节点,为了体现出3-节点,这里将3-节点的两个元素用左斜红色的链接连接起来,即连接了两个2-节点来表示一个3-节点。这里红色节点标记就代表指向其的链接是红链接,黑色标记的节点就是普通的节点。所以才会有那样一条定义,叫“从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点”,因为红色节点是可以与其父节点合并为一个3-节点的,红黑树实现的其实是一个完美的黑色平衡,如果你将红黑树中所有的红色链接放平,那么它所有的叶子节点到根节点的距离都是相同的。所以它并不是一个严格的平衡二叉树,但是它的综合性能已经很优秀了。

      借一张别人的图来看:


红链接放平:


      所以,红黑树的另一种定义是满足下列条件的二叉查找树:
⑴红链接均为左链接。
⑵没有任何一个结点同时和两条红链接相连。(这样会出现4-节点)
⑶该树是完美黑色平衡的,即任意空链接到根结点的路径上的黑链接数量相同。

      理解了这个过程以后,再去看红黑树的各种严格定义,以及其插入,删除还有旋转等操作,相信你脑子里的思路会清晰得多的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691215&siteId=291194637