AVL tree
- AVL tree is withEquilibrium conditionsThe balanced binary tree
- Equilibrium conditions: the height of its left subtree of each node and right subtree
- This is an AVL tree
- This is not an AVL tree of
After resolving the insert node AVL tree damage equilibrium conditions
- byRotationWay to change the node AVL tree balance.
- We must rebalance node is called a, due to the arbitrary node has at most two sons, resulting in highly unbalanced height difference would need two subtrees a point 2, easy to see that this imbalance may occur in the following four cases:
- 1, a left subtree left his son once inserted - (left - left)
- 2, left and right sub-tree of a son once inserted - (left - right)
- 3, left and right sub-tree of a son once inserted - (Right - Left)
- 4, a pair of right subtree right son once inserted - (right - R)
1 and 4 for which we use a single rotation, for 2 and 3 with double rotary us to solve this problem and achieve new AVL tree
Single rotation
After 6 avl entire tree node into nodes destroyed the balance, at the node 8. Here is a 8. Here belong (left - left), we will define as the 8-node k1, k2 is defined as the node 7, will now be put k1 k2 position to solve.
- Similarly for Scenario 4 is the same reason
- Summary: We will set a node k1, the next node of a node is set to k2, after the post into k1, k2 re-allocation position of the nodes on the formation of a new AVL tree, there's a little knowledge is when inserted between the node value is not k1 and k2, it will be the case 1 and the case 4, while the solution with a single rotation.
Double rotation
- Here for solving scenarios 2 and 3 (ie, left - right and right - left).Dual rotary actually two single rotation
- Here a (7) is set to k1, k1 following points k3, k3 following points k2. The node k2 into position a, the other nodes rearranged. Resulting in the desired value of a double-rotation nodeCertainly between k1 and k3
- Here is k1,16 7 is k3,15 is k2.
- Follow the steps above to resolve, this is a right - left, empathy left - right is the same.
Implementation code
AVL tree data structure
- This is an internal class
public static class AvlNode<T>{
AvlNode node;
AvlNode<T> rnode;
AvlNode<T> lnode;
Integer height;
AvlNode(AvlNode node){
this(node,null,null);
}
AvlNode(AvlNode node,AvlNode<T> lnode,AvlNode<T> rnode){
this.node = node;
this.rnode = rnode;
this.lnode = lnode;
}
}
Single rotation
- There is left portion corresponding to the incoming node placed above, switching node.
private AvlNode<T> rotateWithLeftChild(AvlNode<T> k2){
AvlNode<T> k1 = k2.lnode;
k2.lnode = k1.rnode;
k1.rnode = k2;
k2.height = Math.max(height(k2.lnode),height(k2.rnode))+1;
k1.height = Math.max(height(k1.lnode),height(k1.rnode))+1;
return k1;
}
- Similarly, the right rotation
private AvlNode<T> rotateWithRightChild(AvlNode<T> k2){
AvlNode<t> k1 = k2.rnode;
k2.rnode = k1.lnode;
k1.lnode = k2;
k2.height = Math.max(height(k2.lnode),height(k2.rnode))+1;
k1.height = Math.max(height(k1.lnode),height(k1.rnode))+1;
return k1;
}
Calculate the height
public Integer height(AvlNode<T> t ){
return t == null?-1:t.height;
}
Double rotation
private AvlNode<T> doubleWithLeftChild(AvlNode<T> k3){
k3.lnode = rotateWithRightChild(k3.lnode);
return rotateWithLeftChild(k3);
}
private AvlNode<T> doubleWithRightChild(AvlNode<T> k3){
k3.lnode = rotateWithLeftChild(k3.lnode);
return rotateWithRightChild(k3)
}