cs 225
play

CS 225 Data Structures Ma March 4 AV AVL Trees G G Carl Evans - PowerPoint PPT Presentation

CS 225 Data Structures Ma March 4 AV AVL Trees G G Carl Evans Resources on the Website https://courses.engr.illinois.edu/cs225/fa2019/pages/lectures.html Le Left R Rot otation on 38 13 51 10 25 40 84 66 89 95 38 13 51


  1. CS 225 Data Structures Ma March 4– AV AVL Trees G G Carl Evans

  2. Resources on the Website • https://courses.engr.illinois.edu/cs225/fa2019/pages/lectures.html

  3. Le Left R Rot otation on 38 13 51 10 25 40 84 66 89 95

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

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

  6. 38 84 13 89 51 10 25 40 66 95

  7. 38 13 51 10 25 37

  8. 38 13 51 10 25 37

  9. BS BST R Rot otation on Su Summary - 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:

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

  11. AV AVL Tree Rotations Four templates for rotations:

  12. Fi Findi nding ng the he Rotation n on n Ins nser ert 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 ______.

  13. Fi Findi nding ng the he Rotation n on n Ins nser ert 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

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

  15. Inser In ertio tion in into an an AVL Tree ee _insert(6.5) 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 1 4 11 2 T key; 3 unsigned height; 4 TreeNode *left; 2 5 TreeNode *right; 6 };

  16. 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 }

  17. 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 };

  18. Hei Heigh ght-Ba Balanced T Tree Height balance: b = height(T R ) - height(T L )

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

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

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

Recommend


More recommend