Introduction to the implementation and comparison-----algorithms of red-black and AVL trees

Source: Internet
Author: User

First, the problem description

Implement two of the 3 types of trees: red-black, AVL, treap

Second, the principle of the algorithm

(1) Red and black trees

A red-black tree is a binary lookup tree, but adds a storage bit to each node that represents the color of the node, which can be red or black. The red and black trees meet the following five properties:

1) Each knot is either red or black.

2) The root node is black.

3) Each leaf node is black.

4) If a junction is red, then its two sons are black

5) contains the same number of black nodes on all paths of each node to its descendant nodes

This experiment mainly implements the initialization, insertion and deletion of red and black trees. When the red and black trees are inserted and

The five properties of the red-black tree may be destroyed when the operation is deleted. In order to ensure the nature of red and black trees, we need to modify color and pointer structure. The modification of the pointer structure is done by a rotation operation. So here we first describe the rotation of the red and black trees.

A . Rotation Operation

The rotary operation is mainly divided into left-and right-handed operations. When a left-hand operation is performed on a node x, it is assumed that X's right child y is not nil[t], and that the left side is the axis of the X-to-y chain, so that y becomes the new root of the subtree, X becomes the left child of Y, and Y's child is the right child of X. Right-handed similar.

B . Insert Operation

There are two main steps to inserting a red-black tree.

A) First insert a red node in the form of a two-fork search tree;

b) The new two-fork tree is then repaired with color adjustment or rotation to meet the red

Five properties of the black tree.

The loop entry condition is that the parent node of the current node is red when an insert repair operation is made to the red and black tree.

That is, the nature of 4 destruction. The following three scenarios are primarily considered in the repair function:

Case 1: The parent node of the insertion node z is red, and the Uncle Z's node y is red

At this point, the parent node of the parent node of z must exist, and it must be a black node, otherwise it is not a red-black tree before it is inserted. The main idea of this situation is to modify the parent node of z and uncle Knot y color is black, Z's grandfather node color is changed to red, the purpose is to move red, and then the current node pointer to the grandfather node, loop repair operation

The 2:z parent node is red, and Z's uncle Knot y is black, and z is the right child

At this point, the parent node of Z of the current nodes is set to the new current node, and the new current node is left-handed. Turn case 2 into scenario 3.

The 3:z parent node is red, and Z's uncle Knot y is black, and Z is the left child

At this point, the parent node of z is modified to black, the grandfather node turns red, and the grandfather node is the fulcrum for right-handed operation. At this point there are no more than two consecutive red nodes in the row, and all the properties of the red and black trees are adjusted so that all the processing is complete.

C . Delete Operation

Similar to insertions, red-black tree removal operations are mainly two steps.

c) First delete a specified node in the form of a two-fork search tree;

D) The new two-fork tree is then repaired with color adjustment or rotation to meet the red

Five properties of the black tree.

When the red-black tree is removed and repaired, the loop entry condition is that the current node x is a non-root node and

And it is a double black knot point. The following four scenarios are primarily considered in the repair function:

Situation 1:x's brother Knot W is red

At this point, you need to change the parent node to red, the Brother node W changed to black. Then, for the parent node of the left-hand operation, at this time the red and black tree Properties 5 unchanged. Convert situation 1 to other situations.

The case of 2:x's brother W is black, and the two children of W are black

At this point, you need to remove a heavy black from the current node x and the sibling node W, so that X has only one heavy black and w is red. At the same time, in order to compensate for the removal of a heavy black, you need to add a black on the parent node of X, and the parent node of X as the new current node, re-enter the loop.

Situation 3:x's brother W is Black, W's left child is red, right child is black

At this point, the Brother node W changed to Red, Brother W's left child to change to black, and then the Brother node W as the fulcrum of the right-hand operation, and then re-enter the algorithm. Turn case 5 into scenario 6.

The case of 4:x's brother W is black, and W's right child is red

In this case, the sibling node W is changed to the color of the parent node of X, then the parent node of x is changed to black, the right child of the Brother node W is changed to black, and the parent node of X is the pivot point for the left-hand operation. At the end of the algorithm, all properties of the red and black tree are adjusted.

D . Calculates the black height corresponding to all paths of the root node to the leaf node .

When inserting or deleting a node from a red-black tree, a corresponding repair operation is performed in order to maintain the nature of the red-black tree. In order to be more intuitive to test the nature of the red and black tree after the repair operation is correct, in the results of this experiment will output the root node to all the path of the leaf node and output the number of all black nodes on the path. It is therefore necessary to implement an output path and calculate the corresponding black height method.

The main principle of this method is to use recursive thought to maintain an array of record path nodes within the function path[]. Each time a node is visited, it is first judged whether the node is a sentinel node. A sentinel indicates that the path has been scanned for the end. You need to output the path and output the corresponding black height. If the node is not a sentinel node, the node key value must be recorded in the path[] array at that point. At the same time, if the node is black, the variable value of the black height is added by 1. After these operations are done, the left and right subtrees of the node are called recursively once. The main code to implement the operation is as follows:

E . Structure of the print tree

After inserting or deleting the red-black tree, in order to be able to observe the characteristics of the current red-black tree more intuitively, this experiment will output the structure of the current red-black tree in the result. The main principle of the implementation of this operation is to use the structure of the tree, priority to print out the right subtree, and then print the root node value and the color of the node, and finally print out the left subtree, in the function through the space to control the final display effect. The main code is as follows:

(2) AVL tree

The AVL tree is a highly balanced two-fork lookup tree. It is either an empty tree, or a two-prong tree with the following properties: Its Saozi right subtree is a balanced binary tree, and the absolute value of the depth of the Saozi right subtree does not exceed 1. Each node in the AVL tree has a balance factor BF, which represents the height difference of the left and right subtrees of the node, that is, the height of the Zuozi minus the height of the right subtree. The balance factor of all nodes on the AVL tree can only be 1, 0, 1.

The key to implementing the AVL tree is to maintain the balance of the tree. It is possible to break the balance of the tree whenever an insert or delete operation occurs. So first check to see if the balance of the tree is broken, if the balance of the two-fork lookup tree is broken by inserting a node, find the unbalanced node closest to the insertion point and rotate the unbalanced node to the root subtree. Rotary operation is mainly divided into: ll type, RR type, LR type, RL type and so on.

A . Rotation Operation

A) LL type

Because the node is inserted on the left subtree of the Zogen Node B of a, the equilibrium factor of a is increased from 1 to 2 and the balance is lost. A right clockwise rotation is required. The left child B of a will rotate to the right to replace a as the root node, and a to the right down to the root node of the right subtree of B. and the right sub-tree of B turns into a's left subtree.

b) RR type

Because the node is inserted on the right sub-tree of the right subtree node B of a, the balance factor of a is increased from 1 to 2 and loses its balance. It is necessary to rotate the operation clockwise to Zoshun.

c) LR type

Due to the insertion of nodes on the right subtree of the Zogen Node B of a, the equilibrium factor of a is increased from 1 to 2 and the balance is lost. It is necessary to perform two rotation operations, first left-handed and then right-handed.

d) RL Type

Because the node is inserted on the left subtree of the right subtree node B of a, the balance factor of a is increased from 1 to-2 and loses its balance. It is necessary to perform two rotation operations, first left-handed and then right-handed.

B . Insert Operation

The recursive algorithm for inserting a new data element E on the AVL tree can be described as follows:

(1) If Bbst is an empty tree, a new node with a data element of E is inserted as the root node of BBST, and the depth of the tree is increased by 1;

(2) If the keyword of e is equal to the keyword of the root node of the BBST, it is not carried out;

(3) If the keyword of E is less than the key of the root node of BBST, and there is no node in the left subtree of Bbst and E has the same keyword, E is inserted into the left subtree of Bbst, and when the left subtree depth increases (+1) after insertion, it is processed in the following different cases respectively:

The equilibrium factor of the root node of a and Bbst is 1 (the depth of the right subtree is greater than the depth of the left subtree, then the equilibrium factor of the root node is changed to the depth of the 0,bbst;

The balance factor of the root node of B and Bbst is 0 (the depth of the left and right subtree is equal): the equilibrium factor of root node is changed to 1,bbst depth by 1;

C, Bbst the root node of the balance factor is 1 (the depth of the left subtree is greater than the depth of the right subtree): If the Bbst Zogen node balance factor is 1: one-way right-balance treatment, and after the right-handed processing, the root node and its right subtree node balance factor is changed to 0, the depth of the tree is unchanged;

(4) If the keyword of E is greater than the key of the root node of BBST, and there is no node in the right subtree of Bbst and E has the same keyword, then E is inserted in the right subtree of Bbst, and when the right subtree depth after insertion is increased (+1), it is dealt with in different situations respectively.

C . Delete Operation

Suppose the parent node of the deleted node x is Y, and if the left subtree of y is deleted, the balance factor of Y is reduced by 1, otherwise the balance factor of Y is BF plus 1. The value of the balance factor according to Y is divided into the following three cases:

Situation One:

If the new Balance factor bf of node y equals 0, then the balance factor of the first y is equal to 1 or-1, and the height is reduced by 1, and the balance factor of the parent node and other grandparent nodes needs to be changed.

Situation Two:

If the new Balance factor bf of node Y equals 1 or-1, then the balance factor of the first Y is equal to 0, and the left and right subtree height is the same, and the balance factor of the parent node and some other grandparent nodes needs to be changed.

Situation Three:

If the node y New Balance factor bf equals-2 or 2, it means that the balance factor 1 equals or 1 of the pre-deletion y, the height of the left and right subtree is not the same, and the tree is unbalanced at node Y, which needs to be treated in balance.

Third, the experimental data

(1) Input:

The data used in both the red and black tree experiments and the AVL tree experiment are 20 key values randomly generated. These keywords are then inserted into the appropriate tree using the Insert algorithm in turn. When the delete operation is performed, the deleted data is 3 randomly selected key values, then the corresponding nodes are deleted in turn.

(2) Output:

For red-black trees, after inserting and deleting, the structure of the corresponding tree is output, and the color of the node (red or black) is indicated, and then the black height of all paths of the root node to the leaf node is computed and the output is displayed in turn.

Iv. Source Code

Download Link: http://download.csdn.net/detail/zhh1992/8359301

Introduction to the implementation and comparison-----algorithms of red-black and AVL trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.