390 likes | 637 Views
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.
E N D
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
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
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
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
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
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
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
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
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
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
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
Tree Rotations to Restore Balance j Suppose we have balance . . . k Z X Y
Tree Rotations to Restore Balance j Suppose we have balance . . . k Z Y X . . . but we insert here, destroying the balance.
Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X
Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X
Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X
Tree Rotations to Restore Balance Do a “single rotation” j k Z Y X
Tree Rotations to Restore Balance Do a “single rotation” k j Z Y X
Tree Rotations to Restore Balance Do a “single rotation” k j X Z Y
Tree Rotations to Restore Balance Do a “single rotation” k Done! j X Z Y
Tree Rotations to Restore Balance j In one step: k Z Y X
Tree Rotations to Restore Balance k j X Z Y
Tree Rotations to Restore Balance j Again, suppose we have balance . . . k Z X Y
Tree Rotations to Restore Balance j Again, suppose we have balance . . . k Z X Y . . . but this time we insert here.
Tree Rotations to Restore Balance j Again, we do a single rotation . . . k Z X Y
Tree Rotations to Restore Balance Again, we do a single rotation . . . k . . . but it fails to restore balance j X Z Y
Tree Rotations to Restore Balance j Go back to where we started . . . k Z X Y
Tree Rotations to Restore Balance j Go back to where we started . . . k Z X . . . and consider structure of Y Y
Tree Rotations to Restore Balance j k Z i X V W
Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W
Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W
Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W
Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W
Tree Rotations to Restore Balance j Now do a “double rotation”. . . k Z i X V W
Tree Rotations to Restore Balance Now do a “double rotation”. . . i j k Z V W X
Tree Rotations to Restore Balance Now do a “double rotation”. . . i j k Done! Z V W X