Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Similar presentations


Presentation on theme: "AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)"— Presentation transcript:

1 AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

2 AVL Trees First-invented self-balancing binary search tree Named after its two inventors, 1.G.M. Adelson-Velsky and 2.E.M. Landis, –published it in their 1962 paper "An algorithm for the organization of information."

3 AVL Properties First, its a binary search tree... L <= P and P <= R P LR

4 Question? Is this a binary tree search tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 607179 77 123556 2970 50

5 BTW, Binary Heap Properties Min Heap P <= L and P <= R Max Heap P >= L and P >= R P LR

6 AVL Properties An AVL tree is a balanced binary tree To understand balance we need to understand the notion of Tree Height 55 3271 6486 Height 0 Height 1 Height 2

7 AVL Properties By default, nodes with no children have a height of Height of 0. 55 3271 6486 Height 0 Height 1 Height 2 Height 0

8 AVL Properties But, we must also understand the concept of Sub-trees 55 3271 6486 Height 0 Height 1 Height 2 Height 0 sub-tree L has a height of 0 sub-tree R has a height of 1 Height = max(L.height, R.height) + 1

9 AVL Properties Also empty sub-trees have a Height of -1 44 58 91 Height = 2 = max(0, 1) + 1 Height = 0 = max(-1,-1) + 1 Height = max(L.height, R.height) + 1 Height = 1 = max(-1, 0) + 1

10 AVL Properties Anyway, the AVL Balance Property is as follows... For ALL nodes, the Height of the Left and Right Sub-trees can only differ by 1. P A Node L R

11 AVL Properties Wouldn’t this be a better Balance property? For ALL nodes, the Height of the Left and Right Sub-trees must be equal! P A Node L R

12 AVL Properties For ALL nodes, the Height of the Left and Right Sub-trees must be equal! 1 3 7 15 31

13 AVL Properties For ALL nodes, the Height of the Left and Right Sub-trees must be equal! Strict Balance Conditions are too restrictive. 1 3 7 15 31

14 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 607179 77 123556 2970 50

15 Question? Is this an AVL Tree? 39 5 1521 17 3034 32 4349 46 5254 53 5868 607179 77 123556 2970 50

16 Question? Is this an AVL Tree? 39 5 1521 17 3034 32 4349 46 5254 53 5868 607179 77 123556 2970 50 111111 1 22 2 3 4 3

17 Question? Is this an AVL Tree? 3 5173279 77 123556 2970 50

18 Question? Is this an AVL Tree? 3 5173279 77 123556 2970 50 1000 1 02 1 3 4 2 0

19 Question? Is this an AVL Tree? 3 5173279 77 123556 2970 50 1000 1 02 1 3 4 2 0 0 +1 0 00 0

20 Question? Is this an AVL Tree? 39 5 1521 17 49 46 5254 53 5868 607179 77 123556 2970 50

21 Question? No 39 5 1521 17 49 46 5254 53 5868 607179 77 123556 2970 50

22 Question? Did this fix the problem? 39 5 1521 1749 46 5254 53 5868 607179 77 12 35 56 2970 50

23 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 607179 77 123556 2970 50 69

24 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 607179 77 123556 2970 50 69 0 +2

25 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 607179 77 123556 2970 50 69 0 +2

26 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 53 5868 60 7179 77 123556 29 70 50 69 0 +2

27 Question? Is this an AVL Tree? 39 5 1521 17 2834 32 4351 46 5254 535868 60 7179 77 123556 29 70 50 69 0

28 Correcting Imbalance 1.After every insertion 2.Check to see if an imbalance was created. All you have to do backtrack up the tree 3.If you find an imbalance, correct it. 4.As long as the original tree is an AVL tree, there are only 4 types of imbalances that can occur.

29 Imbalances Left-Left (Single Rotation) Right-Right(Single Rotation) Left-Right (Double Rotation) Right-Left(Double Rotation)

30 Illustration http://www.eli.sdsu.edu/courses/fall96/cs66 0/notes/avl/avl.htmlhttp://www.eli.sdsu.edu/courses/fall96/cs66 0/notes/avl/avl.html


Download ppt "AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)"

Similar presentations


Ads by Google