Balanced binary search Tree--avl tree

Source: Internet
Author: User

The binary lookup tree may be N-1 in the worst case, that is, the element inserted after inserting the element is always larger or smaller than the previously inserted element. To solve this imbalance, the equilibrium condition was introduced to limit the depth of the nodes in the tree, the oldest of which is called the AVL tree. This tree limits the height of the left and right subtrees of each node in the tree to no more than one. (another stricter tree limits the height of the left and right subtree of a node must be equal, but such a tree requires that the number of nodes in the tree is 2 K power minus 1, is an ideal balance tree, but the requirements are too strict to be used in practice.) )

Analysis of the balance condition of AVL tree

The AVL tree is a special two-fork look-up tree, while the operation of the AVL tree, except that the insert operation differs from the normal binary lookup tree, the other operations on the AVL tree are the same as for the normal binary search tree. The insertion operation of the AVL tree differs from the normal binary search tree in that it is necessary to determine whether the new tree satisfies the equilibrium condition of the AVL tree after inserting the new element (this operation is the normal two-fork lookup tree insert operation), and if the balance condition is not satisfied, the new tree is adjusted to meet the equilibrium condition. The starting point for the adjustment is at the node where the first (that is, the deepest node) destroys the equilibrium condition. It can be proved that the whole tree satisfies the equilibrium condition of the balance tree, as long as the sub-tree which is rooted in this node is adjusted to satisfy the equilibrium condition of the balance tree.

When a new element is inserted, it can be divided into four cases where the new tree does not meet the balance condition. Set A to the first node that does not meet the equilibrium condition after inserting the new element (that is, the deepest node that does not meet the height difference of the left and right sub-trees is not greater than 1), on this basis describes the four cases:

(1) One-time insertion of the left subtree of a's left sub-tree

(2) One-time insertion of the right subtree of the left subtree of a

(3) One-time insertion of the left subtree of the right subtree of a

(4) One-time insertion of the right subtree of the right sub-tree of a

where conditions (1) and (4) are logically the same, (2) and (3) are the same situation. But the programming requires different processing (the same idea). Where single rotation is used on (1) and (4), a double rotation (i.e. two single rotation) is used for (2) and (3) processing. The main idea of rotation is to reduce the depth of a and make it as a node of a tree in the root of a (the new root node is different), thereby reducing the depth of the subtrees tree, which is higher in the subtree with a root, and increasing the depth of the original lower subtree, so that it satisfies the equilibrium condition of the equilibrium tree.

AVL Tree Basic Routines

The basic structure of the AVL tree node is not much different from the normal two-fork lookup tree node structure, the only difference being that there is a high level of information:

struct Avlnode

{

ElementType Element;

Avltree left;

Avltree right;

int Height;

} ;

In addition, the AVL tree has a routine for calculating the height of the node, as follows:

static int Height (Position P)

{

if (p==null)

return-1;

Else

Return p->height;

}

Single rotation

In the case of single rotation processing (1) and (4), the main idea of a single rotation is to Zuozi the first unbalanced node (or right subtree, depending on the left or right single-spin. Case (1) is Zodan, the case (4) is the root node of the right paddle) as the new root node of the unbalanced tree, the original root node as the root node of the right subtree (or Zuozi) of the new root node. At the same time, the original right subtree (or Zuozi) of the new root node is the Zuozi (or right subtree) of the original non-equilibrium tree node. Finally, consider returning the new root node of the unbalanced tree to the root of one of the subtrees trees of the previous layer, which is handled by the recursive mechanism of the insert routine. The routines are as follows:

K2 is an unbalanced tree and is the first unbalanced node

Static Position Singlerotatewithleft (Position K2)

{

Postion K1;

k1=k2->left;

k1->right=k2;

k2->left=k1->right;

K2->height=max (height (k2->left), height (k2->right)) +1;

K1->height=max (Height (k1->left), k2->height) +1;

Return K1;

}

Double rotation

Double rotation processing (2) and (3), at this time using a single rotation does not reduce the depth of the height of the subtree, the reason is higher that the subtrees tree will be reduced to the depth of the original tree root of the subtree, at this time with the same depth of the tree before the rotation. The solution is to use a single rotation of two times to first right (left) a single rotation of the Left (right) subtree of the unbalanced node, to raise the root of the Gaozi (right (left) subtree of the left (right) subtree of the unbalanced node, and to reduce the depth of Gaozi. Then apply a left (right) single rotation to the unbalanced node, and the nodes that will undergo a single rotation and become the root of the sub-tree of the unbalanced node continue to ascend upward, so that after two rotations, the unbalanced tree can be re-balanced. The routines are as follows:

K3 is the first unbalanced node

Static postion Doublerotatewithleft (Position K3)

{

K3->left=singelrotatewithright (K3->left);

Return Singlerotatewithleft (K3);

}

Insert operation

The AVL tree remains balanced when inserted, and whenever a new node is inserted, it is determined whether the tree remains balanced and if unbalanced the first unbalanced node is rotated, and the insert routine selects the specific rotation based on how the new node is inserted. To put it simply, the AVL tree insert operation is based on the common binary search tree insert operation, which is to determine whether the operation that satisfies the equilibrium condition is performed and the operation is rotated without satisfying the equilibrium condition. Two The routines are as follows:

Avltree Insert (ElementType x,avltree T)

{

if (t==null)

{

T=malloc (sizeof (struct avlnode));

If (T==null)

Fataerror ("Out of space!!!");

Else

{

t->element=x;

t->height=0;

t->left=t->right=null;

}

}

else if (x<t->element)

{

T->left=insert (X,t->left);

Maintain a balanced operation

If (Height (t->left)-height (t->right) ==2)

{

if (x<t->left->element)

T=singlerotatewithleft (T);

Else

T=doublerotatewihtright (T);

}

}

else if (x>e->element)

{//The opposite action when inserting Zuozi ...}

T->heihgt=max (height (t->left), height (t->rigth)) +1;

Return T;

}

Balanced binary search Tree--avl tree

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.