The transformation of binary sort tree and balanced binary sorting tree

Source: Internet
Author: User

Binary sorting tree and the transformation of the balanced binary tree start to see the video alone when the feeling is clear, but see the problem is at a loss, if one-way rotation can also, encounter LR type and RL type is difficult, so be sure to write a blog to understand it. This blog will first introduce the binary sorting tree, and then introduce what is a balanced binary tree, why there will be a balanced binary sorting tree, finally see transformation.


1. What is a two-fork sort tree?

Binary sort tree is also known as a binary tree, which can be an empty tree, or to meet the following conditions:

(1) The tree of the left and right is a tree of search.

(2) If there is a left dial hand tree, the values of each node of the left subtree are smaller than the value of the root node.

(3) If there is a right subtree, the value of each node on the right subtree is greater than the value of the root node.


In short, its Zuzi Shubigen node is small, and the right subtree is larger than the root node.


2. What is a balanced binary tree?

A balanced binary tree is either an empty tree, or a tree: the depth of the left and right subtrees of any node of a tree is not more than 1

If the balance of the defined node is the depth of its right subtree minus the depth of its left subtree, then for a balanced binary tree, it has a balance of -1,0,1 three values for each node.


3. Why is there a balanced binary tree?

We know that the binary sort tree is also called find binary tree, balanced binary tree is a sort of binary tree, so is a balanced binary sorting tree, is used to find, look at the following picture, this picture is a soft test video:




If you look for 1, the first figure will look 6 times, the second figure will only need 4 times, so the less depth difference, the less the number of lookups. In order to improve the efficiency of the search, there is a balanced binary tree


4. Transformation of two-fork sort tree and balanced binary sorting tree

Let's look at several ways to convert:


(1) ll-balance rotation (single right-balance treatment)




(2) RR balance rotation (unidirectional left-hand balance treatment)



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



(4) RL type balanced rotation (bidirectional rotation, first right rear left)



From the above example we can easily understand, do the problem when we find the corresponding type to rotate, each rotation 90 degrees.


Example:

Here is a self-test question, with this titled example, to rotate, first look at the original image:


First of all, the type is LR type, first left and right, if you do not know where to place the sub-node can think about the characteristics of the binary sorting tree, you know how to put the number.



The above figure is the process of rotation, do not know that people understand that there is no


Summary:

Look at the theory that it is understood that the real problem is blurred, which mastered a number of concepts, thinking about the characteristics of these trees, using theory to guide the practice, the use of these theories to implement the discovery is actually very simple. Originally not familiar with it, so write this blog to summarize, after summing up will be, haha ~



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.