Cinda Heeren / Geoffrey Tien AVL Trees Properties Insertion October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST rotation g g e h b h b f i a e i a c c f d d Rotations preserve the in-order BST property, while altering the tree structure we can use rotations to bring imbalanced trees back into balance October 12, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL trees An AVL tree is a balanced BST Each node's left and right subtrees differ in height by at most 1 Rebalancing via rotations occurs when an insertion or removal causes excessive height difference AVL tree nodes contain extra information to support this height information 43 19 63 57 60 50 78 38 4 21 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL nodes enum balance_type {LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = +1}; class AVLNode { public: int data; // or template type AVLNode left; AVLNode right; balance_type balance; AVLNode(int value) { ... } AVLNode(int val, AVLNode left1, AVLNode right1) { ... } } AVLNode is almost the same as a binary tree node additional balance field indicates that state of subtree balance at that node October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL imbalance Balanced trees: 3 3 7 12 16 3 7 12 19 3 16 27 31 44 Imbalanced trees: 12 19 3 16 27 31 44 21 24 12 16 3 7 9 3 7 5 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL imbalance 4 cases of imbalance C B A C A B A B C A C B LL imbalance LR imbalance RR imbalance RL imbalance Solve with a right rotation around C Left rotation around A, becomes LL case Symmetric to left imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion Maintaining balance The best way to keep a tree balanced, is to never let it become imbalanced! AVL insertion begins with ordinary BST insertion (i.e. a leaf node) followed by rotations to maintain balance i.e. AVL properties are satisfied before and after insertion if the balance attribute of a subtree's root node becomes critical (-2 or +2) as a result of inserting the new leaf, rebalance it! October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion Pseudocode if root is NULL Create new node containing item, assign root to it, and return true else if item is equal to root->data item exists already, return false else if item < root->data Recursively insert the item into the left subtree if height of left subtree has increased (increase variable is true) balance--; if balance == 0, reset increase variable to false if balance < -1 reset increase variable to false perform rebalanceLeft else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight are the rotations to correct the 4 imbalance cases October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) 47 32 71 65 93 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 47 RR imbalance 32 71 OK 65 93 OK 82 OK October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 71 Insert(87) 47 93 LR imbalance 32 65 82 OK 87 OK October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL insertion example Insert(65) Insert(82) 71 OK Insert(87) 47 87 OK 32 65 82 93 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL tree height The height of an AVL tree containing 𝑛 key values is 𝑂 log 𝑛 Intuition: For a fixed height ℎ, a tree containing fewer nodes has a larger height-to-node ratio 𝑛=15 𝑛=4 ℎ=3= log 𝑛 =𝑂( log 𝑛 ) ℎ=3=𝑛−1=𝑂 𝑛 Attempt to achieve the worst ratio by making an AVL tree of height ℎ with the minimum number of nodes October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL tree height Theorem: The height of an AVL tree with 𝑛 nodes is 𝑂 log 𝑛 Proof: Let 𝑁 ℎ represent the minimum number of nodes in an AVL tree of height ℎ Since the AVL property must be satisfied at every node, the children of such a tree must also be minimal, and the height difference between the children must be 1 Thus 𝑁 ℎ =1+ 𝑁 ℎ−1 + 𝑁 ℎ−2 1 Nh-2 Nh-1 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien AVL tree height 𝑁 ℎ =1+ 𝑁 ℎ−1 + 𝑁 ℎ−2 𝑁 ℎ−1 =1+ 𝑁 ℎ−2 + 𝑁 ℎ−3 𝑁 ℎ =1+ 1+ 𝑁 ℎ−2 + 𝑁 ℎ−3 + 𝑁 ℎ−2 =2+2 𝑁 ℎ−2 + 𝑁 ℎ−3 >2 𝑁 ℎ−2 𝑁 ℎ−2 =1+ 𝑁 ℎ−3 + 𝑁 ℎ−4 =2+2 𝑁 ℎ−4 + 𝑁 ℎ−5 >2 𝑁 ℎ−4 𝑁 ℎ >2∙2 𝑁 ℎ−4 𝑁 ℎ >2∙2∙2 𝑁 ℎ−6 How many times can we subtract 2 from ℎ before we reach 0? ℎ 2 times. 𝑁 ℎ >2∙2∙2∙2 𝑁 ℎ−8 … 𝑁 ℎ > 2 ℎ 2 ℎ< log 𝑁 ℎ ℎ∈𝑂 log 𝑛 October 17, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Exercise Draw the AVL tree resulting from the following sequence of insertions: 71, 13, 65, 22, 49, 37, 45 Note the height recorded/updated at each node What would an ordinary BST look like with the same sequence of insertions? October 17, 2017 Cinda Heeren / Geoffrey Tien
Readings for this lesson Koffman Chapter 11.2 (AVL trees) AVL visualisations: http://visualgo.net/en/bst (AVL tree) http://www.cs.usfca.edu/~galles/visualization/AVLtree.html Next class: AVL removal October 17, 2017 Cinda Heeren / Geoffrey Tien