1 / 14

Case 1: LL single rotation

N2. N2. N1. N1. Case 1: LL single rotation. before rotation. after rotation. N2. N2. N1. N1. Case 4: RR single rotation – mirror image of LL rotation. before rotation. after rotation. N3. N2. N2. N3. N3. N2. N1. N1. N1. Case 2: LR double rotation. N2. N3. N3. N2. N3.

Download Presentation

Case 1: LL single rotation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. N2 N2 N1 N1 Case 1: LL single rotation beforerotation afterrotation

  2. N2 N2 N1 N1 Case 4: RR single rotation – mirror image of LL rotation beforerotation afterrotation

  3. N3 N2 N2 N3 N3 N2 N1 N1 N1 Case 2: LR double rotation

  4. N2 N3 N3 N2 N3 N2 N1 N1 N1 Case 3: RL double rotation – mirror image of LR rotation

  5. 12 12 12 16 16 16 4 14 14 8 14 2 6 9 Exercise: Insert a 7 into thefollowing AVLtree

  6. Removing a node from an AVL Tree Algorithm to remove key x from an AVL tree:If a leaf is reached and x is not found, we’re done.If x is found and has 2 children: - find the logical predecessor y of x - copy y into node containing x - apply this algorithm to delete y from treeIf x is found and has 1 or 0 children: - link parent of x to child of x - delete node containing x - set shorter to trueFor each node on the path from the parent of x to the root: while shorter is true: perform the following adjustments…

  7. N N N N Case 1: balance factor of current node N is E or Height of sub-tree rooted at N has not changed so set shorter to false and process is complete

  8. N N N N Case 2: balance factor of current node N is not E and the taller sub-tree was shortened or Height of sub-tree rooted at N is now shorter so shorter remains true and process continues up to parent node.

  9. M M N M N N Case 3: balance factor of current node N is not E and the shorter sub-tree was shortened Let M be the root of the taller sub-tree of N. Sub-case 3a: the balance factor of M is E N is now unbalanced… …fixed with a single rotation. Overall height of sub-tree has not changed so set shorter to false and we’re done.

  10. M M M N N N Sub-case 3b: the balance factor of M is the same as that of N.For the purposes of illustration, let’s suppose that both M and N are right-high – a similar process can be applied to the left-high case. N is now unbalanced… …fixed with a single rotation. Overall height of sub-tree is now shorter so shorter remains true and process continues up to parent node.

  11. M L M L M L N N N Sub-case 3c: the balance factor of N and M are opposite.For the purposes of illustration, let’s suppose that N is right-high and M is left-high. …fixed with a double rotation. Overall height of sub-tree is now shorter so shorter remains true and process continues up to parent node.

  12. 10 4 12 2 8 1 6 9 5 7 11 13 14 Example: Delete the node containing 1 from the following AVL tree

  13. 10 4 12 2 11 13 14 10 12 11 13 14

  14. Final comments on AVL trees: • the worst case search time is O( log N ) whereas for binary search trees it is O( N ) • it can be shown that rebalancing occurs less than half the time when a node is inserted • if we have relatively few searches compared to insertions and we are doing very few removals, we might favour the use a binary search tree but if the number of searches is high compared to the number of insertions or if we are performing many removals then we should use an AVL tree.

More Related