data structures in java
play

Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel - PowerPoint PPT Presentation

Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel Bauer Balanced BSTs Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero children). Then the height of


  1. Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel Bauer

  2. Balanced BSTs • Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero children). • Then the height of the tree will be O( log N) and all BST operations will run in O(log N) .

  3. AVL Tree Condition • An AVL Tree is a Binary Search Tree in which the following balance condition holds after each operation: • For every node, the height of the left and right subtree differs by at most 1. 7 5 2 3 3 1 2 8 2 8 1 1 2 2 1 0 not an AVL tree 1 4 1 4 7 1 1 1 0 3 5 3

  4. AVL Trees • Height of an AVL tree is at most 
 ~ 1.44 log(N+2)-1.328 = O(log N) • How to maintain the balance condition? • Rebalance the tree after each change (insertion or deletion). • Rebalancing must be cheap.

  5. “Outside” Imbalance node k 2 violates the balance condition k 2 k 2 k 1 k 1 z x y y z x left subtree of left right subtree of right child too high child too high • Solution: Single rotation

  6. “Inside” Imbalance node k 2 violates the balance condition k 2 k 2 k 1 k 1 z x x z y Y right subtree of left left subtree of right child too high child too high • Solution: Double rotation

  7. Single Rotation k 2 k 1 z y x Maintain BST property: • x is still left subtree of k 1. • z is still right subtree of k 2. • For all values v in y : k 1 < v < k 2 
 so y becomes new left subtree of k 2 .

  8. Single Rotation k 1 k 2 x z y Maintain BST property: • x is still left subtree of k 1. • z is still right subtree of k 2. • For all values v in y : k 1 < v < k 2 
 so y becomes new left subtree of k 2 .

  9. 
 Single Rotation k 1 Modify 3 references: • k 2 .left = k 1 .right k 2 • k 1 .right = k 2 x • parent( k 2 ).left = k 1 or 
 z y parent( k 2 ).right = k 1 or Maintain BST property: • x is still left subtree of k 1. • z is still right subtree of k 2. • For all values v in y : k 1 < v < k 2 
 so y becomes new left subtree of k 2 .

  10. Maintaining Balance in an AVL Tree • Assume the tree is balanced. • After each insertion, find the lowest node k that violates the balance condition (if any). • Perform rotation to re-balance the tree. • Rotation maintains original height of subtree under k before the insertion. No further rotations are needed.

  11. Single Rotation Example 3 insert(3)

  12. Single Rotation Example 3 insert(3) insert(2) 2

  13. Single Rotation Example 3 3 insert(3) insert(2) 2 rotate_left(3) insert(1) 1

  14. Single Rotation Example 2 insert(3) insert(2) 1 3 insert(1) rotate_left(3)

  15. Single Rotation Example 2 insert(3) insert(2) 1 3 insert(1) rotate_left(3) insert(4) 4

  16. Single Rotation Example 2 insert(3) insert(2) 1 3 3 insert(1) rotate_left(3) insert(4) 4 rotate_right(3) insert(5) 5

  17. Single Rotation Example 2 insert(3) insert(2) 1 4 insert(1) rotate_left(3) 5 insert(4) 3 rotate_right(3) insert(5)

  18. Single Rotation Example 2 2 insert(3) insert(2) 1 4 insert(1) rotate_left(3) 5 insert(4) 3 rotate_right(3) insert(5) 6 insert(6) rotate_right(2)

  19. Single Rotation Example 4 insert(3) insert(2) 2 5 insert(1) rotate_left(3) 1 insert(4) 3 6 rotate_right(3) insert(5) insert(6) rotate_right(2)

  20. Single Rotation Example 4 insert(3) insert(2) 2 5 5 insert(1) rotate_left(3) 1 insert(4) 3 6 rotate_right(3) insert(5) 7 insert(6) rotate_right(2) insert(7) rotate_right(5)

  21. Single Rotation Example 4 insert(3) insert(2) 2 6 insert(1) rotate_left(3) 1 5 insert(4) 3 7 rotate_right(3) insert(5) insert(6) rotate_right(2) insert(7) rotate_right(5)

  22. Single Rotation does not work for “Inside” Imbalance k 2 k 1 z x y

  23. Single Rotation does not work for “Inside” Imbalance k 1 k 2 x z y Result is not an AVL tree. Now k 1 is violates the balance condition. 
 Problem: Tree y cannot move and it is too high.

  24. Double Rotation (1) • y is non-empty (imbalance due to insertion into y or deletion from z) • so we can view y as a root and two sub-trees. k 3 k 1 z x y

  25. Double Rotation (1) • y is non-empty (imbalance due to insertion into y or deletion from z) • so we can view y as a root and two sub-trees. k 3 k 1 z k 2 x y l y r • either y l or y r is two levels deeper than z (or both are empty).

  26. Double Rotation (2) k 3 k 1 z k 2 Maintain BST property: x • x is still left subtree of k 1. y l y r • z is still right subtree of k 3. • For all values v in y l : k 1 < v < k 2 
 so y l becomes new right subtree of k 1 . • For all values w in y r : k 2 < w < k 3 
 so y r becomes new left subtree of k 3 .

  27. Double Rotation (2) k 2 k 3 k 1 y l y r z Maintain BST property: x • x is still left subtree of k 1. • z is still right subtree of k 3. • For all values v in y l : k 1 < v < k 2 
 so y l becomes new right subtree of k 1 . • For all values w in y r : k 2 < w < k 3 
 so y r becomes new left subtree of k 3 .

  28. Double Rotation (2) These are actually two single rotations: First at k1, then at k3. k 3 k 1 z k 2 x y l y r

  29. Double Rotation (2) These are actually two single rotations: First at k1, then at k3. k 3 k 2 z k 1 y r x y l

  30. Double Rotation (2) These are actually two single rotations: First at k1, then at k3. k 2 k 1 k 3 z y l y r x

  31. Double Rotation (3) Modify 5 references: k 2 • parent( k 3 ).left = k 2 or 
 k 3 k 1 parent( k 3 ).right = k 2 • k 2 .left = k 1 y l y r z • k 2 .right = k 3 x • k 1 .right = root( y l ) • k 3 .left = root( y r )

  32. Double Rotation Example 4 2 6 5 1 3 7

  33. Double Rotation Example insert(16) 4 2 6 5 1 3 7 16

  34. Double Rotation Example insert(16) 4 insert(7) rotate(7) 2 6 5 1 3 7 7 16 15

  35. Double Rotation Example insert(16) 4 insert(7) rotate(7) 2 6 k 1 5 1 3 7 7 k 3 x 16 k 2 z 15 y l y r

  36. Double Rotation Example insert(16) 4 insert(7) rotate(7) insert(14) rotate(6) 2 6 6 5 1 3 15 7 16 14

  37. Double Rotation Example insert(16) 4 insert(7) rotate(7) insert(14) rotate(6) 2 6 6 k 1 5 1 3 15 k 3 7 x 16 k 2 z 14 y l y r

  38. Double Rotation Example insert(16) 4 insert(7) rotate(7) insert(14) rotate(6) 2 7 6 1 3 15 5 14 16

Recommend


More recommend