Graphic balance binary tree, AVL tree (i)

Source: Internet
Author: User

Graphic balance binary tree, AVL tree (i)

After learning two forks to find the tree, we must have encountered a problem. For example, when inserting an array {1,2,3,4} into the tree sequentially, the case of Figure 1 is formed. There is no building tree and no tree for data additions and deletions have no help, but added the cost of maintenance. and only the establishment of the tree 2, can best reflect the advantages of the binary tree.

In the above example, figure 2 is a balanced binary tree. Scientists have proposed a balanced binary tree, which is to maximize the performance of the tree's search (at least I understand it, and welcome criticism to correct it). The following goes to today's topic, balancing the binary tree.

Definition of AVL

Balanced binary search tree : Short balanced binary tree. By the former Soviet mathematician Adelse-velskil and Landis in 1962 a highly balanced two-fork tree, according to the scientist's English name also known as the AVL tree. It has several properties as follows:

    1. Can be an empty tree.
    2. If it is not an empty tree, the Zuozi of any node and the right subtree are balanced binary trees, and the absolute value of the difference in height does not exceed 1.

The meaning of balance, such as the balance, which is about the same weight on both sides. As defined, if the height of the left and right sub-tree of a tree exceeds 1, such as the tree height of the ieft subtree is 2, the tree height is 0, and the absolute value of the sub-tree height difference is 2 to break the balance. The tree loses its balance if the right subtree of the root node is high minus the left subtree height of 2 after inserting a three-by-five node (such as) in turn.

?

So in the process of building a tree, how do we know the height difference between the left and right sub-trees? Here we use the balance factor to record.

balance factor : the height of the Zuozi minus the height of the right subtree. By the definition of the balanced binary tree, the value of equilibrium factor can only be 0,1,-1. Corresponding to the left and right sub-tree, and so on, the higher of the tree, and the higher. Such as

?

In this case, we can probably know what the structure of the balanced binary tree requires, the data members, the balance factor, and the left and right branches. Therefore, we give the following structure definition. We mainly first understand the balance factor of each value and its meaning can be.

typedef char KeyType;???????? Key words

typedef struct MYRCDTYPE???? Recording

{

KeyType key;

}rcdtype,*rcdarr;

typedef enum MYBFSTATUS???????? To facilitate the assignment of the balance factor, enumeration is performed here

{???????????? RH,EH,LH, respectively, indicate higher right sub-tree, high and low subtree, and higher left sub-tree.

Rh,eh,lh

}bfstatus;

typedef struct MYBBSTNODE???? tree node type definition

{

Rcdtype data;???????????????? Data members

Bfstatus BF;???????????????? Balance Factor

struct Mybbstnode *lchild,*rchild;???? Left and right branches

}bbstnode,*bbstree;

Imbalance and adjustment in the insertion of AVL trees

This part of the imbalance adjustment refers to the imbalance and adjustment at the time of insertion. The imbalance of deletion is roughly the same as the adjustment and insertion, but there are many differences that are explained in subsequent chapters.

    1. Imbalance and adjustment guide

Having said that for so long, we began to get into the focus of today, how to turn an unbalanced two-fork tree into a balanced binary tree (the imbalance is only discussed because if the tree is balanced, we do not have to deal with it). The imbalance adjustment of the balanced binary tree is mainly realized by rotating the minimum unbalance subtree .

Minimum unbalance subtree : A subtree that is found on a newly inserted node that is the root of a node with an absolute value of more than 1 of the first balance factor is called the smallest unbalanced subtree. In other words, an unbalanced tree is likely to have multiple subtrees trees at the same time unbalanced, as follows. At this point, we can adjust the unbalanced tree to a balanced tree as long as we adjust the smallest unbalanced subtree.

In Figure 7. 2 The absolute value of the node (high-right subtree of the left subtree) = 2. Similarly, the 3-node equilibrium factor is also 2. At this time there are two unbalanced subtrees, and the tree with 3 roots is the smallest unbalanced subtree. As long as we have 3 as the center, the smallest imbalance tree to the left to rotate, you can get a balanced binary tree, 8. Detailed method follow-up explanation.

First, let's start with two simple examples to get a sense of how to adjust.

Example 1: The right sub-tree is too high and rotates to the left. Steps are as follows

    1. 2 as the root node
    2. 1 as the left child of 2
    3. 2 left child as 1 right child (maintains the order of the tree, only null here)

Example 1

Example 2: The left dial hand tree is too high and rotates to the right. Steps are as follows

    1. 2 as the root node
    2. Put 3 as 2 right child
    3. 2 of the right child as 3 of the left child (maintaining the order of the tree, just null here)

Example 2

Let's look at an example of failure that can be achieved by rotating, but not being able to achieve balance.

Example 3: The right sub-tree is too high and rotates to the left. Steps are as follows

    1. 3 as the root node
    2. 3 of the left child as 1 of the right child
    3. 1 as the left child of 3

Example 3

As above, we found that the tree did not restore balance after the rotation. In contrast to Figure 9, we found that the right sub-tree of the root is inconsistent.

In the above three examples, we can see that when we rotate the unbalanced tree, we need not only to consider the equilibrium factor of the root node of the minimum unbalanced subtree, but also to consider the equilibrium factor of the root node of the higher sub-tree of the root node. 9 and Figure 13, the higher subtree is the right subtree, the right subtree is different, the rotation has a completely different result.

For the sake of discussion, we use a continuous two letters to represent the balance factor to denote different situations. The first letter represents the balance factor of the minimum imbalance subtree node, and the second letter represents the balance factor of the root node of the higher subtree of the smallest unbalanced subtree. Use L to indicate a higher left subtree, R for higher right subtree, and E for high-left subtree. As shown in Figure 11 above, the root of the balance factor L, the higher subtree of the root is L, we will be represented as the ll type, and then as above example 13, the root is R, the higher subtree root is L We call this the RL type.

We will discuss all the imbalances below. Broadly divided into two categories, one left sub-tree is too high, two right sub-tree is too high. Incidentally, the method of memory, the reader for a specific type as long as the last node to remember as the root can be, that is, the red section below.

    1. Imbalance and handling
      1. Left dial hand tree is too high
        1. LL type

        In the ll type of unbalanced tree, we first find the smallest unbalanced subtree, and then rotate it to the right with its root node. Why do you rotate to the right? It should not be difficult to understand, after the right rotation, equivalent to the right of the sub-tree tree height increased by 1, while the left tree tree height decreased by 1, and the original tree height difference of 2, then can be the root of the balance factor into 0. Refer to the previous figure below. After rotation, the " left child of the original root node as the new root node ".

We rotate the tree to the right, centered on the root node. The rotation steps are as follows

    1. 2 as the root node
    2. Put 3 as 2 right child
    3. 2 of the right child as 3 of the left child (maintaining the order of the tree, just null here)

?

After rotation, the balance factor of 3 and 2 is eh,1, which remains the same.

    1. Le type

What needs to be explained here is that when inserting, there is no such case of Le. It will only appear when you delete it. Here are some personal insights into why inserting cannot occur.

We might as well assume that this is the case with Le. As follows.

Assuming that the element we just inserted is 1, then the original tree is no longer a balanced tree. No way.

Assuming that the element we just inserted is 2.5, then the original tree is not a balanced tree, nor is it possible. So when inserting, there is no such case of Le. And when it happens, we'll explain it in the deleted chapters. Similarly, there is no possibility of re-occurrence, nor is there any discussion below. Readers can use contradiction to authenticate themselves.

    1. LR type

For LR, there are two steps to spin. After rotation, the "right child of the left child of the original root node as the new root node".

The first is the root of the higher subtree, that is, 1, which rotates the center to the left. The following steps are described.

    1. The left subtree of 2 is used as the right subtree of 1 (maintaining the order of the tree, only null here)
    2. 1 as a Zuozi of 2
    3. 2 as a Zuozi of 3

The second is centered on the root of the original tree, which is 3, and rotates to the right. The final result is as follows

After rotation, the balance factor of the 0 is changed (no memory required). Once again the personal opinion, balance factor to use when the time to push a bit better.

    1. Right sub-tree too high
      1. RR type

        or refer to the previous example. The steps to rotate are as follows. After rotation, the " right child of the original root node as the new root node ".

        1. 2 as the root node
        2. 1 as the left child of 2
        3. 2 left child as 1 right child (maintains the order of the tree, only null here)

At last, the equilibrium factor of the all-in-the-.

    1. RL Type

or refer to the previous example. Similar to the LR type, we need to rotate two times. The left child of the right child with the original root node is rotated to be the new root node.

    1. The right child at the root node, which is 3, rotates to the right, and the result is as follows. The steps are as follows
    1. Put 2 as 1 right child
    2. Put 3 as 2 right child
    3. 2 of the right child as 3 of the left child (maintaining the order of the tree, just null here)

    1. The original root node is 1, which rotates to the left as the center. The results are as follows. The steps are as follows
    1. 2 as the root node
    2. 1 as the left child of 2
    3. 2 left child as 1 right child (maintains the order of the tree, only null here)

?

The equilibrium factor of the last and the same will be eh

    1. Summary of imbalance and adjustment during insertion
    1. In all cases of imbalance, follow the "finding the smallest unbalanced tree", "finding the type of imbalance", "fixing the program according to 4 categories".
    2. LL,LR,RR,RL has actually provided us with the last node to indicate the direction as the new root. As the final root node of the LR is the right child of the left child of the original root, the final root node of the RL type is the left child of the right child of the original root. We just have to remember these four situations and we can deduce all of them quickly.
    3. The most troublesome part of maintaining a balanced binary tree is the maintenance of the balance factor. To familiarize yourself with the process, it is advisable to draw a lot of pictures and experience the process first in the senses.

      ?

Speaking of which, we have learned about what is a balanced binary tree, and how to adjust the balanced binary tree after inserting a node. Our data structure is often mentioned in the change and deletion, then we will explain how to delete.

Imbalance and adjustment in the deletion of AVL tree

This is the main reason why today's whim wants to write this blog. I found on the internet for a long time, many people for the AVL tree search, insert are explained very wonderful, but delete often posted a piece of code, relatively few explanations, for me to complete the work of the students really uncomfortable, completed the homework after the hope to share with you. Cough, let's go back to the chase. Ahead of the high energy, drink saliva, look out of the window handsome beauty and continue to see it.

    1. Pre-knowledge
      1. Deletion of trees

If there is a binary search tree as follows, we have it in the middle sequence traversal, you can get 1, 2, 2.5, 3. We find that this is an ascending sequence. If we want to delete the node is 3, when the balance of the tree is not considered, which node should be used as the replacement of 3 position? The answer is: 3 of the direct precursor or direct-to-back drive for the sequencing binary tree in the middle sequence traversal. Here, is 2.5, so after deletion, do not make adjustments to the results such as the middle diagram. If we are going to delete a node that is 2, without considering the balance of the tree, 1 replaces the 2 position (assuming the left child takes precedence over the right child). Finally, the right image is shown below.

The specific steps are as follows:

    1. Find the node you want to delete (3)
    2. Assign the immediate precursor of the node to be deleted or direct back-drive assignment to the node to be deleted (2.5 assigned to 3 nodes).
    3. Delete the direct precursor or direct back drive (remove 2.5 of the leaf node).

???? Since we are mainly talking about balanced binary tree balance adjustment, this part of the right to give readers a bad supplement. If the reader still does not understand, please first look at the binary search tree deletion, and then continue to look down.

    1. The advance of the balance factor

We already know that the equilibrium factor has only three kinds of values, Lh,rh,eh. For the following tree, delete a node after

    1. The tree is so tall and so on. The value of root balance factor changes to EH->LH,EH->RH.
    2. The original tree and left and right sub-tree were not high, the higher subtree was deleted, the root balance factor changed to Lh->eh,rh->eh. It is important to note that when the balance factor of the root changes to Lh->eh,rh->eh, the height of the whole tree is decreased. The simplest examples are the following. The following two trees, respectively removed 1,3, balance factor Lh->eh,rh->eh. The height of the last tree fell.

?

    1. In the original tree, the trees are not high and the trees are deleted on the lower subtree, and the tree should be treated with balance. Delete the end point 1 below to get the unbalanced tree on the right.

    1. What can cause a tree to drop high
      1. As in item B of the 2nd, the balance factor of the root is decreased by the height of the whole tree when Lh->eh,rh->eh.
      2. Based on a point and the correct balance treatment, the tree height will be lowered when the tree is properly balanced. Why is it? Since the smallest unbalanced subtree is rotated, the balance factor of the root of the smallest unbalanced sub-tree always becomes eh, or the balance adjustment always decreases the height of the smallest unbalanced subtree. Examples are as follows. The height of the tree becomes 2 from the original 3.

    1. Formal access to the AVL tree deletion and adjustment
      1. Delete node causes balanced binary tree imbalance

The AVL tree is also a binary lookup tree, so its deletion is based on the deletion of the binary search tree, but we need to adjust it when unbalanced. We have already mentioned in the 2nd C of the preparatory knowledge that if we delete the lower subtree, it will lead to the emergence of the unbalanced tree directly. Well, that's what we need to deal with in a balanced way. Give me a chestnut.

    1. Resizing an unbalanced subtree results in a larger unbalanced subtree

Assuming that the minimum imbalance subtree is a, it is the left subtree of the parent node B, while the balance factor of B is RH. Let's say we have a balanced treatment of a, as we said above, balancing will result in a lower tree height. That is, we make B shorter sub-trees become shorter. At this point, it is also unbalanced for B. At this point, we need to do a balancing process again. Give me a chestnut as follows.

Let's say we delete the closing point 6. So the smallest unbalanced subtree is the corresponding two-tree of the 1,3,5. The balance factor of 10 of its parents is RH. We first adjust the minimum unbalance subtree, as shown in the figure on the right. We found that the smallest unbalanced subtree changed from the left subtree of the root node to the whole tree, so we have to make a balance adjustment at this point. The specific balance adjustment steps are consistent with the insertion and are detailed here.

When explaining the insertion of a new node for balancing, there is no big difference between when deleting and inserting. At the time of inserting, the whole tree will be balanced, and when it is deleted, it will need to be processed several times to ensure the tree is in a balanced state.

A careful friend may find that the balance factor of the higher subtree of the least-balanced subtree is eh in the upper right image. At this point, there is an unlikely imbalance mentioned in the previous insert.

    1. The last case of imbalance and adjustment le and re

The unbalance tree of Le and re, when adjusted, is consistent with the way ll and RR are rotated. Only the equilibrium factor of the last initial root node is not eh. Taking the example above, the result of the adjustment is as follows. The equilibrium factor of the initial root node is RH. Correspondingly, if it is Le, the equilibrium factor of the initial root node after adjustment is LH.

?

If you see this place, please first clap yourself for the weary self. This is the end of this article, the next one will bring you a concrete C code implementation.

Graphic balance binary tree, AVL tree (i)

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.