Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9."— Presentation transcript:

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

2 6 2 4 3 1 8 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. 9

3 6 2 4 3 1 8 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. 9 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.

4 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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(  ) =  1

5 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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(  ) =  1 Is this is an AVL tree?

6 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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. 0 0 0 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1

7 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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. 0 0 0 1 1 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1

8 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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. 0 0 0 1 12 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1

9 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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. 0 0 0 1 12 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1 3 Yes, this is an AVL tree.

10 6 2 4 3 1 8 AVL Tree: Balanced Binary Search Tree 9 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. 0 0 0 1 12 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1 3 Suppose we delete the 9 node.

11 6 2 4 3 1 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. 0 0 1 02 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1 3

12 6 2 4 3 1 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. 0 0 1 02 height(X) = max(height(left(X)), height(right(X))) + 1 height(  ) =  1 3 AVL balance property fails at 6 node.

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

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

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

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

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

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

19 Tree Rotations to Restore Balance jk X Y Z Do a “single rotation”

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

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

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

23 Tree Rotations to Restore Balance j k X Y Z

24 j k XY Z Again, suppose we have balance...

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

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

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

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

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

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

31 j k X V Z W i Now do a “double rotation”...

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

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

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

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

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

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


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

Similar presentations


Ads by Google