AVL trees BST rotations AVL introduction February 26
AVL trees BST rotations AVL introduction February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
Rotations • An item must be inserted into a BST at the correct position • The shape of a tree is determined by – The values of the items inserted into the tree – The order in which those values are inserted • This suggests that there is more than one tree (shape) that can contain the same values • A tree’s shape can be altered by rotation while still preserving the BST property – and the in-order traversal is also preserved 70 50 50 30 February 26, 2020 70 30 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Left rotation rotate. Left(x) x z x y z D y C A February 26, 2020 A B C D B Cinda Heeren / Andy Roth / Geoffrey Tien 3
Right rotation rotate. Right(z) x z x y z D y C A February 26, 2020 A B C D B Cinda Heeren / Andy Roth / Geoffrey Tien 4
Left rotation example Left rotation of 32 (referred to as x) 47 Create a pointer to x’s right child 32 81 13 40 temp 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 44 5
Left rotation example Left rotation of 32 (referred to as x) 47 Create a pointer to x’s right child Set x’s right child to temp’s left child 32 81 Detach temp’s left child 13 40 temp 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 44 6
Left rotation example Left rotation of 32 (referred to as x) 47 Create a pointer to x’s right child Set x’s right child to temp’s left child 32 81 Detach temp’s left child Make x the left child of temp 13 40 temp Make temp the left child of x’s parent 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 44 7
Left rotation example Left rotation of 32 (completed) 47 40 32 13 February 26, 2020 81 44 37 Cinda Heeren / Andy Roth / Geoffrey Tien 8
Right rotation example Right rotation of 47 (referred to as x) Create a pointer to x’s left child 47 temp 32 81 13 7 February 26, 2020 40 29 37 Cinda Heeren / Andy Roth / Geoffrey Tien 9
Right rotation example Right rotation of 47 (referred to as x) Create a pointer to x’s left child 47 temp Set x’s left child to temp’s right child 32 81 Detach temp’s right child 13 7 February 26, 2020 40 29 37 Cinda Heeren / Andy Roth / Geoffrey Tien 10
Right rotation example Right rotation of 47 (referred to as x) Create a pointer to x’s left child 47 temp Set x’s left child to temp’s right child 32 81 Detach temp’s right child Make x the right child of temp 13 7 February 26, 2020 40 29 37 Cinda Heeren / Andy Roth / Geoffrey Tien 11
Right rotation example temp Right rotation of 47 (referred to as x) 32 Create a pointer to x’s left child Set x’s left child to temp’s right child 13 47 Detach temp’s right child Make x the right child of temp 7 29 40 81 Make x the new root 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12
Exercise Balancing with rotations • Suggest a sequence of rotations which will produce a perfect binary tree from the starting tree: root 3 7 12 13 18 26 31 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13
AVL trees February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14
BST rotation Summary g e b a g h f b i h a c e f c d i d • Rotations preserve the in-order BST property, while altering the tree structure – we can use rotations to bring imbalanced trees back into balance February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15
AVL trees • An AVL tree is a balanced BST – Each node's left and right subtrees differ in height by at most 1 – Rebalancing via rotations occurs when an insertion or removal causes excessive height difference • AVL tree nodes contain extra information to support this height information 43 19 63 4 38 21 February 26, 2020 57 50 78 60 Cinda Heeren / Andy Roth / Geoffrey Tien 16
AVL nodes enum balance_type {LEFT_HEAVY = -1, BALANCED = 0, RIGHT_HEAVY = +1}; class AVLNode { public: int data; // or template type AVLNode left; AVLNode right; balance_type balance; AVLNode(int value) {. . . } AVLNode(int val, AVLNode left 1, AVLNode right 1) {. . . } } • AVLNode is almost the same as a binary tree node – additional balance field indicates that state of subtree balance at that node – there are many alternate implementations! February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17
AVL imbalance • Balanced trees: 3 3 12 3 7 27 16 12 7 3 19 27 12 3 5 3 12 16 3 7 9 February 26, 2020 44 16 • Imbalanced trees: 7 31 31 19 16 Cinda Heeren / Andy Roth / Geoffrey Tien 44 21 24 18
AVL imbalance • 4 cases of imbalance C B A LL imbalance Solve with a right rotation around C February 26, 2020 C A A A B B LR imbalance C C RR imbalance Left rotation around A, becomes LL case Cinda Heeren / Andy Roth / Geoffrey Tien B RL imbalance Symmetric to left imbalance cases 19
AVL insertion Maintaining balance • The best way to keep a tree balanced, is to never let it become imbalanced! • AVL insertion begins with ordinary BST insertion (i. e. a leaf node) followed by rotations to maintain balance – i. e. AVL properties are satisfied before and after insertion – if the balance attribute of a subtree's root node becomes critical (-2 or +2) as a result of inserting the new leaf, rebalance it! February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20
AVL insertion Pseudocode if root is NULL Create new node containing item, assign root to it, and return true else if item is equal to root->data item exists already, return false else if item < root->data Recursively insert the item into the left subtree if height of left subtree has increased (increase variable is true) balance--; if balance == 0, reset increase variable to false if balance < -1 reset increase variable to false perform rebalance. Left Summary: BST insertion, then fix imbalances moving up towards root else if item > root->data (symmetric to left subtree case, incrementing balance) rebalance. Left and rebalance. Right are the rotations to correct the 4 imbalance cases February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21
AVL insertion example Insert(65) 47 32 71 65 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 93 22
AVL insertion example Insert(65) Insert(82) RR imbalance 47 32 OK 71 93 65 82 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien OK OK 23
AVL insertion example Insert(65) Insert(82) 71 Insert(87) 47 32 93 LR imbalance 65 82 OK 87 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien OK 24
AVL insertion example Insert(65) Insert(82) 71 OK Insert(87) 47 32 87 OK 65 82 93 Complexity? What is the cost of doing one "fix"? How many fixes need to be performed for a single insertion? February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25
Exercise • Starting with an empty AVL tree, insert the keys in the following order: – 1, 3, 5, 7, 9, 8, 6, 4, 2 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26
Readings for this lesson • Carrano & Henry – Chapter 19. 4 (end – rotations) – Chapter 19. 5 (AVL trees) • Play with some interactive AVL tree applets – https: //www. cs. usfca. edu/~galles/visualization/AVLtree. html – https: //visualgo. net/bn/bst February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27
- Slides: 27