AVL trees
BST rotations AVL introduction
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
AVL trees BST rotations AVL introduction February 26, 2020 Cinda - - PowerPoint PPT Presentation
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
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
– and the in-order traversal is also preserved
Cinda Heeren / Andy Roth / Geoffrey Tien 2 February 26, 2020
50 30 70 50 30 70
Cinda Heeren / Andy Roth / Geoffrey Tien 3
rotateLeft(x) x y z A B C D x y z A B C D
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 4
rotateRight(z) x y z A B C D x y z A B C D
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 5
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 6
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp Set x’s right child to temp’s left child Detach temp’s left child
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 7
47 32 81 13 40 Left rotation of 32 (referred to as x) 37 44 Create a pointer to x’s right child temp Set x’s right child to temp’s left child Detach temp’s left child Make x the left child of temp Make temp the left child of x’s parent
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 8
Left rotation of 32 (completed) 47 40 81 32 44 13 37
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 9
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 temp Create a pointer to x’s left child
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 10
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child temp
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 11
Right rotation of 47 (referred to as x) 47 32 81 13 40 7 29 37 Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child temp Make x the right child of temp
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 12
32 13 47 7 29 temp 40 81 37 Right rotation of 47 (referred to as x) Create a pointer to x’s left child Set x’s left child to temp’s right child Detach temp’s right child Make x the right child of temp Make x the new root
February 26, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 13
3 7 12 13 18 26 31 root
February 26, 2020
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14
g b h a e i c f d g e h b f i c a d
Cinda Heeren / Andy Roth / Geoffrey Tien 15 February 26, 2020
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16
43 19 63 57 60 50 78 38 4 21
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17
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) { ... } }
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18
3 3 7 12 16 3 7 12 19 3 16 27 31 44
3 7 5 12 16 3 7 9 12 19 3 16 27 31 44 21 24
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19
C B A LL imbalance Solve with a right rotation around C C A B LR imbalance Left rotation around A, becomes LL case A B C RR imbalance Symmetric to left imbalance cases A C B RL imbalance
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21
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 else if item > root->data (symmetric to left subtree case, incrementing balance) rebalanceLeft and rebalanceRight are the rotations to correct the 4 imbalance cases Summary: BST insertion, then fix imbalances moving up towards root
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22
Insert(65) 47 32 71 93 65
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23
Insert(65) Insert(82) 47 32 71 93 65 82 OK OK OK RR imbalance
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24
Insert(65) Insert(82) 71 47 93 82 32 65 Insert(87) 87 OK OK LR imbalance
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25
Insert(65) Insert(82) 71 47 87 82 32 65 Insert(87) OK 93 OK 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 26
February 26, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27