When we implemented our own shared memory template, map and set were not implemented. We originally considered using an AVLTree as the underlying implementation. Why, I was not familiar with RBTree in my data structure knowledge at that time, but I only engaged in AVLTree, but I never saw how to delete it. As a result, Scottxu jumped out. refer to the implementation of STLport and quickly use RBTree for Crash. The header file of the Code has been put there. After 7-8 years, I sorted out the code and looked at the basis of the Scottxu code. I think it is quite good. I think it should be easy to implement Copy to transform an AVLTree, you can get started.
There is nothing to say about AVL's insertion, that is, referring to the hand in Mr Yan Weimin's "data structure" (as if returning to the college age more than a decade ago), it was a quick start, but to delete it, it was dumb again. There are not many codes for reference. In particular, the Code for deleting AVLTree is not clear, and there are some obvious errors. Some are the balance factors calculated using Height, but most of the calculations using Height are recursive, however, this performance consumption method is basically not feasible in the formal environment. So I read some posts and found out how to implement them.Structure Algorithm for saving the balance factor on each node (The insertion in Mr Yan Weimin's Data Structure Tree is also based on the balance factor.), Implementation process, found a lot of traps, summed up.
(1) first, find the node to be deleted.
(2) If the node is a leaf node, delete it directly. If it is not a leaf node, switch it into a leaf node. The switch method is to select the largest node of the Left subtree (the rightmost son node of the Left subtree,) or the smallest node of the right subtree (the leftmost son node of the right subtree ). That is to say, select the most adjacent node of the node and exchange it with it. If the switch location is not a leaf node, continue the previous method to find a Node switch.
When the node becomes a leaf node, delete it.
Example 1: 10 is the node to be deleted. After two exchanges, it is changed to node 3 to become a leaf node.
After the switch, the tree structure becomes:
Example 2: 10 indicates the node to be deleted. After one switch, it is changed to the node 7 position to become a leaf node.
The adjusted tree is changed:
(3) After deletion, adjust the balance factor of the parent node. During the insertion process, you can adjust it to a node with a balance factor not equal to 0. The deletion process is the opposite,Adjusted to a balance factor0(The Impact of height changes on the balance is only until now)To stop. If a node is found to be unbalanced and the balance factor is 2 or-2, you need to adjust the rotation. After the rotation adjustment, you may need to continue to adjust (this is not the same as the insertion), or stop the adjustment (this problem will be highlighted later ).
If there are four types of rotations in the traditional sense of LL, LR, RR, and RL rotation, the height of the tree will be reduced, so we still need to adjust the balance factor upwards.
But interestingly, due to the deletion feature, you will find that the possible situations are not all four types of rotation: Traditional LL, LR, RR, and RL. For example, the following example.
The internal () of the node is the equilibrium factor, and 12 is the node to be deleted. After deletion, the equilibrium factor of node 7 is 2, but the equilibrium factor of node 5 on the left is 0, this is different from LL (left subtree balance factor is 1) and LR rotation (left subtree balance factor is-1.
The tree structure can still be balanced by LL rotation. However, after balancing, the balance factors of each node are different from those after insertion. The root node balance factor is not adjusted to 0, in this case, the height of the tree does not change. Therefore, you do not need to adjust the height of the tree when deleting the tree.
It also shows a situation similar to RR rotation:
The adjusted tree structure becomes
Well, the basic situation is as mentioned above. The reference code is put in GIT, which is based on templates, similar to STLPort.
The algorithm does not clearly indicate that we should be cautious when we touch it. This test draws a bunch of trees. In the end, I had written AVLTree when filtering the phone list. I didn't delete it at that time. So far, I have looked at the naive code of my time, the code of the "Data Structure" course in the current college age was not known on the moldy 3-inch floppy disk. Okay. In two days, I will listen to Li zongsheng's concert, "Now you can't stay young.
[The author of this article is Yandu hantan. In the spirit of freedom, you can repost this document in full without profit. for reprinting, please attach the BLOG link: http://www.cnblogs.com/fullsail/. if the negative word is a dollar, every figure one hundred is. Double the bidu library and 360doc price]