AVL trees
BST removal Rotations
February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
AVL trees BST removal Rotations February 24, 2020 Cinda Heeren / - - PowerPoint PPT Presentation
AVL trees BST removal Rotations February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Removing a node with 2 children The most difficult case is when the node to be removed has two children The strategy when the removed node
February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
Cinda Heeren / Andy Roth / Geoffrey Tien 2 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 3
47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 57 91 97 Remove 32 Let’s try replacing it with one of its subtrees like the 1-child case e.g. 41 But 41 already has 2 children and must “adopt” the other child of 32 Cannot have 3 children
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 4 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 5
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 32’s predecessor The predecessor of 32 is the rightmost node in its left subtree The predecessor cannot have a right child as then it would not be the predecessor
February 24, 2020
greater than them
Cinda Heeren / Andy Roth / Geoffrey Tien 6 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 7 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 8
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach
temp
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 9
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach
temp
37
temp
Attach removed node’s children to its successor
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 10
57 59 54 53 44 41 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach Attach removed node’s children to its successor 37
temp
Make successor child of removed node’s parent
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 11
57 59 54 53 44 41 30 12 7 10 23 19 47 63 79 96 91 97 37 Remove 32 Find successor and detach Attach removed node’s children to its successor Make successor child of removed node’s parent In this example the successor had no subtree
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 12
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97
temp
Remove 63 Find predecessor
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 13
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97
temp
Remove 63 Find predecessor Attach predecessor’s subtree to its parent
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 14
57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97
temp
Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59
temp
Attach removed node’s children to predecessor
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 15
57 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59
temp
Attach deleted node’s children to predecessor Attach predecessor to deleted node’s parent
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 16
57 54 53 44 41 37 30 12 7 10 23 19 32 47 79 96 91 97 Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59
temp
Attach removed node’s children to predecessor Attach predecessor to removed node’s parent
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 17 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 18 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 19
Insert 7 Insert 4 Insert 1 Insert 9 Insert 5 7 4 1 9 5 This is a complete BST height = log(5) = 2
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 20
Insert 9 Insert 1 Insert 7 Insert 4 Insert 5 This is a linked list with extra unused pointers 9 1 7 4 5 height = n – 1 = 4 = O(n)
February 24, 2020
February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21
Cinda Heeren / Andy Roth / Geoffrey Tien 22 February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 23
A C E B D F A C E B D F G
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 24
A C D B F E A B C D
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 25 February 24, 2020
– and the in-order traversal is also preserved
Cinda Heeren / Andy Roth / Geoffrey Tien 26 February 24, 2020
50 30 70 50 30 70
Cinda Heeren / Andy Roth / Geoffrey Tien 27
rotateLeft(x) x y z A B C D x y z A B C D
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 28
rotateRight(z) x y z A B C D x y z A B C D
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 29
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 30
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 31
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 32
Left rotation of 32 (completed) 47 40 81 32 44 13 37
February 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 33
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 34
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 35
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 36
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 24, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 37
3 7 12 13 18 26 31 root
February 24, 2020
February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 38