AVL树

AVL (Adelson-Velsky and Landis) trees

•height is O(log n), where n is the number of elements in the tree

•get, put, and remove take O(log n) time

•binary tree

•for every node x, define its balance factor:balance factor of x = height of left subtree of x - height of right subtree of x

• balance factor of every node x is  -1, 0, or 1

实例与各个节点的平衡因子

AVL树_第1张图片

高度

The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).

The height of every n node binary tree is at least  log2 (n+1).

平衡旋转

AVL树_第2张图片

AVL树_第3张图片

LL(rA):
   rB = rA.left;
   rA.left = rB.right;
   rB.right = rA;
   rA.height = max(rA.left.height,rA.right.height) + 1 
   rB.height = max(rB.left.height, rA.height) +1 
   return rB;

AVL树_第4张图片

LR(rA):
   rA.Left = RR(rA.left): 
      rC = rB.right;
      rB.right = rC.left;
      rC.left = rB;
      rB.height = … 
      rC.height = …
      return rC;  
   LL(rA):
      rC = rA.left;
      rA.left = rC.right;
      rC.right = rA;
      rA.height = … 
      rC.height = … 
      return rC;


AVL树插入

AVL树_第5张图片

AVL树删除

删除叶子节点

AVL树_第6张图片

删除度为1的节点

AVL树_第7张图片

删除度为2的节点

AVL树_第8张图片



你可能感兴趣的:(avl树)