Using JS to achieve those data structure 14 (Tree 02-avl tree)

Source: Internet
Author: User

One problem with using a two-fork search tree is that one branch of the tree has many layers, while the other branches have only a few layers, just like this:

If the amount of data is large enough, we will consume a significant amount of time when we are doing an additional pruning on an edge. We spend our energies building a structure that can improve efficiency, but it backfired. That's not what we want. So, we need another kind of tree to solve this problem, that is, self-balancing binary search Tree--adelson-velskii-landi (AVL). What do you mean? This means that the height difference between the left and right sub-trees of any one node of this tree is up to 1. This means that the tree will try to become a complete tree when adding or removing nodes.

The implementation of the self-balancing binary search tree and the two-fork search tree is almost identical, and the only difference is that every time we insert or delete a node, we need to detect its balance factor (because it is only possible to affect the balance of the tree when it is inserted or deleted). if necessary, apply its logic to the self-balance of the tree.

First, we need to know how this balance factor is calculated. The balance factor is calculated from the difference between the right subtree height (hr) and the Zuozi height (HL) of each node, which should be 0,1,-1. If this is not the three value, then the AVL tree needs to be balanced. This is the simple way to calculate the balance factor. What do you mean?

In our case, the balance factor of root node 11 is 6-3 = 3. The balance factor of the left child node 7 is 2-2 = 0; The balance factor of the right child node 18 is 5-2 = 3; the balance factor of node 70 is 0, remember that all leaf nodes (external nodes) have a balance factor of 0. Because the leaf node has no child nodes. It is also important to note that. The balance factor we calculate is the height of the left and right subtree of the node.

  We learned how to calculate the balance factor, so we're going to have a ritual that's important ... Oh, sorry. is its important knowledge-- rotation.

  Before we begin to explain the rotation, let's start with a little appetizer. Look at the possible situations that cause the tree to be unbalanced when we insert a child node. I'll draw a few pictures so that you can see it carefully.

  

First of all, we use the above image (to intercept part of the previous tree structure) as the initial tree, and this tree must definitely be balanced. Everyone has no problem with it. So what does RR,LL,RL,LR mean? So let's keep looking down.

  First case: RR.

We add a node 20 to the right child node in 18, and the right side is to add a value larger than the parent node.

After we joined a node 20, we found the tree to be balanced! Alas? No, it doesn't seem like the result I want. I'm going to try one more node?

Well...... Now the cliff is unbalanced. So let's see what's going on here. After joining Node 21 (19), the depth of the subtree on the left of the 11 node is 1, and the depth of the right subtree is the width of the tree. Yes, 3. Then 1-3 equals-2. Well, adding and subtracting within 10 should not be wrong. We determined that the tree would not be balanced after adding two right (R) nodes after node 18. And now there's an important question. Which is the subtrees tree that caused the tree to be unbalanced? Is after we join Node 21 (19), which is the part that we curse it with a small circle. So we can describe in a word that we have added a right child node to the right child node of the right child node of the tree (if it is the same as the left child node) , causing the tree to be unbalanced, so we need to manipulate the child node on the right. that is, 18 nodes, to make this self-balancing tree come from balance. In other words, if we join a node (or delete a node) that leads to an imbalance in our entire tree, we first need to find the nearest unbalanced tree to adjust.

The above RR case I added 19, and 212 byte points, to illustrate, these two sub-nodes are to more clearly tell everyone in root right node under the right node, regardless of whether the insertion of the left node or the right node belongs to the RR situation . Same below In the specific rotation of the time will give you a detailed introduction.

In other words, we decide whether or not the node will cause imbalance when adding or deleting nodes, determined by the first two parent nodes of the insertion node! Everybody pay attention to Oh! It's important!

    When the strike is over, it explains the condition of RR, so the following ll,lr,rl are the same. There is no difference in thinking. But this time I want to interrupt everyone. Ask you two questions. The solution of these two problems will bring great convenience to the subsequent study.

1, in the AVL tree or other trees, whether duplicate values can appear, such as the tree has a 11, I would like to add a 11, is it permissible? Is it possible?

2, see (RR situation map), is it possible to appear in addition to these four cases of other circumstances? Or is the balance factor of a node likely to be greater than 2 or less than 2? (In this case we have to rotate the tree more than two times, which is beyond the four of us.) )

Ok. Hope you close your eyes, think of your dream lover, oh no. Think about your answer.

No suspense, but I really want you to think about it because it is necessary and important.

Well, I'm starting to answer the first question. In fact, in the previous implementation of the tree is not allowed to repeat the value of the occurrence, we can go to see the previous code, if the equality will be overwritten. Then someone might ask, I want this tree to store duplicate values (of course, most of this happens when you have a problem with your design ...) There is no unique identification ah ... How the requirements are implemented). So I remember there is a solution to the conflict in HashMap, is it possible to store the same mapping of key values through a linked list? Can I use other storage methods? The answer is more open. So if you can store duplicate values, look at your actual needs.

The answer to the second question is not possible, because we must remember a premise that the tree must be balanced before inserting a node that causes the tree to be unbalanced. Why do you say that? Because our AVL tree is self-balancing binary search tree, if it is unbalanced before inserting, then you tell me what is this? Hurry back to see the code, there is a bug pro.

Here hope that we have lifted the hearts of a lot of doubts, if there are problems, we can continue to discuss the message.

So let's go on, and finish drawing the illustrations of several other cases.

  Second case: LL.

 

  The third case: LR.

  Fourth case: RL.

So after reading these pictures, we all know the 4 possibilities that affect the balance of the tree when inserting nodes. So in the face of these 4 possibilities. We give the corresponding 4 ways to solve the imbalance (in fact, two kinds). So here we are going to go into the most important part of this article, rotate . Before we begin, I hope you will remember one sentence. That is, what causes the imbalance, it is rotated in the opposite direction . What do you mean, like the unbalance that ll causes, then we turn right. If it is an imbalance caused by RR we will go to the left, if it is RL, we will re RR. If it's LR, we'll start with RR and then LL. Well, let's see how it spins.

So here's a little bit of meaning. I hope you can look at it carefully.

First, the left rotation of the RR condition

  We also have to look at the picture to speak, I try to make the picture easy to misunderstand, oh no, easy to understand.

I that, drawing tools used is not too skilled, crooked curve did not draw out, I take the mouth to say it ...

As you can see, the left side of the tree is rotated at 18 axis, so that 18 becomes the root node and 11 becomes the left child node of 18. This rotation is equivalent to reducing the depth of a layer of the right-hand tree, thus turning the whole tree into a balanced tree. Then there may be the following, but the same is true.

So this is the axis node (18) to rotate, and the left child node, after the rotation,18 of the left child node 13 will become the right child node 11. In fact, it is simple to think that the left-handed after the node 11 to " squeeze " over.

  In fact, 18 of the left child node after the rotation will become 11 of the right child node also has a reason, that is, thevalue of 18 left child node must be greater than 11 less than 18 (before the rotation of the diagram). Why do you want to. Then after the rotation, it must be greater than 11, so it can be the right child node of 11.

First, ll the right rotation of the situation

Then the right turn of the situation will be nothing to say, and RR situation is the same, we directly.

This cliff is no problem, the principle is the same. Just a change of direction.

There's nothing to say, right. Let's look at other things. Double rotation ...

Third, the LR condition of the left- hand (RR) and then right-handed (LL)

We still direct, and then explain, explain the RL situation and no longer wordy. Very good...... Very convenient, hey.

In fact, let a person a little crazy is the name, I specifically added a parenthesis, I hope you don't get mad.

Do not know people do not understand, the total feeling that this picture is not very friendly ah, there are 8 of nodes of the small flaws do not care, anyway are dashed ... And the line that points to node 10 is dashed ..... Do not affect ..... Hey.

Explain that we need a double rotation case, the first rotation is the red box part, that is, if we need double rotation, two rotation of the pivot point is not the same, the first rotation of the axis is inserted into the parent node , and the second axis of rotation is The grandparent node of the insertion node . We must pay attention to it.

Then there may be a question about why the 8 node becomes the right child node of the 7 node after the first rotation. This is very important and it is directly related to whether you understand the rotation of the AVL tree.

We first look at the first rotation, if the insertion is 8 nodes instead of 10 nodes, then in the first left, node 7 will become node 9 of the child nodes, and this time the 8 node is nowhere to go, because 7 occupies my position, this whole, not because of a balance on the deletion of my node Ah, node 8 certainly do not do, Or what do you insert me for? Hey? I feel a little bit wrong ..... The cough of the forehead .... Let's go on .... and node 8 This position must be smaller than 9 small than 7, so we after the rotation, let it become the right child node 7 node on it. I hope I can say it clearly.

So this time there may still exist 7 nodes have left child node situation, the above no painting, no matter ah, you are the left side of the 7 node node, left rotation after you still in the original position, no one occupies your position, you do not have to move. Well, that's it ..... Complete!

Iv. Right-hand (LL) re - rotation (RR) for RL case

  In fact, there is really nothing to say, I do not explain, we see, do not understand you look at the beginning!

Alas....... Said a lot, finally can go to the last code, on the code!

//This is how we calculate the height of the current node    varHeightnode =function(node) {//if not, that would be-1 .        if(node = = =NULL) {            return-1; } Else {            //if the execution logic exists            //So here's what I understand, Math.max. Compare the size of the left and right nodes, return the large value, and then + 1.             //Why do you want to return the value of the big one? Because if the left node exists, then the value is 0 (-1 + 1), and the right node is nonexistent, then the right node is-1.             //But at this point we have a high level, so we have to choose the height of the node, that is, the value of the big one.             //then why do you want +1? Because the height can only be 0 can not be-1. 1 is obtained by subtracting, not by calculating the height of the calculation. Remember this is the height of the calculation. Console.log (Math.max (Heightnode (Node.left), Heightnode (node.right)) + 1)            returnMath.max (Heightnode (Node.left), Heightnode (node.right)) + 1; }    }    //RR: Single rotation to the left    varROTATIONRR =function(node) {varTMP =Node.right; Node.right=Tmp.left; Tmp.left=node; returntmp; }    //LL: Single rotation to the right    varROTATIONLL =function(node) {varTMP =Node.left; Node.left=Tmp.right; Tmp.right=node; returntmp; }    //LR: Get double rotation to the right    varROTATIONLR =function(node) {Node.left=ROTATIONRR (Node.left); returnROTATIONLL (node); }    //RL: Double rotation to the left    varROTATIONRL =function(node) {node.right=RORARIONLL (node.right); returnRORARIONRR (node); }    varBalanceinsertnode =function(node,element) {//if node does not have a value, join it directly.         if(node = = =NULL) {node=NewNode (Element); //If the value is less than the current node, we are going to add it to the left of the current node. }Else if(Element <node.key) {node.left=Insertnode (node.left,element); //then the next thing is to determine whether it is null, if it is null, then no problem, directly add to the good.             if(Node.left!==NULL) {                //if not, we are going to calculate the left height of node minus whether the right height is greater than 1, and if so, it is necessary to call the balance method to balance.                 if((Heightnode (Node.left)-Heightnode (Node.right)) > 1) {                    //if the value of the currently inserted node is less than the value of node.left, it is the case of ll that we need to turn right. Otherwise, we need to turn left and then right.                     if(Element <Node.left.key) {node=RORARIONLL (node); } Else{node=RORARIONLR (node); }                }            }        } Else if(Element >node.key) {node.right=Insertnode (node.right,element); if(Node.right!==NULL) {                if((Heightnode (node.right)-Heightnode (Node.left)) > 1) {                    if(Element >Node.right.key) {node=RORARIONRR (node); } Else{node=RORARIONRL (node); }
} } } }

There is a Balanceinsertnode method in the code, this method needs to replace the Insertnode method we wrote earlier, so that we can make a better comparison. The code does not, as before, write a whole bunch of comments to explain. In fact, many of them are mentioned in the preceding picture and language description. So when you look at this code. There is no understanding of the place, against the previous logic 1.1 point of view, it must be understood. For example, the replacement of ROTATIONLL and ROTATIONRR, and why this is replaced, are mentioned earlier. So it's no longer verbose in the code.

This article is a bit long, it took me a little thought to complete. It is important that if you want to have a good understanding of the tree, these must be. I as far as possible with my understanding of the idea to explain to you, if there is anything unclear, we can leave a message to discuss.

Oh yes, I would like to talk to you about other trees, but I think there is no need to give you a link, we can do some simple understanding, such as red and black trees, stacked trees, and b-trees and so on. There are many kinds. If you want to finish about dozens of articles are not enough, I hope these two articles tree structure can be a catalyst. Let's bring up the interest in data structures.

You can look at this to understand https://zh.wikipedia.org/wiki/AVL%E6%A0%91, swipe to the bottom of the page, you can see the other tree structure.

Well, finally, the self-balancing binary search tree is basically over here. The next section will explain the last and most complex kind of nonlinear data structure-diagram.

Finally, because my level is limited, the ability and the great God is still very far apart, if there are errors or unclear, but also hope that everyone is not hesitate to correct. Thank you so much!

Using JS to achieve those data structure 14 (Tree 02-avl 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.