cs 225
play

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

CS 225 Data Structures Feb. 28 AVL L Tre rees Wad ade Fag agen-Ulm lmschneid ider Course Logistics Update CBTF exams will go on as-scheduled: Theory Exam 2 is ongoing Sample Exam available on PL MPs and Lab assignments will be


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

  2. Course Logistics Update CBTF exams will go on as-scheduled: • Theory Exam 2 is ongoing • Sample Exam available on PL MPs and Lab assignments will be due on schedule: • MP4 is released; due March 12, 2018 • lab_huffman is released later today My office hours are cancelled today.

  3. Lab Sections All lab sections are not meeting this week. Instead, all CAs and non-striking TAs will hold open office hours (using the regular queue, held in the basement): • Feel free to use the room to work with your peers on the lab. Staff will be available in open office hours in the basement of Siebel. • An intro video on Huffman trees will be provided.

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

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

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

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

  8. 38 13 51 10 25 37

  9. 38 13 51 10 25 37

  10. 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:

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

  12. AVL Tree Rotations Four templates for rotations:

  13. 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 ______.

  14. 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

  15. _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 };

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

  17. template <class T> void AVLTree<T>::_insert(const T & x, treeNode<T> * & t ) { 1 2 if( t == NULL ) { 3 t = new TreeNode<T>( x, 0, NULL, NULL); 4 } 5 6 else if( x < t->key ) { 7 _insert( x, t->left ); 8 int balance = height(t->right) - height(t->left); 9 int leftBalance = height(t->left->right) - height(t->left->left); 10 if ( balance == -2 ) { 11 if ( leftBalance == -1 ) { rotate_____________( t ); } 12 else { rotate_____________( t ); } 13 } 14 } 15 16 else if( x > t->key ) { 17 _insert( x, t->right ); 18 int balance = height(t->right) - height(t->left); 19 int rightBalance = height(t->right->right) - height(t->right->left); 20 if( balance == 2 ) { 21 if( rightBalance == 1 ) { rotate_____________( t ); } 22 else { rotate_____________( t ); } 23 } 24 } 25 26 t->height = 1 + max(height(t->left), height(t->right)); 27 }

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

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

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

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

Recommend


More recommend