Keywords : AVL, insert, delete
Introduction:
balanced binary tree and AVL distinguishing these two concepts: balanced binary tree is a data structure definition, is a description; and AVL tree is a data structure implemented in this way. Like red-black tree and B-tree, are of such a configuration is achieved while having all self-balancing characteristics.
AVL tree is a self-balancing binary tree is first invention.
1. Definition:
① left subtree and right subtrees are AVL trees;
② height of the left subtree and right subtree difference can not be more than one.
2, Properties:
① AVL tree of n nodes one of which is maintained at a height of 0 (log2 (n)), no more than 3 / 2log2 (n + 1);
The average length of the search an AVL tree of n nodes ② is held at 0 (log2 (n));
③ a time of n nodes AVL tree delete a node do equilibration is required to rotate the 0 (log2 (n)).
3, AVL tree maintaining a balanced manner: balance factor rotation.
4, AVL tree to add a new node:
(1) to find the insertion position of the node, then the insertion node;
(2) determining whether a point of imbalance case tree. If you do not then everything will be fine. If there is, then the first starting point to determine the imbalance, and then a corresponding operation according to rotational self-balancing policy (L, D) (see: self-balancing mode - rotating ).
5, AVL tree delete a node:
(1) find a replacement node.
① If you delete a node is a leaf node, then the node replacement is NULL;
② If you delete a node has a child node, then the replacement node is its left child or right child node; (If you delete a node has a child node, then the child node is a leaf node sure to meet strict AVL balance of properties.)
③ If you delete a node has two children, then preorder successor node to node as a replacement;
(2) the replacement node and delete nodes exchange; (Note: When replacing is essentially swap the values of two nodes, the original location of the two nodes remain the same.)
(3) delete operations:
① After deleting the replacement node at this time to determine which node can lead to imbalance. OK unbalanced starting point, and then follow the rotation policy (see: self-balanced way - rotation ) appropriate action to ensure that after the deletion of the current node in the tree or balance;
② adjusted first step is to ensure that after the deletion of nodes in the tree or the balance of the situation, and then delete nodes.
AVL tree, red-black tree && comparison:
① Because AVL tree is to ensure strict balance, so for insertion and deletion, slower than the red-black tree, red-black tree just asking because the relative balance;
② Because AVL tree is to ensure strict balance, so in certain circumstances, to find faster than in the red-black tree in the AVL tree.