cs 225
play

CS 225 Data Structures Feb. 27 AVL L Tre rees Wad ade Fag - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 27 AVL L Tre rees Wad ade Fag agen-Ulm lmschneid ider Left ft Rotation 38 13 51 10 25 40 84 89 66 95 38 13 51 10 25 84 A 89 B C D 38 84 13 51 51 89 10 25 84 A A C B D 89


  1. CS 225 Data Structures Feb. 27 – AVL L Tre rees Wad ade Fag agen-Ulm lmschneid ider

  2. Left ft Rotation 38 13 51 10 25 40 84 89 66 95

  3. 38 13 51 10 25 84 A 89 B C D

  4. 38 84 13 51 51 89 10 25 84 A A C B D 89 B C D

  5. 84 38 51 89 84 13 89 51 10 25 A C B D 40 95 66

  6. 38 13 51 10 25 37

  7. 38 13 51 10 25 37

  8. BST Rotation Summary ry - Four kinds of rotations (L, R, LR, RL) - All rotations are local (subtrees are not impacted) - All rotations are constant time: O(1) - BST property maintained GOAL : We call these trees:

  9. AVL Trees Three issues for consideration: - Rotations - Maintaining Height - Detecting Imbalance

  10. AVL Tree Rotations Four templates for rotations:

  11. Finding the Rotation t t 1 t 2 t 3 t 4

  12. If an insertion occurred in Finding the Rotation subtrees t 3 or t 4 and a subtree was detected at t: t t t 1 t 1 t 2 t 2 t 3 t 4 t 3 t 4

  13. Finding the Rotation t t L t R

  14. Finding the Rotation t t L t R

  15. Finding the Rotation t t L t A t B

  16. Finding the Rotation Theorem: t If an insertion occurred in subtrees t 3 or t 4 and a subtree was detected at t , then a __________ rotation about t restores the balance of the t 1 tree. t 2 We gauge this by noting the balance t 3 t 4 factor of t->right is ______.

  17. Example: 38 13 51 10 25 40 84 89 66

  18. Finding the Rotation t t 1 t 4 t 2 t 3

  19. Theorem: Finding the Rotation If an insertion occurred in subtrees t 2 or t 3 and a subtree t was detected at t : t t 1 t 1 t 4 t 4 t 2 t 3 t 2 t 3

  20. Finding the Rotation Theorem: t If an insertion occurred in subtrees t 2 or t 3 and a subtree was detected at t , then a __________ rotation about t restores the balance of the t 1 tree. t 4 We gauge this by noting the balance factor of t->right is ______. t 2 t 3

  21. _insert(6.5) Insertion into an AVL Tree In 8 5 10 3 6 9 12 1 struct TreeNode { 7 4 11 1 2 T key; 3 unsigned height; 4 TreeNode *left; 2 5 TreeNode *right; 6 };

  22. _insert(6.5) In Insertion into an AVL Tree Insert (pseudo code): 1: Insert at proper place 2: Check for imbalance 3: Rotate, if necessary 8 4: Update height 5 10 3 6 9 12 1 struct TreeNode { 7 4 11 1 2 T key; 3 unsigned height; 4 TreeNode *left; 2 5 TreeNode *right; 6 };

  23. 151 template <typename K, typename V> 152 void AVL<K, D>::_insert(const K & key, const V & data, TreeNode *& cur) { 153 if (cur == NULL) { cur = new TreeNode(key, data); } 157 else if (key < cur->key) { _insert( key, data, cur->left ); } 160 else if (key > cur->key) { _insert( key, data, cur->right );} 166 _ensureBalance(cur); 167 }

  24. 119 template <typename K, typename V> 120 void AVL<K, D>::_ensureBalance(TreeNode *& cur) { 121 // Calculate the balance factor: 122 int balance = height(cur->right) - height(cur->left); 123 124 // Check if the node is current not in balance: 125 if ( balance == -2 ) { 126 int l_balance = height(cur->left->right) - height(cur->left->left); 127 if ( l_balance == -1 ) { ____________________________; } 128 else { ____________________________; } 129 } else if ( balance == 2 ) { 130 int r_balance = height(cur->right->right) - height(cur->right->left); 131 if( r_balance == 1 ) { _____________________________; } 132 else { _____________________________; } 133 } 134 135 _updateHeight(cur); 136 };

  25. 8 5 10 3 6 9 12 4 7 11 1 2

  26. AVL Tree Analysis We know: insert, remove and find runs in: __________. We will argue that: h = _________.

  27. AVL Tree Analysis Definition of big-O: …or, with pictures:

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend