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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
-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
Background image of page 2
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).
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online