Test site: Balance Binary Tree

Source: Internet
Author: User

Balanced Binary Tree

 

The balanced binarytree was first proposed by Adelson-velskii and Landis in 1962. Therefore, it is also called the AVL Tree.

 

I. Basic Introduction to balanced Binary Trees

 

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.

Search, insert, and delete are both O (log n) in average and worst cases ). Adding or deleting a tree may require one or more tree rotations to rebalance the tree.

For the Balance Tree and non-Balance Tree:


 

(Figure 1 shows a non-balanced binary tree on the left, and Figure 1 shows a balanced binary tree on the right)

 

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 (right turn)

 

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.

Figure 2 (1)

Figure 2 (2)

 

(2) rr balanced rotation (left turn)

 

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.

Figure 3 (1)

 

Figure 3 (2)

 

(3) LR balanced rotation (first left and then right)

 

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.

Figure 4 (1)

As shown in figure 4, the circle is first adjusted to the Balance Tree, then it is connected to the left Tree of A as the root node, then it becomes the LL type, and then processed as the balance type according to the LL type.

Figure 4 (2)

 

(4) RL balanced rotation (right first and left)

 

The F node is inserted in the left subtree of the right Child C, so that 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.

Figure 5 (1)

As shown in figure 5, the circle is first adjusted to the Balance Tree, and then it is connected to the left Tree of A as the root node, which becomes the RR type and then processed as the balance type according to the RR type.

Figure 5 (2)

(The above four types of nodes contain all the situations that lead to imbalance during insertion. As shown in the figure, only the minimum imbalance subtree generated after the beam tree is inserted)

 

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. 8

 

For example, an imbalance occurs when a node is inserted in a balanced binary tree. The minimum unbalanced node is a, and the balance factor of the left child of a is-1, if the balance factor of the right child is 0, the method for adjusting the balance is ()

A. ll Type B. lr type

C. RL type D. RR type

 

Solution: see Figure 4 (1). Select B as the answer.

(For personal understanding, please give me some advice if any errors exist)

Test site: Balance Binary 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.