# day19 - COP 3503 Computer Science II CLASS NOTES DAY#19...

This preview shows pages 1–4. Sign up to view the full content.

COP 3503 – Computer Science II CLASS NOTES - DAY #19 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): -1 0 0 initial AVL tree Day 19- 1 P S A C B P S A C B P S C B A

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-2 -1 0 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: 0 0 R ebalanced tree is now an AVL tree Day 19- 2 S P A B C
Example – Case 1 Insertion initial AVL tree initial balance factors AVL tree after insertion of 20 balance factors after insertion Perform right rotation of 30 about 40 (40 is the unbalanced node).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

day19 - COP 3503 Computer Science II CLASS NOTES DAY#19...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online