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 had one child was to replace it with its child – But when the node has two children problems arise • Which child should we replace the node with? – We could solve this by just picking one … • But what if the node we replace it with also has two children? – This will cause a problem February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Removed node has 2 children Remove 32 But 41 already has 2 children and 47 must “adopt” the other child of 32 Let’s try replacing it with one of its Cannot have 3 32 63 subtrees like the children 1-child case e.g. 41 19 41 54 79 10 23 37 44 53 59 96 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3
Find the predecessor • When a node has two children, instead of replacing it with one of its children find its predecessor – A node’s predecessor is the right most node of its left subtree – The predecessor is the node in the tree with the largest value less than the node’s value • The predecessor cannot have a right child and can therefore have at most one child – Why? February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4
Predecessor node 32’s predecessor The predecessor of 32 is the rightmost node in its left subtree 47 The predecessor cannot have a 32 63 right child as then it would not be the 19 41 54 79 predecessor 10 23 37 44 53 59 96 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5
Why use the predecessor? • The predecessor has some useful properties – Because of the BST property it must be the largest value less than its ancestor’s value • It is to the right of all of the nodes in its ancestor’s left subtree so must be greater than them • It is less than the nodes in its ancestor’s right subtree – It can have at most only one child • These properties make it a good candidate to replace its ancestor February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6
What about the successor? • The successor to a node is the left most child of its right subtree – It has the smallest value greater than its ancestor’s value – And cannot have a left child • The successor can also be used to replace a removed node – Pick either one, but be consistent! February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Removed node has 2 children Replacement with successor Remove 32 Find successor and detach 47 32 63 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8
Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 32 63 37 temp 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9
Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 32 63 37 Make successor child of temp removed node’s parent 19 41 54 79 10 23 44 53 59 96 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10
Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 63 37 Make successor child of removed node’s parent 19 41 54 79 10 23 44 53 59 96 7 12 30 57 91 97 In this example the successor had no subtree February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11
Removed node has 2 children Replacement with predecessor Remove 63 Find predecessor 47 32 63 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12
Removed node has 2 children Replacement with predecessor Remove 63 Find predecessor Attach predecessor’s subtree to its parent 47 32 63 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13
Removed node has 2 children Replacement with predecessor Remove 63 Find predecessor Attach predecessor’s subtree to its parent 47 Attach removed node’s 32 63 59 children to predecessor temp 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14
Removed node has 2 children Replacement with predecessor Remove 63 Find predecessor Attach predecessor’s subtree to its parent 47 Attach deleted node’s 32 63 59 children to predecessor temp Attach predecessor to deleted node’s 19 41 54 79 parent 10 23 37 44 53 96 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15
Removed node has 2 children Replacement with predecessor Remove 63 Find predecessor Attach predecessor’s subtree to its parent 47 Attach removed node’s 32 59 children to predecessor temp Attach predecessor to removed node’s 19 41 54 79 parent 10 23 37 44 53 96 7 12 30 57 91 97 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16
BST removal alternatives • An alternative to the removal approach for nodes with 2 children is to replace the data – The data from the predecessor node is copied into the node to be deleted – And the predecessor node is then removed • Using the approach described for removing nodes with one or no children • This avoids some of the complicated pointer assignments February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17
BST efficiency • The efficiency of BST operations depends on the height of the tree – All three operations (search, insert and delete) are O(height ) • If the tree is complete the height is log( n ) – What if it isn’t complete? February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18
Height of a BST Insert 7 7 Insert 4 4 9 Insert 1 Insert 9 1 5 Insert 5 height = log(5) = 2 This is a complete BST February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19
Height of a BST Insert 9 9 Insert 1 1 Insert 7 Insert 4 7 Insert 5 height = n – 1 = 4 = O( n ) 4 This is a linked list with extra unused pointers 5 February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20
Rotations February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21
Balanced binary trees • A binary tree is balanced if – Leaves are all about the same distance from the root – The exact specification varies • Sometimes trees are balanced by comparing the height of nodes – e.g. the height of a node’s right subtree is at most one different from the height of its left subtree (e.g. AVL trees) • Sometimes a tree's height is compared to the number of nodes – e.g. red-black trees February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22
"Balanced" binary trees A A B C B C D E F D E F G February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23
"Imbalanced" binary trees A A B C B D E C D F February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24
Balanced BSTs • It would be ideal if a BST was always close to complete – i.e. balanced • How do we guarantee a balanced BST? – We have to make the structure and / or the insertion and removal algorithms more complex • e.g. red – black trees, AVL trees (next!) February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25
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 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26
Left rotation rotateLeft(x) x z x y z D y C B C D A B A February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27
Right rotation rotateRight(z) x z x y z D y C B C D A B A February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 28
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 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 29
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 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 30
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 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 31
Recommend
More recommend