Why should there be a red-black tree? What is the red-black tree? Drew 20 chart, after reading this you will understand

Why should there be a red-black tree

Surely everyone is familiar on the binary search tree, first look at the definition of a binary search tree:
binary search tree (Binary Search Tree), or an empty tree or a binary tree with the following properties: if its left child tree is not null, then the value of the left sub-tree, all the nodes are less than the value of the root node; if it is not empty right subtree, the right subtree of nodes which are all greater than its root value; its left and right sub-trees are binary sort tree.
In theory, the time of the query binary search tree, insert and delete a node complexity are O (log (n)), has been fully meet our requirements, then why there is a red-black trees?
We look at an example, the binary search tree insertion order (1,2,3,4,5,6), then insert like this
Degenerate into a linked list of binary search tree
can be seen, in this case, devolved into a binary search tree the list! ! ! This time query, insert, and delete an element, the time complexity becomes O (n), it is clearly unacceptable. The reason for this situation is the mechanism of binary search tree is not self-balanced, so there is the concept of balanced binary tree.
Balanced binary tree (Balanced Binary Tree) having the following properties: it is an empty tree height difference or absolute value of its left and right subtrees of no more than 1, and the left and right sub-trees are a balanced binary tree.
Or just an example, if we are to achieve a balanced binary tree, then, after you insert an element should look like this (not unique) below
Self-balancing binary tree
Balanced binary tree to ensure that in the worst case, a binary tree is still able to maintain absolute balance, that is, about two subtrees of absolute difference height of not more than one. But it will also bring a problem, that is the definition of balanced binary tree is too strict, resulting in after each insert or delete an element, should be to maintain the overall balance of the binary tree, which generate additional costs and too big. Binary search tree may degenerate into a linked list, and balanced binary tree to maintain the balance of the price and cost too much, then how to do it? It is necessary to talk about the wisdom of "moderation" in the. To put it plainly is to define the appropriate balance of relaxed, less stringent, so neither degenerate into a binary tree list, maintain the balance of the cost is acceptable. Yes, this is what we want to talk about the red-black tree. First, look at the definition of red-black tree:
red-black tree is a node and can contain red and black self-balancing binary search tree. In addition it must meet the nature of the binary search tree, but also meet the following properties:
== Property 1: Each node is either black or red.
Property 2: the root is black.
Property 3: each leaf (NIL) is black.
Property 4: two red sub-nodes each node necessarily black.
Nature 5: any node to a leaf node of each path contains the same number of black nodes. ==
This is the nature of five red-black tree. I believe many people have seen, there are also many can back down, but I'm afraid that not many really understand why such a definition. Here from the perspective of 2-3 tree to talk about the definition of red-black tree.

From 2-3 Tree view black tree

一般我们接触最多的是二叉树,也就是一个父节点最多有两个子节点。2-3树与二叉树的不同之处在于,一个父节点可以有两个子节点,也可以有三个子节点,并且其也满足类似二叉搜索树的性质。还有最重要的,2-3树的所有叶子节点都在同一层,且最后一层不能有空节点,类似于满二叉树。
我们依次插入10,9,8,7,6,5,4,3,2,1来看一下2-3树是如何进行自平衡的。
2-3树在插入元素之前首先要进行一次未命中的查找,然后将元素插入叶子节点中,之后再进行平衡操作,下面具体说明。
首先插入10,如下图
2-3 inserted in the tree 10
然后插入9,9小于10,2-3树在插入时要将9融入10这个叶子节点中(当然也是根节点),融合完成后如下:
9 is inserted into the tree 2-3
这是一个3节点,不用执行平衡操作。2-3树中把有两个元素,三个子节点的节点称为3节点,把有一个元素,两个子节点的的节点称为2节点。
接着插入8,插入8的时候同样要先融入叶子节点中,如下图左侧所示
2-3 8 is inserted into the tree
8融入叶子节点后,该结点便拥有了3个元素,不满足2-3树的定义,这时就要把3节点进行分裂,即把左侧和右侧的元素分裂为2节点,而中间的元素抽出,继续融入其父节点,在这里便成为了一个根节点。
继续插入7,如下
7 is inserted into the tree 2-3
插入后,各个节点都满足2-3树的定义,不需要进行平衡操作。
接着插入6,还是首先找到叶子节点,然后将其融入,如下图左侧所示
6 is inserted into the tree 2-3
插入后6、7、8三个元素所在的叶子节点不再满足2-3树的定义,需要进行分裂,即抽出元素7融入父节点,6和8分裂为7的左右子节点。
接着插入5,如下图所示,同样不需要进行平衡操作
2-3 inserted into the tree 5
接着插入4,还是首先找到叶子节点,然后将其融入,如下图左侧所示
2-3 inserted into the tree 4
插入后4、5、6三个元素所在的叶子节点不再满足2-3树的定义,需要进行分裂,即抽出元素5融入父节点,4和6分裂为5的左右子节点。5融入父节点后,该结点便有了5、7、9三个元素,因而需要继续分裂,元素7成为新的根节点,5和9成为7的左右子节点。
接着插入3,3融入4所在的叶子节点中,不需要进行平衡操作
2-3 inserted into the tree 3
接着插入2,还是首先找到叶子节点,然后将其融入,如下图左侧所示
2-3 inserted into the tree 2
插入后2、3、4三个元素所在的叶子节点不再满足2-3树的定义,需要进行分裂,即抽出元素3融入父节点,2和4分裂为3的左右子节点,3融入5所在的父节点中。
最后插入2,同样先找到叶子节点,然后将其融入,如下图所示
1 2-3 tree insert
至此,我们便完成了在2-3树中依次插入10,9,8,7,6,5,4,3,2,1,并且2-3树始终维护着平衡。怎么样,是不是很神奇。

再看红黑树

那么红黑树与2-3树有什么关系呢?现在我们对2-3树进行改造,改造成一个二叉树。怎么改造呢?对于2节点,保持不变;对于3节点,我们首先将3节点中左侧的元素标记为红色,如下图2所示。
2-3 red-black tree to tree transformation
然后我们将其改造成图3的形式;再将3节点的位于中间的子节点的父节点设置为父节点中那个红色的节点,如图4的所示;最后我们将图4的形式改为二叉树的样子,如图5所示。图5是不是很熟悉,没错,这就是我们常常提到的大名鼎鼎的红黑树了。
下面我们回过头再看下红黑树的五条性质。
From 2-3 tree look red-black tree
性质1:每个节点要么是黑色,要么是红色。
2-3树中存在2节点和3节点,3节点中左侧的元素便是红色节点,而其他的节点便是黑色节点。
性质2:根节点是黑色。
在2-3树中,根节点只能是2节点或者3节点,2节点与3节点在红黑树中的等价形式,如下图所示
2 and 3 equivalents node in a red-black tree node
显然,无论是哪种情况,根节点都是黑色的。
性质3:每个叶子节点(NIL)是黑色。
这里的叶子节点不是指左右子树为空的那个叶子节点,而是指节点不存在子节点或者为空节点被称作叶子节点。在性质2中我们讨论的根节点是黑色的都是讨论根节点不为空的情况,若红黑树是一个空树,那么根节点自然也是空的叶子节点,这时候叶子节点便必然是黑色的。
性质4:每个红色结点的两个子结点一定都是黑色。
还是从2-3树的角度来理解,红色节点对应2-3树中3节点左侧的元素,那么它的子节点要么是2节点,要么是3节点。无论是2节点还是3节点对应的节点颜色都是黑色的,这在性质2时已经讨论了。
性质5:任意一结点到每个叶子结点的路径都包含数量相同的黑结点。
性质5应该是红黑树最重要的一条性质了。2-3树是一颗绝对平衡的树,即2-3树中任意一个节点出发,到达叶子节点后所经过的节点数都是一样的。那么对应到红黑树呢?2-3树中2节点对应到红黑树便是一个黑色的节点,而3节点对应到红黑树是一个红色节点和一个黑色节点。所以,无论是2节点还是3节点,在红黑树中都会对应一个黑色节点。那么2-3树中的绝对平衡,在红黑树中自然就是任意一结点到每个叶子结点的路径都包含数量相同的黑结点了。
相信大家现在已经对红黑树的五条性质有了更加深刻的体会了。那么我们再看下红黑树维持平衡的三种操作,即变色、左旋、右旋怎么理解呢?
首先看一下变色,以下图为例,
Color red-black tree
在2-3树中插入节点3后,便不再满足2-3树的定义,需要进行分解,将元素2抽出作为1和3的父节点,然后2继续向上融合。
对应到红黑树中就是,首先插入节点3,在红黑树中新插入的节点默认为红色,然后不满足定义,所以需要进行分解,分解后各个节点都为2节点,所以变为黑色。而2节点需要继续向上融合,故要变成红色。
接着看一下右旋转,以下图为例,
Right red-black tree rotation
插入元素1后,进行右旋转操作,首先把2节点与3节点断开连接,同时把2与2的右子树断开连接,然后把2的右子树连接至3的左子树位置,不会违背二分搜索树的性质,然后再把3连接至2的右子树位置。最后还要改变对应节点的颜色,即把2节点的颜色改为原来3节点的黑色,把3节点的颜色改为原来2节点的红色。
接着看一下左旋转,与右旋转类似,以下图为例,
Left red-black tree rotation
Insert element 3, a left rotation operation, the first node 3 and node 2 is disconnected, while the left subtree of 3 and 3 is disconnected, then the left subtree is connected to the 3 position of the right subtree 2, without departing from the nature of the binary search tree, and then connected to the 2 position 3 of the left subtree. Finally, to change the color of the corresponding node, i.e. the node 2 changes to red color original node 3, node 3 to the black color original 2 nodes.

Written in the last

Finally, it should say is that red-black tree mentioned in this article is a special red-black tree - left-leaning red-black trees, red nodes are namely the parent node of the left subtree, in fact, do not have such in accordance with the definition of red-black tree . Five properties as long as the red-black tree is a red-black tree, for example, can achieve rightist red-black trees, etc., I hope you do not misunderstand.
More about the red-black trees, such as red-black tree insertion, deletion, space is limited, this is no longer introduced, interested recommend you read "algorithm 4" or "Introduction to Algorithms."
More details about the algorithm, data agencies and basic knowledge of computers, I welcome scan code we are concerned about the public number "super Wyatt programming."
Super Wyatt Programming

Guess you like

Origin www.cnblogs.com/exzlc/p/12185776.html