AVL trees BST removal Rotations February 24, 2020 Cinda Heeren / - - PowerPoint PPT Presentation

avl trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

AVL trees

BST removal Rotations

February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

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

Cinda Heeren / Andy Roth / Geoffrey Tien 2 February 24, 2020

slide-3
SLIDE 3

Removed node has 2 children

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

slide-4
SLIDE 4

Find the predecessor

  • When a node has two children, instead of replacing it with one
  • f 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?

Cinda Heeren / Andy Roth / Geoffrey Tien 4 February 24, 2020

slide-5
SLIDE 5

Predecessor node

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

slide-6
SLIDE 6

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

Cinda Heeren / Andy Roth / Geoffrey Tien 6 February 24, 2020

slide-7
SLIDE 7

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!

Cinda Heeren / Andy Roth / Geoffrey Tien 7 February 24, 2020

slide-8
SLIDE 8

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 8

Replacement with successor

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

slide-9
SLIDE 9

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 9

Replacement with successor

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

slide-10
SLIDE 10

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 10

Replacement with successor

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

slide-11
SLIDE 11

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 11

Replacement with successor

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

slide-12
SLIDE 12

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 12

Replacement with predecessor

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

slide-13
SLIDE 13

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 13

Replacement with predecessor

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

slide-14
SLIDE 14

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 14

Replacement with predecessor

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

slide-15
SLIDE 15

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 15

Replacement with predecessor

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

slide-16
SLIDE 16

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 16

Replacement with predecessor

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

slide-17
SLIDE 17

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

Cinda Heeren / Andy Roth / Geoffrey Tien 17 February 24, 2020

slide-18
SLIDE 18

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?

Cinda Heeren / Andy Roth / Geoffrey Tien 18 February 24, 2020

slide-19
SLIDE 19

Height of a BST

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

slide-20
SLIDE 20

Height of a BST

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

slide-21
SLIDE 21

Rotations

February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

slide-22
SLIDE 22

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

Cinda Heeren / Andy Roth / Geoffrey Tien 22 February 24, 2020

slide-23
SLIDE 23

"Balanced" binary trees

Cinda Heeren / Andy Roth / Geoffrey Tien 23

A C E B D F A C E B D F G

February 24, 2020

slide-24
SLIDE 24

"Imbalanced" binary trees

Cinda Heeren / Andy Roth / Geoffrey Tien 24

A C D B F E A B C D

February 24, 2020

slide-25
SLIDE 25

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!)

Cinda Heeren / Andy Roth / Geoffrey Tien 25 February 24, 2020

slide-26
SLIDE 26

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

Cinda Heeren / Andy Roth / Geoffrey Tien 26 February 24, 2020

50 30 70 50 30 70

slide-27
SLIDE 27

Left rotation

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

slide-28
SLIDE 28

Right rotation

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

slide-29
SLIDE 29

Left rotation example

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

slide-30
SLIDE 30

Left rotation example

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

slide-31
SLIDE 31

Left rotation example

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

slide-32
SLIDE 32

Left rotation example

Cinda Heeren / Andy Roth / Geoffrey Tien 32

Left rotation of 32 (completed) 47 40 81 32 44 13 37

February 24, 2020

slide-33
SLIDE 33

Right rotation example

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

slide-34
SLIDE 34

Right rotation example

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

slide-35
SLIDE 35

Right rotation example

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

slide-36
SLIDE 36

Right rotation example

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

slide-37
SLIDE 37

Exercise

  • Suggest a sequence of rotations which will produce a perfect

binary tree from the starting tree:

Cinda Heeren / Andy Roth / Geoffrey Tien 37

Balancing with rotations

3 7 12 13 18 26 31 root

February 24, 2020

slide-38
SLIDE 38

Readings for this lesson

  • Carrano & Henry

– Chapter 15.3 (Binary search tree) – Chapter 16.3 (Implementation – BST) – Chapter 19.4 (end – rotations)

  • Next class

– Carrano & Henry: Chapter 19.5 (AVL trees)

February 24, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 38