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 70 30 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Left rotation rotateLeft(x) x z x y z D y C B C D A B A February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3
Right rotation rotateRight(z) x z x y z D y C B C D A B A February 26, 2020 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 44 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 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 44 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 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 44 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Left rotation example Left rotation of 32 (completed) 47 40 81 32 44 13 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child temp 32 81 13 40 7 29 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child temp Set x ’s left child to temp ’s right child 32 81 Detach temp ’s right child 13 40 7 29 37 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10
Right rotation example Right rotation of 47 (referred to as x ) 47 Create a pointer to x ’s left child 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 40 7 29 37 February 26, 2020 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 g e h b h b f i a e i a f c c d 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 57 78 21 50 60 February 26, 2020 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 left1, AVLNode right1) { ... } } • 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 27 7 3 16 12 31 7 3 19 44 16 • Imbalanced trees: 27 12 3 12 31 3 16 7 3 19 44 7 5 16 21 9 24 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18
AVL imbalance • 4 cases of imbalance C C A A B A B C A B C B LL imbalance LR imbalance RR imbalance RL imbalance Solve with a right Left rotation Symmetric to left rotation around C around A, becomes imbalance cases LL case February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 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 rebalanceLeft Summary: BST insertion, then fix imbalances moving up towards root else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight 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 93 February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22
AVL insertion example Insert(65) Insert(82) RR imbalance 47 32 71 OK 65 93 OK 82 OK February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23
AVL insertion example Insert(65) Insert(82) 71 Insert(87) 47 93 LR imbalance 32 65 82 OK 87 OK February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24
AVL insertion example Insert(65) Insert(82) OK 71 Insert(87) 47 87 OK 32 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
Recommend
More recommend