AVL Search Tree Presented By: Prof. U V Thete Dept of Computer Sci. YMA AVL TREES: Defination of AVL trees Why AVL Trees? Properties of AVL trees Construct of AVL trees Insertion and Deletion DEFINATION OF AVL TREES An AVL tree is another balanced binary search tree AVL Trees =Adelson- Velskii and L andis trees comes from its inventors described in 1962 for the organization of information.“ Example: 12 8 5 4 18 11 17 WHY AVL TREES? When sorted elements inserted into BST than it becomes as degenarated tree 12 Which is insufficient After insertion/deletion its height will be increase And also can be imbalanced 13 18 That is ensure AVL trees! 20 AVL TREES: 12 12 8 5 18 11 17 13 18 20 4 PROPERTIES OF AVL TREES: Sub-trees of each node can differ by at most 1 in their height So, h=max(l.h-r.h)+1 h=Height l=left, r=Right Calculate the height By default, leaf height =0. And empty sub-trees have a Height of -1. Height = max(L.height, R.height) + 1 sub-tree L has a height of 0 55 Height 0 32 Height 0 64 Height 2 71 sub-tree R has a height of 1 Height 1 86 Height = 0 = max(-1,-1) + 1 AVL (ADELSON-VELSKII AND LANDIS) TREES 44 4 9 • An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. 2 17 78 1 3 2 32 88 50 1 48 62 1 An example of an AVL tree where the heights are shown next to the nodes: 1 AVL (ADELSON-VELSKII AND LANDIS) TREES AVL tree is a binary search tree with balance condition Balance condition For every node in the tree, height of left and right subtree can differ by at most 1 10 To ensure depth of the tree is O(log(N)) And consequently, search/insert/remove complexity bound O(log(N)) AVL TREES: 12 12 8 8 18 5 5 11 17 4 4 11 7 2 YES H=3-2 =1 18 NO H=4-2 =2 17 ROTATIONS: Rotations are to rearrange nodes to maintain AVLness. They are needed on insert and delete. NAME OF ROTATIONS There are four of them Single Left (LL) and Single Right (RR) Double Left (LR) and Double Right (RL) The names describe which way the node moves. For example, a left rotation moves the node down and left. Nodes always move down when rotated. Double Rotation A LR double rotation is to rotate something to the left, and then its former parent to the right. A RL double rotation is to rotate something to the right, and then its former parent to the left. Insertion and Deletions It is performed as in binary search trees After insertion or del if it will be imbalanced, Than we need rotation to correct balance Single Rotation Double Rotation, Both can be Left/Right rotation. For insertions, one rotation is sufficient For deletions, may needed double AVL TREE INSERT AND REMOVE Do something to fix it : rotations After rotations, the balance of the whole tree is maintained 16 Do binary search tree insert and remove The balance condition can be violated sometimes BALANCE CONDITION VIOLATION If condition violated after a node insertion Rebalance the tree through rotation at the deepest node with balance violated The entire tree will be rebalanced Violation cases at node k (deepest node) An insertion into left subtree of left child of k 2. An insertion into right subtree of left child of k 3. An insertion into left subtree of right child of k 4. An insertion into right subtree of right child of k 1. Cases 1 and 4 equivalent Single rotation to rebalance Cases 2 and 3 equivalent Double rotation to rebalance 17 Which nodes do we need to rotate? Only nodes on path from insertion point to root may have their balance altered AVL TREES COMPLEXITY Overhead Extra space for maintaining height information at each node Insertion and deletion become more complicated, but still O(log N) Advantage Worst case O(log(N)) for insert, delete, and search 18 Construct of AVL Trees: Now we build an AVL tree: 15, 20, 24, 10, 13, 7, 30, 36, 25 20 15 20 15 24 10 13 24 15, 20, 24, 10, 13, 7, 30, 36, 25 20 20 15 13 10 24 13 10 24 15 15, 20, 24, 10, 13, 7, 30, 36, 25 20 13 10 7 13 10 24 15 7 20 15 24 30 36 15, 20, 24, 10, 13, 7, 30, 36, 25 13 13 10 10 20 7 7 20 15 15 30 30 24 24 36 36 25 15, 20, 24, 10, 13, 7, 30, 36, 25 13 10 7 13 20 15 10 24 7 30 25 24 20 15 36 30 25 36 Remove Operation in AVL Tree Removing a node from an AVL Tree is the same as removing from a binary search tree. However, it may unbalance the tree. Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node. Use the appropriate single or double rotation to balance the tree. May need to continue searching for unbalanced nodes all the way to the root. Deletion Remove 24 from the AVL tree. 13 10 13 10 24 7 20 15 7 30 25 36 20 15 30 25 36 Deletion Remove 20 from the AVL tree. 13 10 7 13 20 15 10 30 30 7 25 15 36 36 25 Deletion After Remove 24 and 20 from the AVL tree. 13 13 10 7 30 15 10 36 25 Fig (a):Unbalanced 15 7 30 25 Fig(b): Balanced 36 Thank you All
© Copyright 2024 Paperzz