Data structure of AVL tree detailed _c language

Source: Internet
Author: User

1. Overview

AVL Tree is the earliest proposed self-balanced binary tree, in the AVL tree, any node in the two subtree height of the maximum difference is one, so it is also known as a highly balanced tree. The AVL tree derives its name from its inventor g.m Adelson-velsky and E.M. Landis. AVL tree lookups, inserts, and deletions are all O (log n) in both the average and worst-case scenarios, and the addition and deletion may require one or more tree rotations to rebalance the tree. This paper introduces the design idea and basic operation of AVL tree.

2. Basic terminology

There are four situations that may cause two fork lookup trees to be unbalanced, respectively:
(1) LL: Insert a new node to the root of the Zuozi (left) of Zuozi (left), resulting in the root node balance factor from 1 to 2
(2) RR: Inserts a new node to the right subtree (right) of the root node, causing the root node's balance factor to change from-1 to-2
(3) LR: Insert a new node to the root of the Zuozi (left) tree (right), resulting in the root node balance factor from 1 to 2
(4) RL: Inserts a new node to the root node's right subtree (left), resulting in the root node's balance factor from-1 to-2 Zuozi
The imbalances that may result from the four situations can be balanced by the rotation. There are two basic types of rotation:
(1) Left rotation: Rotate the root node to (the root node) the left child position of the right child
(2) Right rotation: Rotate the root node to (the root node) the right child's position on the left child

3. The rotation operation of AVL tree

The basic operation of the AVL tree is rotation, there are four kinds of rotation, respectively: left rotation, right rotation, rotation around (first left and right), right left rotation (first right after the left), in fact, these four kinds of rotation operation 22 symmetrical, so can also be said to be two types of rotation operation.
The basic data structure:

Copy Code code as follows:

typedef struct NODE* tree;
typedef struct NODE* node_t;
typedef Type INT;

struct node{
Node_t left;
node_t right;
int height;
Type data;
};
int Height (node_t Node) {
Return node->height;
}

3.1 LL

ll situation requires a right rotation resolution, as shown in the following figure:


The code is:

Copy Code code as follows:

node_t rightrotate (node_t a) {
b = a->left;
A->left = b->right;
B->right = A;
A->height = Max (height (a->left), height (a->right));
B->height = Max (height (b->left), height (b->right));
return b;
}

3.2 RR
The RR situation requires a left-spin resolution, as shown in the following illustration:

The code is:

Copy Code code as follows:

node_t leftrotate (node_t a) {
b = a->right;
A->right = b->left;
B->left = A;
A->height = Max (height (a->left), height (a->right));
B->height = Max (height (b->left), height (b->right));
return b;
}

3.3 LR

LR condition needs to be left or right (first B to rotate, and then a to rotate) to solve, as shown in the following figure:

The code is:

Copy Code code as follows:

node_t leftrightrotate (node_t a) {
A->left = Leftrotate (a->left);
Return Rightrotate (a);
}

3.4 RL

RL situation requires a right left hand to solve (first B right rotation, after a left rotation), as shown in the following figure:

The code is:

Copy Code code as follows:

node_t rightleftrotate (node_t a) {
A->right = Rightrotate (a->right);
Return Leftrotate (a);
}

4. The insert and delete operation of the AVL number

(1) Insert operation: In fact, in different situations using different rotation way to adjust the whole tree, the specific code is as follows:

Copy Code code as follows:

node_t Insert (Type x, tree t) {
if (t = = NULL) {
t = NewNode (x);
else if (x < T->data) {
T->left = Insert (t->left);
if (height (t->left)-height (t->right) = = 2) {
if (x < T->left->data) {
t = rightrotate (t);
} else {
t = leftrightrotate (t);
}
}
} else {
T->right = Insert (t->right);
if (height (t->right)-height (t->left) = = 2) {
if (x > T->right->data) {
t = leftrotate (t);
} else {
t = rightleftrotate (t);
}
}
}
T->height = Max (height (t->left), height (t->right)) + 1;
return t;
}

(2) Delete operation: First locate the node to be deleted, then replace the node with the left child of the right child of the node, and readjust the subtree with the root of the node as the AVL tree, which is similar to the insertion data, as follows:

Copy Code code as follows:

node_t Delete (Type x, tree t) {
if (t = = null) return null;
if (T->data = = x) {
if (t->right = = NULL) {
node_t temp = t;
t = t->left;
Free (temp);
} else {
node_t head = t->right;
while (Head->left) {
Head = head->left;
}
T->data = head->data; Just copy data
T->right = Delete (T->data, t->right);
T->height = Max (height (t->left), height (t->right)) + 1;
}
return t;
else if (T->data < x) {
Delete (x, t->right);
if (t->right) Rotate (x, t->right);
} else {
Delete (x, T->left);
if (t->left) Rotate (x, T->left);
}
if (t) Rotate (x, T);
}

5. Summary

AVL Tree is the earliest self-balanced binary tree, compared with the later balanced Binary tree (red-black tree, Treap,splay tree), it is now less used, but the study of AVL tree is important for understanding the commonly used balanced binary tree.

6. Reference materials

(1) Data structure (C language Edition) Min, Wu Weimin
(2) http://zh.wikipedia.org/wiki/AVL%E6%A0%91

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.