1 / 37

AVL Tree: Balanced Binary Search Tree

AVL Tree: Balanced Binary Search Tree. 6. 2. 8. 4. 9. 1. 3. AVL Tree: Balanced Binary Search Tree. BST Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. 6. 2. 8. 4. 9. 1. 3.

Télécharger la présentation

AVL Tree: Balanced Binary Search Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Tree: Balanced Binary Search Tree 6 2 8 4 9 1 3

  2. AVL Tree: Balanced Binary Search Tree BST Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. 6 2 8 4 9 1 3

  3. AVL Tree: Balanced Binary Search Tree BST Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X. AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. 6 2 8 4 9 1 3

  4. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 6 2 8 4 9 1 3

  5. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 6 Is this is an AVL tree? 2 8 4 9 1 3

  6. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 6 2 8 4 9 0 1 0 0 3

  7. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 6 1 2 8 4 1 9 0 1 0 0 3

  8. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 6 2 1 2 8 4 1 9 0 1 0 0 3

  9. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 3 6 Yes, this is an AVL tree. 2 1 2 8 4 1 9 0 1 0 0 3

  10. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 3 6 Suppose we delete the 9 node. 2 1 2 8 4 1 9 0 1 0 0 3

  11. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 3 6 2 0 2 8 4 1 1 0 0 3

  12. AVL Tree: Balanced Binary Search Tree AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1. height(X) = max(height(left(X)), height(right(X))) + 1 height(L) = -1 3 6 AVL balance property fails at 6 node. 2 0 2 8 4 1 1 0 0 3

  13. Tree Rotations to Restore Balance j Suppose we have balance . . . k Z X Y

  14. Tree Rotations to Restore Balance j Suppose we have balance . . . k Z Y X . . . but we insert here, destroying the balance.

  15. Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X

  16. Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X

  17. Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X

  18. Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X

  19. Tree Rotations to Restore Balance Do a “single rotation” k j Z Y X

  20. Tree Rotations to Restore Balance Do a “single rotation” k j X Z Y

  21. Tree Rotations to Restore Balance Do a “single rotation” k Done! j X Z Y

  22. Tree Rotations to Restore Balance j In one step: k Z Y X

  23. Tree Rotations to Restore Balance k j X Z Y

  24. Tree Rotations to Restore Balance j Again, suppose we have balance . . . k Z X Y

  25. Tree Rotations to Restore Balance j Again, suppose we have balance . . . k Z X Y . . . but this time we insert here.

  26. Tree Rotations to Restore Balance j Again, we do a single rotation . . . k Z X Y

  27. Tree Rotations to Restore Balance Again, we do a single rotation . . . k . . . but it fails to restore balance j X Z Y

  28. Tree Rotations to Restore Balance j Go back to where we started . . . k Z X Y

  29. Tree Rotations to Restore Balance j Go back to where we started . . . k Z X . . . and consider structure of Y Y

  30. Tree Rotations to Restore Balance j k Z i X V W

  31. Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W

  32. Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W

  33. Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W

  34. Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W

  35. Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W

  36. Tree Rotations to Restore Balance Now do a “double rotation”. . . i j k Z V W X

  37. Tree Rotations to Restore Balance Now do a “double rotation”. . . i j k Done! Z V W X

More Related