Balanced Binary Tree

Source: Internet
Author: User

Preface

The average time complexity of a binary sorting tree (also known as a binary search tree and a binary search tree) during search, insertion, and deletion operations is O (logn ), however, their worst-case time complexity is the same as the efficiency of O (N) and sequential search. The reason for this is that they sorted the keyword values, however, the tree shape is not restricted. That is, when the keyword sequence is ordered, this worst case (a one-side Binary Tree) occurs, and the introduction of a balanced binary tree can be effectively adjusted.

For example, for a keyword sequence (,), create a binary sorting tree as a graph (B), and change it to a balanced binary tree as a graph ()

A balanced binary tree is also called an AVL Tree. It is either an empty tree or its left and right trees are balanced binary trees. The absolute value of the height difference between the left and right words cannot exceed 1. The key and difficulty of a balanced binary tree is to adjust it to a balanced binary tree after inserting a node in a binary tree. There are four types of unbalanced Binary Trees: RR, ll, rl, and LR.

Four Types

(1) rr-unidirectional left-hand balance Processing

Insert node F on the right subtree e of right Child C of a. The balance factor of A is changed from-1 to-2, and the binary tree is out of balance. At this time, we need to perform a counter-clockwise rotation so that the upper-left rotation of C replaces a as the root node. The lower-left rotation of A is the left child of C, and the original left subtree of C is changed to the right subtree of.

 

(2) ll-type one-way right-hand balance Processing

Insert node F on the left subtree C of the left child B of A. The balance factor of A is changed from 1 to 2, and the binary tree is out of balance. At this time, you need to perform a clockwise rotation so that B's upper right rotation replaces a as the root node, and a's lower right is the right child of B, and B's original right subtree is changed to the left subtree of

(3) RL-bidirectional rotation, right first and left

Insert node F on the left subtree D of the right Child C of a. The balance factor of A is changed from-1 to-2, and the binary tree is out of balance. At this time, we need to perform two rotations and divide the binary tree into two parts: the first rotation is to rotate the upper right of the d node in the circle to replace the C node, and the lower right of the C node to the right child of D, change the entire binary tree to the RR type. Perform the second rotation according to the RR type adjustment method.

 

(4) LR type -- bidirectional rotation, first left and right

Insert node F on the right subtree D of the left child B of A. The balance factor of A is changed from 1 to 2, and the binary tree is out of balance. At this time, we need to rotate the binary tree twice and divide it into two parts: the first rotation is to rotate the upper left of the d node in the circle instead of the B node, and the lower left of the B node is changed to the left child of D, change the entire binary tree to LL type. The second rotation can be performed according to the LL type adjustment method.

 

When adjusting the sorting binary tree, we first look at the overall Type of the Binary Tree, determine the type, and then adjust it according to the corresponding balancing method. (Note: In the course of doing the question, you should not only look at the part but ignore the whole, and rotate the RR or ll binary tree twice, resulting in incorrect results)

Summary:

The adjustment of the balanced binary tree is very simple, but it is very cumbersome to describe it in words, and the picture will be clear and easy to understand. Other types of sorting binary trees can be converted from the above four types. The core of balancing is "rotation first". After mastering these four adjustment rules, the balanced binary tree can be understood almost.

 

 

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