# Balanced binary tree for data structure (AVL)

Source: Internet
Author: User

I. Balanced Binary Tree features:
The balanced binary tree was first proposed by Adelson-velskii and Landis in 1962. It is also called the AVL Tree.
Definition: a balanced binary tree, an empty tree, or a binary sorting tree of the following properties:

(1) the absolute value of the difference between left and right subtree depth cannot exceed 1;
(2) Left and Right Subtrees are still balanced binary trees.

Balance factor: BF = left subtree depth-right subtree depth.
The balance factor of each node of a balanced binary tree can only be 1, 0,-1. If its absolute value exceeds 1, the binary sorting tree is unbalanced.

Ii. Balanced Binary Tree Algorithm

If a new node is inserted into a balanced binary tree, the balance of the balanced binary tree is damaged. First, you need to find the pointer to the minimum child root node that is not balanced after the new node is inserted. Then adjust the links between the relevant nodes in the subtree to make it a new balanced subtree. When the minimum subtree without balance is adjusted to the balance subtree, no adjustment is required for all other unbalanced subtree, and the entire binary tree becomes a balanced binary tree.

The minimum subtree with a loss of balance refers to the subtree with the node closest to the inserted node and the absolute value of the equilibrium factor greater than 1 as the root node. Assume that A is used to represent the root node of the minimum subtree that loses its balance, the operations to adjust the subtree can be summarized into the following four cases.

(1) ll-type balanced rotation method

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.

(2) rr balanced rotation method

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.

(3) LR balanced rotation method

Because node F is inserted on the right sub-number 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.

As shown in, that is, first adjust the circle to the Balance Tree, and then connect it to the left Tree of A as the root node. Then it becomes the LL type, and then it is processed as the balance type according to the LL type.

(4) RL balanced rotation method

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.

In, that is, first adjust the circle to the Balance Tree, and then connect it to the left Tree of A as the root node. Then it becomes the RR type, and then it is processed as the balance type according to the RR type.

Balancing relies on rotation. The three nodes involved in rotation (one of which may be the external node null) are rotated to convert the three nodes to different positions. Note that P-> right must not be empty when left-handed, and p-> left must not be empty when right-handed. This is obvious.

If you create an empty tree and keep the balance at all times, the imbalance only occurs in the insert/delete operation, the imbalance is indicated by the emergence of BF = 2 or BF =-2 nodes.

Refer:

One-step write balance binary tree (AVL Tree) http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html

Balance Binary Tree http://kimibob.blog.163.com/blog/static/163873051201153093851369/

Balanced binary tree for data structure (AVL)

Related Keywords:

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.