A4B33ALG 2011 / 06 AVL tree -- G.M. Adelson-Velskij & E.M. Landis, 1962 AVL tree is a BST with additional properties which keep it acceptably balanced. 2 1 0 -1 8 -1 13 0 -1 34 -1 1 1 -1 22 51 Operations Find, Insert, Delete also apply to AVL tree. 0 40 0 -1 -1 45 68 -1 76 0 -1 92 -1 -1 AVL rule: There are two integers associated with each node: Depth of the left and depth of the right subtree of the node. Note: Depth of an empty tree is -1. The difference of the heights of the left and the right subtree may be only -1 or 0 or 1 in each node of the tree. 2 A4B33ALG 2011 / 06 Inserting a node may disbalance the AVL tree The subtrees height difference may be only -1, 0, 1 !! Insert 30 3 2 0 -1 8 -1 13 34 1 -1 -1 Changed depths 0 30 1 1 -1 22 51 0 40 0 -1 -1 45 68 -1 76 0 -1 92 -1 -1 -1 Left subtree of node 51 is too deep, the tree is no more an AVL tree. 3 A4B33ALG 2011 / 06 Rotation R yields a balanced tree 51 3 Insert 30 2 0 -1 8 13 -1 34 1 -1 1 -1 22 -1 0 40 0 30 1 0 -1 -1 45 68 76 0 -1 -1 92 -1 -1 -1 34 2 2 0 13 1 The tree balanced by single R rotation -1 8 -1 -1 1 22 -1 0 30 -1 -1 40 51 0 -1 1 0 45 -1 -1 68 76 -1 0 -1 92 -1 4 A4B33ALG 2011 / 06 Rotate to obtain a balanced tree 51 3 2 Insert 30 0 -1 8 -1 13 34 1 -1 1 -1 22 -1 0 40 0 30 1 0 -1 -1 45 68 76 0 -1 -1 92 -1 -1 Direction of rotation -1 34 2 0 13 1 Rotation R in node 51 Unaffected subtrees -1 2 8 -1 -1 1 22 -1 0 30 -1 -1 40 51 0 -1 1 0 45 -1 -1 68 76 -1 0 -1 92 -1 5 A4B33ALG 2011 / 06 Rotation R in general, before and after A x+2 x+1 Before B C x D -1 E Z -1 x+1 After x B x+1 D x E -1 Z A x C -1 6 A4B33ALG 2011 / 06 Rotation L in general, before and after x Before A x+2 C x B x+1 D E Rotation L is a mirror image of rotation R, there is no other difference between the two. Z -1 x+1 After x C A B -1 x+1 D x E -1 Z -1 7 A4B33ALG 2011 / 06 AVL tree 2 1 0 -1 8 -1 13 34 -1 1 1 0 -1 51 36 0 40 0 -1 -1 45 -1 68 -1 76 0 -1 92 -1 -1 Demonstration AVL tree for rotation LR 8 A4B33ALG 2011 / 06 Inserting a node may disbalance the AVL tree The subtrees height difference may be only -1, 0, 1 !! Insert 38 3 1 0 -1 8 -1 13 34 -1 36 -1 Left subtree of node 51 is too deep, the tree is no more an AVL tree. 1 2 1 -1 51 0 40 0 -1 0 -1 38 45 -1 -1 68 -1 76 0 -1 92 -1 Changed depths -1 Rotation R would not help, the right subtree of node 34 would become relatively too deep compared to the new right subtree of the root. 9 A4B33ALG 2011 / 06 Rotation LR yields a balanced tree 51 3 Insert 38 1 0 -1 8 13 34 -1 2 1 -1 -1 36 -1 0 40 0 -1 0 -1 38 -1 45 68 76 0 -1 -1 92 -1 -1 -1 40 Balanced tree after double rotation LR. 2 1 0 -1 1 8 -1 13 -1 34 2 0 1 -1 36 -1 -1 0 38 -1 -1 45 51 -1 1 0 -1 68 76 -1 0 -1 92 -1 10 A4B33ALG 2011 / 06 Rotate to obtain a balanced tree 51 3 Insert 38 1 0 -1 8 13 34 1 -1 -1 36 -1 Rotation LR in nodes 34 and 51 0 -1 8 -1 13 R 2 L -1 40 0 -1 0 -1 38 -1 34 0 -1 45 68 -1 0 -1 -1 92 -1 Direction of rotations 40 Unaffected subtrees 2 0 1 -1 76 -1 2 1 1 36 -1 -1 0 38 -1 -1 45 51 -1 1 0 -1 68 76 -1 0 -1 92 -1 11 A4B33ALG 2011 / 06 Rotation LR in general, before and after X+2 x+1 Before B C E x 0 F -1 G Z -1 x+1 x x x+1 D After A B E x+1 x x F D -1 Z G A x C -1 12 A4B33ALG 2011 / 06 Rotation RL in general, before and after x A X+2 C x+1 B x+1 Before 0 E G Rotation RL is a mirror image of rotation LR, there is no other difference between the two. After x C A F -1 x+1 E D x Z -1 x+1 x x G F -1 Z B x D -1 13 A4B33ALG 2011 / 06 Rules for aplying rotations L, R, LR, RL in Insert operation Travel from the inserted node up to the root and update subtree depths in each node along the path. If a node is disbalanced and you came to it along two consecutive edges * in the up and right direction perform rotation R in this node, * in the up and left direction perform rotation L in this node, * first in the in the up and left and then in the up and right direction perform rotation LR in this node, * first in the in the up and right and then in the up and left direction perform rotation RL in this node, After one rotation in the Insert operation the AVL tree is balanced. After one rotation in the Delete operation the AVL tree might still not be balanced, all nodes on the path to the root have to be checked. 14 A4B33ALG 2011 / 06 Delete in AVL tree Demonstration AVL tree for rotation after deletion 1 -1 16 51 2 0 1 Delete 16 -1 28 -1 0 -1 55 70 84 0 -1 -1 93 -1 -1 1. Remove node using the same method as in BST. 2. Travel from the place of deletion up to the root. Update subtree heights in each node, and if necessary apply the corresponding rotation. 15 A4B33ALG 2011 / 06 Delete in AVL tree 1 Delete 16 -1 16 51 2 0 1 -1 28 -1 -1 0 -1 28 70 0 51 55 84 0 -1 -1 93 -1 -1 2 -1 1 0 Changed depths -1 55 70 -1 84 0 -1 93 -1 -1 16 A4B33ALG 2011 / 06 Delete in AVL tree 0 -1 28 51 L 2 -1 1 0 Changed depths -1 55 70 84 0 R -1 -1 93 -1 -1 In the disbalanced node (51), check the root (84) of the subtree opposite to the one which you came from. If the heights of subtrees of that root are equal apply a single rotation R or L. If the hight of the more distant subtree of that root is bigger than the height of the less distant one apply a single rotation R or L. In the remaining case apply a double rotation RL or LR. In this example, apply RL. 17 A4B33ALG 2011 / 06 Delete in AVL tree 0 -1 28 51 L 2 -1 1 Delete 16 0 70 84 0 R -1 -1 93 -1 Changed depths -1 0 After rotation RL in nodes 84 and 51 0 -1 28 -1 51 0 -1 55 70 -1 0 -1 55 -1 84 0 -1 93 -1 18 A4B33ALG 2011 / 06 Possibility of multiple rotations in operation Delete. Example. The AVL tree is originally balanced. 1 3 2 4 Balanced. 19 A4B33ALG 2011 / 06 Implementation ot the AVL tree operations ... // homework... Asymptotic complexities of Find, Insert, Delete in BST and AVL BST with n nodes AVL tree with n nodes Operation Balanced Maybe not balanced Balanced Find (log(n)) (n) (log(n)) Insert (log(n)) (n) (log(n)) Delete (log(n)) (n) (log(n)) 20 A4B33ALG 2011 / 06 B-tree -- Rudolf Bayer, Edward M. McCreight, 1972 All lengths of paths from the root to the leaves are equal. B-tree is perfectly balanced. Keys in the nodes are kept sorted. Fixed k > 1 dictates the same size of all nodes. Each node except for the root contains at least k and at most 2k keys and if it is not a leaf it has at least k+1 and at most 2k+1 children. The root can contain any number of keys from 1 to 2k. If it is not simultaneously a leaf it has at least 2 and at most 2k+1 children. X Y keys < X X < keys < Y Y < keys 21 A4B33ALG 2011 / 06 B-tree -- Find Find 17 18 8 14 1 2 4 5 10 12 15 16 17 26 41 19 20 22 25 27 36 42 45 60 Search in the node is sequential (or binary or other...). If the node is not a leaf and the key is not in the node then the search continues in the appropriate child node. If the node is a leaf and the key is not in the node then the key is not in the tree. 22 A4B33ALG 2011 / 06 B-tree -- Insert B-tree 8 17 26 2 4 10 12 14 16 Insert 5 19 22 25 36 41 42 45 8 17 26 41 2 4 5 10 12 14 16 19 22 25 36 41 42 45 Insert 20 8 17 26 2 4 5 10 12 14 16 19 20 22 25 36 41 42 45 23 A4B33ALG 2011 / 06 B-tree -- Insert Insert 27 2 4 5 8 17 26 10 12 14 16 19 20 22 25 36 41 42 45 27 27 36 41 42 45 Sort outside the tree. Select median, create new node, move to it the values bigger than the median. 41 27 36 42 45 27 8 17 26 41 Try to insert the median into the parent node. 19 20 22 25 27 36 42 45 Success. 24 A4B33ALG 2011 / 06 B-tree -- Insert Insert 15 2 4 5 8 17 26 41 10 12 14 16 19 20 22 25 27 36 42 45 15 10 12 14 15 16 Sort outside the tree. Select median, create new node, move to it the values bigger than the median. 14 10 12 15 16 ? Try to insert the median into the parent node. 14 8 17 26 41 Success? 25 A4B33ALG 2011 / 06 B-tree -- Insert K 15 inserted into a leaf... Key ... 8 17 26 41 ... key 14 goes to parent node 14 2 4 5 10 12 15 16 19 20 22 25 27 36 42 45 The parent node is full – repeat the process analogously. Sort values Select median, create new node, move to it the values bigger than the median together with the corresponding references. Cannot propagate the median into the parent (there is no parent), create a new root and store the median there. 8 14 17 26 41 17 8 14 26 41 17 8 14 26 41 26 A4B33ALG 2011 / 06 B-tree -- Insert Recapitulation - insert 15 8 17 26 41 2 4 5 10 12 14 16 19 20 22 25 27 36 42 45 Insert 15 17 8 14 2 4 5 10 12 15 16 Unchanged nodes 26 41 19 20 22 25 27 36 42 45 Each lavel acquired one new node, a new root was created too, the tree grows upwards and remains perfectly balanced. 27 A4B33ALG 2011 / 06 B-tree -- Delete Delete in a sufficiently full leaf. Delete 4 2 4 5 17 8 14 10 12 26 41 15 16 19 20 22 25 27 36 42 45 60 27 36 42 45 60 17 8 14 2 5 10 12 15 16 26 41 19 20 22 25 28 A4B33ALG 2011 / 06 B-tree -- Delete The deleted key is substituted by the smallest bigger key, like in a BST. Delete in an internal node 17 Delete 17 8 14 2 5 10 12 26 41 19 20 22 25 15 16 27 36 42 45 60 The smallest bigger key is always in the leaf in a B-tree. If the leaf is sufficiently full the delete operation is complete. 19 8 14 2 5 10 12 15 16 26 41 20 22 25 27 36 42 45 60 29 A4B33ALG 2011 / 06 B-tree -- Delete Delete in an insufficiently full leaf. Delete 27 2 5 19 8 14 10 12 The neighbour leaf is sufficiently full. 26 41 15 16 20 22 25 Merge the keys of the two leaves with the dividing key in the parent into one sorted list. 27 36 42 45 60 26 41 42 45 60 36 36 41 42 45 60 Insert the median of the sorted list into the parent and distribute the remainig keys into the left and right children of the median. 26 42 36 41 45 60 30 A4B33ALG 2011 / 06 B-tree -- Delete Recapitulation - delete 27 19 8 14 2 5 10 12 26 41 15 16 20 22 25 27 correctly deleted 27 36 42 45 60 Unchanged nodes 19 8 14 2 5 10 12 15 16 26 42 41 20 22 25 27 41 36 36 42 60 45 45 60 31 A4B33ALG 2011 / 06 B-tree -- Delete Delete in an insufficiently full node. Delete 12 2 5 19 8 14 10 12 None of the neighbours is sufficiently full. 15 16 26 42 41 20 22 25 27 41 36 36 42 60 45 45 60 8 14 10 12 Merge the keys of the node and of one of the neighbours and the median in the parent into one sorted list. Move all these keys to the original node, delete the neighbour, remove the original median and associated reference from the parent. 15 16 8 14 10 14 15 16 32 A4B33ALG 2011 / 06 B-strom -- Delete Deleted 12 19 8 2 5 The parent violates B-tree rules. 10 14 15 16 26 42 41 20 22 25 27 41 36 36 42 60 45 45 60 If the parent of the deleted node is not sufficiently full apply the same deleting strategy to the parent and continue the process towards the root until the rules of B-tree are satisfied. 19 8 19 26 41 26 42 8 26 42 41 8 19 26 42 26 41 33 A4B33ALG 2011 / 06 B-tree -- Delete Recapitulation - delete 12 19 8 14 2 5 10 12 26 42 41 15 16 20 22 25 27 41 36 36 42 60 45 45 60 Key 12 was deleted and the tree was reconstructed accordingly. Unchanged nodes 8 19 26 41 26 42 2 5 10 14 15 16 20 22 25 27 41 36 36 42 60 45 45 60 34