VIEWS: 4 PAGES: 14 CATEGORY: Computers & Internet POSTED ON: 5/1/2010 Public Domain
COP 3503H – Computer Science I – CLASS NOTES - DAY #26 Supplement More on Tree Rotations Single Rotations Recall that the insertion of a new node into an AVL tree could occur in one of four different ways (two of which are symmetric to each other): 1. The new node is in the left subtree of the left child of a node. 2. The new node is in the right subtree of the left child of a node. 3. The new node is in the left subtree of the right child of a node. 4. The new node is in the right subtree of the right child of a node. Cases 1 and 4 are symmetric as are cases 2 and 3. Whenever an insertion of case 1 or 4 occurs, the resulting tree will be unbalanced and a single rotation can be used to rebalance the tree. Consider the following case 1 situation (insertion in left subtree of a left child): P -1 S 0 C 0 A B initial AVL tree Day 26 Supplement- 1 P -2 S -1 C 0 B A tree immediately after insertion into subtree A The new node has been inserted into the left subtree (A) of node S which is the left child of node P. The tree is now unbalanced and is temporarily not an AVL tree. To restore the balance in the tree and return it to the status of an AVL tree, a single rotation of S about P is needed as shown below: S 0 P 0 A B C Rebalanced tree is now an AVL tree Day 26 Supplement- 2 Example – Case 1 Insertion 40 -1 30 50 0 0 25 35 0 0 initial AVL tree initial balance factors 40 -2 30 50 -1 0 25 35 -1 0 20 0 AVL tree after insertion of 20 balance factors after insertion Perform right rotation of 30 about 40 (40 is the unbalanced node). 40 30 30 50 25 40 25 35 20 35 50 20 Day 26 Supplement- 3 The steps in the right rotation are: 1. Interchange nodes 30 and 40. 2. Interchange causes node 40 to move to right subtree of 30. 3. Former right subtree of 30 becomes left subtree of 40. 4. Left subtree of 30 is unchanged. 5. Right subtree of 40 is unchanged. You should verify that the resulting tree is AVL balanced. Now consider a case 4 situation (insertion in right subtree of a right child): +1 P 0 C S A B initial AVL tree P +2 C S +1 A B tree immediately after insertion into subtree B Day 26 Supplement- 4 The new node has been inserted into the right subtree (B) of node S which is the right child of node P. The tree is now unbalanced and is temporarily not an AVL tree. To restore the balance in the tree and return it to the status of an AVL tree, a single rotation of S about P is needed as shown below: 0 S 0 P B C A final AVL tree Example – Case 4 Insertion 40 +1 30 50 0 0 45 55 0 0 initial AVL tree initial balance factors Day 26 Supplement- 5 40 +2 30 50 0 +1 45 55 0 +1 65 0 AVL tree after insertion of 65 balance factors after insertion Perform left rotation of 50 about 40 (40 is the unbalanced node). 40 50 30 50 40 55 45 55 30 45 65 0 65 0 The steps in the left rotation are: 1. Interchange nodes 40 and 50. 2. Interchange causes node 40 to move to left subtree of 30. 3. Former left subtree of 50 becomes the right subtree of 40. 4. Left subtree of 40 is unchanged. 5. Right subtree of 50 is unchanged. Day 26 Supplement- 6 Notice the symmetry of the two final AVL trees after the insertions and subsequent rebalancing. S S P P A B B C C A case 1 final AVL tree case 4 final AVL tree Double Rotations Cases 2 and 3 when inserting into an AVL tree require a double rotation to rebalance the tree. Case 2 occurs when the new node is in the left subtree of the right child of a node. Case 3 occurs when the new node is in the right subtree of the right child of a node. As before, cases 2 and 3 are symmetric. Consider the following case 2 situation (insertion in left subtree of a right child): G 0 P +1 A S 0 D B C initial AVL tree Day 26 Supplement- 7 -2 G P +2 A S -1 D C B tree immediately after insertion into subtree B The new node has been inserted into subtree B which is left subtree of node S which is a right child of P. The tree is now unbalanced and is temporarily not an AVL tree. To restore the balance in the tree and return it to the status of an AVL tree, a double rotation of S about P followed by S about G is needed as shown below. While the double rotation may seem confusing it is in reality two single rotations as shown below: (1) rotate G’s grandchild about G’s child [rotate S about P] G P A S D C B initial unbalanced tree Day 26 Supplement- 8 G S P C D A B rotation of S about P (first rotation – tree still unbalanced) S P G A C B D rotation of S about G (second rotation – tree becomes balanced) Day 26 Supplement- 9 S P G A C D B final AVL tree (balanced) Example – Case 2 Insertion (double left/right rotation) 40 -1 30 50 0 0 25 35 0 0 initial AVL tree initial balance factors 40 -2 30 50 +1 0 25 35 0 -1 32 0 AVL tree after insertion of 32 balance factors after insertion Day 26 Supplement- 10 Perform double rotation: 1st do 35 about 30 followed by 35 about 40. 40 40 30 50 35 50 step 1 25 35 30 32 25 32 tree after insertion tree after first rotation of 35 about 30 -2 40 -2 0 35 50 step 2 0 30 0 0 25 32 balance factors after first rotation rotate 35 about 40 35 0 30 40 0 +1 25 32 50 0 0 0 tree after second rotation of 35 about 40 final balance factors Day 26 Supplement- 11 The steps in the double rotation are: 1. Interchange parent and grandparent of new node (nodes 35 and 30). 2. Interchange previous parent and great-grandparent (noded 35 and 40). Example – Case 3 Insertion (double right/left rotation) 40 +1 30 50 0 0 45 65 0 0 initial AVL tree initial balance factors 40 +2 30 50 0 -1 45 65 +1 0 48 0 AVL tree after insertion of 48 balance factors after insertion Day 26 Supplement- 12 Perform double rotation: 1st do 45 about 50 followed by 45 about 40. 40 40 30 50 30 45 step 1 45 65 50 48 48 65 tree after insertion tree after first rotation of 45 about 50 +2 40 0 +2 30 45 step 2 0 50 0 0 48 65 balance factors after first rotation rotate 45 about 40 45 0 40 50 -1 0 30 48 65 0 0 0 tree after second rotation of 45 about 40 final balance factors Day 26 Supplement- 13 The steps in the double rotation are: 1. Interchange parent and grandparent of new node (nodes 35 and 30). 2. Interchange previous parent and great-grandparent (noded 35 and 40). Day 26 Supplement- 14