AVL Trees B.Ramamurthy 4/27/2019 BR
Types of Trees trees static dynamic game trees search trees priority queues and heaps graphs binary search trees AVL trees 2-3 trees tries Huffman coding tree 4/27/2019 BR
Height of a Tree (Review) Definition is same as level. Height of a tree is the length of the longest path from root to some leaf node. Height of a empty tree is -1. Height of a single node tree is 0. Recursive definition: height(t) = -1 if T is empty = 0 if number of nodes = 1 = 1+ max(height(LT), height(RT)) otherwise 4/27/2019 BR
Balanced Binary Search Trees Let n be the number of nodes (keys) in a binary search tree and h be its height. Binary search tree offers a O(h) insert and delete if the tree is balanced. h is (log n) for a balanced tree. Height-balanced(k) tree has for every node left subtree and right subtree differ in height at most by k. 4/27/2019 BR
Unbalanced Tree 45 54 65 Maximum height 78 87 98 4/27/2019 BR
Balanced Tree 78 87 87 98 87 87 Minimum Height 4/27/2019 BR
AVL Property AVL (Adelson, Velskii and Landis) tree is a height-balanced(1) tree that follows the property stated below: For every internal node v of the tree, the heights of the children of v differ by at most 1. AVL property maintains a logarithmic height in terms of the number of nodes of the tree thus achieving O(log n) for insert and delete. Lets look at some examples. 4/27/2019 BR
AVL Tree: Example 4/27/2019 BR
Non-AVL Tree 4/27/2019 BR
Transforming into AVL Tree Four different transformations are available called : rotations Rotations: single right, single left, double right, double left There is a close relationship between rotations and associative law of algebra. 4/27/2019 BR
Transformations Single right : ((T1 T2) T3) = (T1 (T2 T3) Single left : (T1 (T2 T3)) = ((T1 T2) T3) Double right : ((T1 (T2 T3)) T4) = ((T1 T2) (T3 T4)) Double left : (T1 ((T2 T3) T4)) = ((T1 T2) (T3 T4)) A A B B A B A B A B C A C B A B C A B C A,B,C are nodes, Tn (n =1,2,3,4..) are subtrees 4/27/2019 BR
AVL Balancing : Four Rotations X3 X2 X1 X1 X3 Double right X2 X3 X1 X2 Single right X2 X1 X3 X1 X2 X3 X1 X3 X2 Single left Double left X2 X1 X3 X1 X2 X3 4/27/2019 BR
Example: AVL Tree for Airports Consider inserting sequentially: ORY, JFK, BRU, DUS, ZRX, MEX, ORD, NRT, ARN, GLA, GCM Build a binary-search tree Build a AVL tree. 4/27/2019 BR
Binary Search Tree for Airport Names ORY JFK ZRH MEX BRU ORD DUS ARN NRT GLA GCM 4/27/2019 BR
An AVL Tree for Airport Names After insertion of ORY, JFK and BRU : ORY JFK BRU Single right JFK BRU ORY Not AVL balanced AVL Balanced 4/27/2019 BR
An AVL Tree for Airport Names (contd.) After insertion of DUS, ZRH, MEX and ORD After insertion of NRT? JFK BRU ORY DUS MEX ZRH ORD NRT JFK BRU ORY DUS MEX ZRH ORD Still AVL Balanced 4/27/2019 BR
An AVL Tree … Not AVL Balanaced Double Left Now add ARN and GLA; no JFK BRU ORY DUS MEX ZRH ORD NRT JFK BRU ORY DUS NRT ZRH ORD MEX Double Left Now add ARN and GLA; no need for rotations; Then add GCM 4/27/2019 BR
An AVL Tree… Double left JFK BRU DUS ORY NRT ZRH ORD MEX ARN GLA GCM NOT AVL BALANCED 4/27/2019 BR
Search Operation For successful search, average number of comparisons: sum of all (path length to node+1) / number of nodes For the binary search tree (of airports) it is: 39/11 = 3.55 For the AVL tree (of airports) it is : 33/11 = 3.0 4/27/2019 BR