AVL trees and red-black tree

AVL trees and red-black

AVL tree

When the binary search tree only to find that balance efficiency will be high.

To maintain binary search tree of balance is not an easy task. However, there are some very classic way to do it, which is the best way to achieve binary search trees AVL tree .

AVL tree is named after its inventor GM Adelson-Velsky and EM Landis, they published it in 1962, the paper "An algorithm for the organization of information " in. AVL tree is a special type of binary tree, each node that are stored in an additional information: the node balance factor .

Balance factor nodes left subtree = height - height of the right subtree

Insert and delete operations will result in a self-alignment AVL tree (self-balancing), such that all nodes maintain balance factor +1, -1, or 0.

When the subtree rooted balance factor is +1, which is the left oblique to (left-heavy).

When the subtree rooted balance factor is -1, which is a right tilt of the (right-heavy).

A sub-root node of the tree represents the balance of the balance factor of the sub-tree .

Keep all sub-trees are almost in balance, AVL tree will be able to basically maintain a balance in general .

AVL tree basic look, insertion nodes as binary tree and the operation of the operation. However, when a node is inserted into the AVL tree, there are some additional work to do. First, the insertion must be calculated by changing the balance factor to bring the operation. Secondly, if any balance factor becomes ± 2, it is necessary to re-balance the old tree down starting from this node, this rebalancing process is called rotation .

Rotation AVL tree

To a rotating operation portion rebalance tree. By rearranging the nodes, the relationship between the nodes always left child node is less than the parent node, the parent node is less than the right child node . So that the tree is still a binary search tree. After the rotation, the rotation of all nodes in the subtree are the balance factor is +1, -1 or 0 .

AVL tree of rotary type four kinds, namely, LL (left-left) rotation, LR (left-right) of rotation, RR (right-right) and the rotation RL (right-left) rotation.

To facilitate understanding of rotation which when executed, provided x represents the newly inserted node in the AVL tree, A is set to change from the balance factor and x is the absolute value of the nearest ancestor 2 . Can be summarized in the following four treatments where:

LL rotation

As shown below, when the left subtree of the A x left subtree performs rotation LL .

A set of left left subtree , LL rotation to be performed, the left pointer A points to the left and right child nodes, the left and the right pointer points A, the original point A pointer to left .

After rotation, the balance factor A and are left to 0 . All other nodes of the balance factor is not changed.

 LR rotation

When the right subtree of the A x left subtree performs rotation LR.

Provided left node is the left child of A, and let A grandchild node descendants of the left and right child nodes.

To perform LR rotation, the left point to the right child node's left child grandchild nodes , grandchild point to the left child node left , left child node A is directed to the right child grandchild node , then the right child node grandchild point to point a , and finally a pointer to the original point a grandchild .

After performing rotation LR, the adjustment node balance factor depends on the primary balance factor value of the rotation before the grandchild nodes .

If the grandchild nodes of the original balance factor is +1, A balance factor will be set to -1, the left balance factor is set to zero.

If the grandchild nodes of the original balance factor is 0, and A will be left balance factor are set to 0.

If the grandchild nodes of the original balance factor is -1, A balance factor will be set to 0, the left balance factor is set to +1.

In all cases, grandchild of the new balance factor is 0. All other nodes of the balance factor has changed.

RR rotation

When the right subtree of x in the left subtree of A, the RR rotation.

RR rotation LL rotational symmetrical relationship.

A set of the right child node is Right. To perform RR is rotated to the left and the right pointer A points to the right child node, the right left pointer points A, the original point A to point right pointer modification .

After the completion of the rotation, the left balance factor A and 0 are modified . All other nodes of the balance factor has changed.

RL rotation

When the left subtree of the A x right subtree is performed RL rotation .

 RL and LR rotation is the rotation symmetrical relationship.

A set of the right child node as right, left to right child node grandchild. To perform RL rotation, the left child node of the right node point grandchild of the right child node, the grandchild's right child nodes pointing right, the right child node A is directed to grandchild left child node and the grandchild of point to the left child node of a, and finally the original pointer to point a grandchild.

After performing RL rotation, adjusting the node balance factor depends on the primary rotational balance factor before grandchild nodes. There are also three cases to consider:

If the balance factor grandchild original value of +1, the balance factor A is updated to 0, the update right is -1;

If the grandchild of the original balance factor value of 0, and the right balance factor A are updated to 0;

If the grandchild of the original balance factor is -1, the balance factor is updated to A + 1, the update right is 0;

In all cases, both the grandchild of a new balance factor is set to 0. All other nodes of the balance factor is not changed.

Red-black tree

Red-black tree is a self-balancing binary search tree is a data structure used in computer science, typical use is to realize an associative array. It is complicated, but it has a good worst-case operating run time, and is effective in practice: it can be found in O (logn) time, insertion and deletion, is where n-number of elements in the tree .

2-3-4 tree is a red-black tree equivalents. In other words, for each of the 2-3-4 tree, there is at least one element of the same order of the red-black tree. In the 2-3-4 tree insertion and deletion operations are also equivalent to the red-black in color flip and rotate the tree. This makes the 2-3-4 tree has become an important tool for understanding the logic behind the red-black tree.

With respect to the red-black tree AVL tree is sacrificed in exchange for part of the balance of insert / remove a small amount of rotational operation of the operation, overall better performance than AVL tree.

nature

(1) a non-red or black node

(2) when the root node black

(3) each leaf node (NULL node) is black

Two child nodes (4) of each red node are black (no two consecutive red nodes)

(5) from any node to all leaves of each path contains the same number of black nodes

Note: Nature (5) red-black tree to ensure the longest path is not more than twice the shortest path

 

 

Red-black tree and AVL tree (balanced binary tree) difference

AVL树(平衡二叉树)

(1)简介

AVL树是带有平衡条件的二叉查找树,一般通过平衡因子差值判断是否平衡并通过旋转来实现平衡,左右子树高不超过1,和红黑树相比,AVL树是严格的平衡二叉树,平衡条件必须满足(所有节点的左右子树高度差不超过1)。不管我们是执行插入还是删除操作,只要不满足上面的条件,就要通过旋转来保持平衡,而因为旋转非常耗时,由此我们可以知道AVL树适合用于插入与删除次数比较少,但查找多的情况

(2)局限性

由于维护这种高度平衡所付出的代价比从中获得的效率收益还大,故而实际的应用不多,更多的地方是用追求局部而不是非常严格整体平衡的红黑树。当然,如果应用场景中对插入删除不频繁,只是对查找要求较高,那么AVL还是较优于红黑树。

(3)应用

1.Windows NI内核中广泛存在;

红黑树

(1)简介

一种二叉查找树,但在每个节点增加一个存储位表示结点的颜色,可以是红或黑(非红即黑)。通过对任何一条从根到叶子的路径上各个节点着色的方法的限制,红黑树确保没有一条路会比其他路径长出两倍,因此红黑树是一种弱平衡二叉树(由于是弱平衡,可以看到,在相同的节点情况下,AVL树的高度低于红黑树),相对于要求严格的AVL树来说,它的旋转次数少,插入最多两次旋转,删除最多三次旋转,所以对于搜索、插入、删除操作比较多的情况下,我们就用红黑树。

(2)性质

(1)节点非红即黑

(2)根节点时黑色的

(3)每个叶子节点(NULL节点)是黑色的

(4)每个红色节点的两个子节点都是黑色的(不能有两连续的红色节点)

(5)从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点

注意:性质(5)保证红黑树的最长路径不超过最短路径的两倍

(3)应用

广泛应用于C++的STL中,map和set底层都是用红黑树实现的。

Guess you like

Origin www.cnblogs.com/kexinxin/p/11618273.html