Balanced binary tree (AVL Tree)

Source: Internet
Author: User
Balanced Binary Tree: it is an empty tree, or a tree of the following nature: Its left and right subtree are both balanced binary trees, the absolute value of the depth difference between the left subtree and the right subtree cannot exceed 1.

The key to balancing a binary tree is how to maintain the balance of the entire tree when inserting the knot.

The following are four situations of imbalance:

(1) A new node is inserted into the left child tree of a left child of a balanced binary tree, so that the node is not balanced.

Ll type (left subtree of the left child)
Because the node F is inserted in the left Tree of the left child B of A, the balance factor of A is increased from 1 to 2, which is out of balance. Therefore, a clockwise rotation operation is required. This means that the Left Child B of A rotates to the right to replace a as the root node, and a rotates to the right child tree of B as the root node. The original right subtree of B is changed to the left subtree of.


/* return pointer to the new subtree root */AVLNode *single_rotate_ll(AVLNode *node) //LL{AVLNode *p = node->lchild;node->lchild = p->rchild;p->rchild = node;node->height = max(height(node->lchild), height(node->rchild)) + 1;p->height = max(height(p->lchild), height(p->rchild)) + 1;//p->height = max(height(p->lchild), node->height) + 1;return p;}

(2) A new node is inserted into the right subtree of the right child of a certain node of the balanced binary tree, so that the node is not balanced.

Rr balanced rotation (right subtree of the right child)

Because node F is inserted in the right tree of the right Child C of A, the balance factor of A is reduced from-1 to-2, and the balance is lost. Therefore, a counter-clockwise rotation operation is required. In this way, the right Child C of a rotates to the top left to replace a as the root node. A rotates to the bottom left to become the root node of the Left subtree of C. The original left subtree of C is changed to the right subtree of.


AVLNode *single_rotate_rr(AVLNode *node) //RR{AVLNode *p = node->rchild;node->rchild = p->lchild;p->lchild = node;node->height = max(height(node->lchild), height(node->rchild)) + 1;p->height = max(height(p->lchild), height(p->rchild)) + 1;//p->height = max(node->height, height(p->rchild)) + 1;return p;}

(3) A new node is inserted into the right subtree of the left child of a node of the balanced binary tree, so that the node is not balanced.

LR balanced rotation method (right subtree of left child)
Because the node F is inserted in the right tree of the left child B of A, the balance factor of A is increased from 1 to 2, which is out of balance. Therefore, you need to perform two rotation operations (clockwise first ). That is, the root node D of the right subtree of the left child B of node A is first rotated to the top left to the position of Node B, then, rotate the D node to the right to the position of node. That is, convert it into LL type first, and then process it according to LL type.


AVLNode *double_rotate_lr(AVLNode *node)//LR{node->lchild = single_rotate_rr(node->lchild);return single_rotate_ll(node);}

(4) A new node is inserted into the left subtree of the right child of a certain node of the balanced binary tree, so that the node is not balanced.

RL balanced rotation (left subtree of the right child)
Because node F is inserted in the left Tree of the right Child C of A, the balance factor of A is reduced from-1 to-2, and the balance is lost. Therefore, you need to perform two rotation operations (clockwise first and then counterclockwise ), that is, first, the root node D of the Left subtree of the right Child C of node A is rotated to the right to the position of node C, then, the D node is rotated to the upper left to the position of node. That is, it is first converted into the RR type, and then processed according to the RR type.


AVLNode *double_rotate_rl(AVLNode *node)//RL{node->rchild = single_rotate_ll(node->rchild);return single_rotate_rr(node);}
The structure of the AVL Tree is:

typedef struct AVLNode {int data;int height;struct AVLNode *lchild, *rchild;}AVLNode;
Calculate the height of the tree with the node as the root:
int height(AVLNode *node){if (node == NULL)return -1;elsereturn node->height;}
Now we can write the insert code for the AVL Tree:

Avlnode * insert (avlnode * root, int data) {If (root = NULL) {root = (avlnode *) malloc (sizeof (struct avlnode )); root-> DATA = data; root-> lchild = root-> rchild = NULL; root-> Height = 0;} else if (Data <root-> data) {root-> lchild = insert (root-> lchild, data); If (height (root-> lchild)-height (root-> rchild) = 2) // left subtree height-right subtree height {If (Data <root-> lchild-> data) // llroot = single_rotate_ll (Root); elseroot = double_rotate_lr (Root ); // LR} else if (data> root-> data) {root-> rchild = insert (root-> rchild, data); If (height (root-> rchild) -Height (root-> lchild) = 2) // right subtree height-left subtree height {If (Data <root-> rchild-> data) // rlroot = double_rotate_rl (Root); elseroot = single_rotate_rr (Root); // RR }}// else // {}// data is in the tree already, do nothingroot-> Height = max (height (root-> lchild), height (root-> rchild) + 1; return root ;}


Traverse AVL Tree in ascending order:

void inorder_traversal_tree(AVLNode *node){if (node != NULL){inorder_traversal_tree(node->lchild);printf("%d ", node->data);inorder_traversal_tree(node->rchild);}}


Next, traverse the nodes to release the tree:

void destory_tree(AVLNode*node){if (node != NULL){destory_tree(node->lchild);destory_tree(node->rchild);free(node), node = NULL;}}


Test code:

int main(){int arr[] = {3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9};AVLNode* T = NULL;for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)T = insert(T, arr[i]);inorder_traversal_tree(T);destory_tree(T);getchar();return 0;}

Refer to AVL Tree rotation.

Analysis of the rotation method after the inserted node of the balanced binary tree AVL

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.