300 likes | 448 Views
AVL Trees. Balanced Binary Search Trees (not covered in book, but related to pp 165-169). AVL Trees. First-invented self-balancing binary search tree Named after its two inventors, G.M. Adelson-Velsky and E.M. Landis,
E N D
AVL Trees Balanced Binary Search Trees(not covered in book, but related to pp 165-169)
AVL Trees • First-invented self-balancing binary search tree • Named after its two inventors, • G.M. Adelson-Velsky and • E.M. Landis, • published it in their 1962 paper "An algorithm for the organization of information."
AVL Properties • First, its a binary search tree... • L <= P and P <= R P L R
Question? • Is this a binary tree search tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68
Min Heap P <= L and P <= R Max Heap P >= L and P >= R BTW, Binary Heap Properties P L R
AVL Properties • An AVL tree is a balanced binary tree • To understand balance we need to understand the notion of Tree Height 55 Height 2 32 71 Height 1 64 86 Height 0
AVL Properties • By default, nodes with no children have a height of Height of 0. 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0
AVL Properties • But, we must also understand the concept of Sub-trees Height = max(L.height, R.height) + 1 sub-tree L has a height of 0 sub-tree R has a height of 1 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0
AVL Properties • Also empty sub-trees have a Height of -1 Height = max(L.height, R.height) + 1 44 Height = 2 = max(0, 1) + 1 58 Height = 1 = max(-1, 0) + 1 91 Height = 0 = max(-1,-1) + 1
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
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
1 3 7 15 31 0 AVL Properties • For ALL nodes, the Height of the Left and Right Sub-trees must be equal!
1 3 7 15 31 0 AVL Properties • For ALL nodes, the Height of the Left and Right Sub-trees must be equal! • Strict Balance Conditions are too restrictive.
Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68
Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 30 34 43 49 52 54 58 68
Question? • Is this an AVL Tree? 50 4 29 3 70 3 2 12 2 35 56 2 77 1 5 1 17 1 32 1 46 1 53 1 60 1 71 79 3 9 15 21 30 34 43 49 52 54 58 68
Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 79 3
Question? • Is this an AVL Tree? 50 4 29 3 70 2 1 12 2 35 56 0 77 1 5 1 17 0 32 0 79 0 3 0
Question? • Is this an AVL Tree? +1 50 4 +1 -1 29 3 70 2 +1 +1 0 -1 1 12 2 35 56 0 77 1 +1 0 0 0 5 1 17 0 32 0 79 0 0 3 0
Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 46 53 60 71 79 3 9 15 21 49 52 54 58 68
Question? • No 50 29 70 12 35 56 77 5 17 46 53 60 71 79 3 9 15 21 49 52 54 58 68
Question? • Did this fix the problem? 50 29 70 12 46 56 77 5 17 35 49 53 60 71 79 3 9 15 21 52 54 58 68
Question? 50 • Is this an AVL Tree? 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69
50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69 0 Question? • Is this an AVL Tree? +2 -1 -1 -1 0
50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69 Question? • Is this an AVL Tree? +2 -1 -1 -1 0
Question? 50 • Is this an AVL Tree? +2 29 -1 60 12 35 56 77 70 -1 5 17 32 46 53 71 79 -1 3 9 15 21 28 34 43 51 52 54 58 68 0 69
Question? 50 • Is this an AVL Tree? 0 29 60 12 35 56 70 5 17 32 46 53 58 68 77 3 9 15 21 28 34 43 51 52 54 69 71 79
Correcting Imbalance • After every insertion • Check to see if an imbalance was created. • All you have to do backtrack up the tree • If you find an imbalance, correct it. • As long as the original tree is an AVL tree, there are only 4 types of imbalances that can occur.
Imbalances • Left-Left (Single Rotation) • Right-Right(Single Rotation) • Left-Right (Double Rotation) • Right-Left(Double Rotation)
Illustration • http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html