avl trees

46
AVL Tree Searching Finding min/max Insertion Deletion

Transcript of avl trees

Page 1: avl trees

AVL Tree

SearchingFinding min/maxInsertionDeletion

Page 2: avl trees

AVL Tree

AVL Tree is a binary search tree that satisfies For each node, the height of the left and right subtrees can differ

by at most 1 Recall that the height of a node is defined as the length of the

longest path from that node to a leaf node Define the height of an empty tree to be -1 for convenience Usually, the height of every node are stored in the implementation

A tree satisfying this property can be proven height = O(log n), since it is an almost balanced tree Proof can be found in Lecture note Fast operations (search, insert, delete) can be supported

Page 3: avl trees

AVL Tree Example

Which of the following is an AVL tree?10

6

1

8

15

18

13

no

10

6

1

8

15

13

18

no(this is not a BST)

13

6

1

8

15

18

10

yes

Page 4: avl trees

AVL Tree - Operations

Operations we will consider in this Searching Finding minimum/maximum Insertion Deletion

Searching and finding min/max are the same as BST But now it is guaranteed to finish in O(log n) time

Page 5: avl trees

Insertion in AVL Tree

Basically, insertion can be done as in BST Eg. Insert 7 and 14 into the following tree

13

6

1

8

15

18

10

Insert 7

13

6

1

8

15

18

10

7

Page 6: avl trees

Insertion in AVL Tree

AVL Property may be violated after insertion We need to restore the AVL property

13

6

1

8

15

18

10

7

Insert 14

13

6

1

8

15

18

10

7 14

Page 7: avl trees

Which nodes may violate AVL property? Before talking about how to maintain the AVL

property, how can we tell whether the AVL property is violated after insertion? Checking the height of subtrees in every node

Too slow (O(n) time to check all nodes) Note that all non-ancestors nodes remain OK

AVL property on these nodes must still be satisfied No need to check them

Checking the direct parent of the inserted node? No, the direct parent of the inserted node should not

violate AVL property.

Page 8: avl trees

Which nodes may violate AVL property? Checking the grandparent of the inserted node?

Yes In general, checking all ancestors of the inserted node

After insertion, the height of subtrees of ancestors are updated

13

8

15

18

14

21

13

8

15

18

21

Insert 14

Page 9: avl trees

Which nodes may violate AVL property? Therefore, we need to check whether the AVL

property holds for all ancestors of the inserted node (up to the root) Indeed, you may skip the checking of its parent

and the inserted node itself If there is no ancestor violate AVL property

We are done Otherwise, we need to restore the AVL

property at that node

Page 10: avl trees

Restore AVL property (Case 0) A node is inserted in T1 or T2

AVL property cannot be violated at node x The height of T1 and T2 can be

increased by at most 1 after insertion AVL property can only be

violated if the height of T1 and T2 differ by 1 originally, and an insertion is occurred at the taller subtree to make it even taller

x

T1 T2 hh

...

Page 11: avl trees

Restore AVL property (Case 1) A node is inserted in T1 and AVL property is

violated in node x And we assume node x is the lowest node that

violate the AVL property

x

y

T1 T2

T3 h

hh + 1

h + 2

...x

y

T1 T2

T3 h

hh

h + 1

...

insert

Page 12: avl trees

Restore AVL property (Case 1) Swap node x and node y

After swapping, the subtrees T1, T2 and T3 must be in their final position From BST property, we have T1 < y < T2 < x < T3 < …

x

y

T1 T2

T3 h

hh + 1

h + 2

...

Single Right Rotationx

y

T1

T2 T3 hh

h + 1 h + 1

...

Page 13: avl trees

Restore AVL property (Case 4) Symmetric case as case 1

Occurs if we insert a node in T3

x

y

T3

T2T1h h

h + 1h + 1

...x

y

T3T2

T1h

h h + 1

h + 2

...

Single Left Rotation

Page 14: avl trees

Restore AVL property (Case 2) Single rotation may fail if a node is inserted in

T2 In this case, we need double rotation

x

y

T1

T2 T3 hh + 1

h h + 2

...

x

y

T1 T2

T3 h

h + 1h

h + 2

...Single Right Rotation

Page 15: avl trees

Restore AVL property (Case 2) Further examine the subtree T2

A node is inserted either in T2a or T2b

x

y

T1

T2a

T3 h

h + 1

h

h + 2

...

T2b

z

x

y

T1 T2

T3 h

h + 1h

h + 2

...

Page 16: avl trees

Restore AVL property (Case 2) Relocate node x, y and z

From BST property, we have T1 < y < T2a < z < T2b < x < T3

h o r

h - 1

y

z

T1 T2a T3 hh + 1

h

...

T2b

x

h o r

h - 1

h + 1

x

y

T1

T2a

T3 h

h + 1

h

h + 2

...

T2b

z

Double Left Rotation

Page 17: avl trees

Restore AVL property (Case 3) Symmetric case as case 2

Occurs if we insert a node in T2a or T2b

h o r

h - 1

x

z

T1 T2a T3 hh + 1

h

...

T2b

y

h o r

h - 1

h + 1

x

y

T1

T2a

T3

h + 1

h

h + 2

...

T2b

z

h

Double Right Rotation

Page 18: avl trees

Why it is called “Double” rotation? Double Left Rotation is equivalent to two

single rotations Single Left Rotation at y Then, Single Right Rotation at x

Similarly, Double Right Rotation is equivalent to Single Right Rotation at y Then, Single Left Rotation at x

Page 19: avl trees

Why it is called “Double” rotation?

x

y

T1

T2a

T3 h

h + 1

h

h + 2

...

T2b

z

Single Left Rotation(y)

h o r

h - 1

y

z

T1 T2a T3 hh + 1

h

...

T2b

x

h o r

h - 1

h + 1

x

z

T1 T2a

T3

...

T2b

y

Single Right Rotation(x)

Double Left Rotation(x)

Page 20: avl trees

Insertion in AVL Tree – Time Complexity Time complexity

Find the location to insert O(log n)

Checking and rotation (if needed) for a node O(1)

Checking and rotation for all ancestors of the inserted node O(log n)

Overall, time complexity of insertion is O(log n)

Page 21: avl trees

Continue to examine its ancestors? After a rotation, it is easy to see that the AVL

property is fixed at the violated node x Recall that the AVL property can only be

violated at ancestors of the inserted node Then, shall we continue to examine the ancestors

of x? No

Page 22: avl trees

Continue to examine its ancestors? We can study all 4 cases, but let’s consider

case 1 here If node x is the root, clearly no further examination

is needed Otherwise, we can assume node x has a parent

(say, node v)

Page 23: avl trees

Continue to examine its ancestors?

x

y

T1

T2 T3 hh

h + 1

h + 2

...

v

T4

h + 1 ,

h + 2o r

h + 3

h + 3o r

h + 4

x

y

T1 T2

T3 h

hh

h + 1

v

T4

h + 2

h + 1 ,

h + 2o r

h + 3

...

h + 3o r

h + 4

insert

h + 1

x

y

T1 T2

T3 h

h

h + 2

...

v

h + 3

T4

h + 1 ,

h + 2o r

h + 3

Single Right Rotation(x)

Page 24: avl trees

Continue to examine its ancestors? In node v

No violation of AVL property in node v after rotation AND

Height of node v before and after insertion (with rotation) remains constant (either h+3 or h+4)

Therefore the AVL property will not be violated in the parent (or any ancestor) of node v

Therefore, no further examination is needed

Page 25: avl trees

Summary: Insertion in AVL tree Perform BST insertion For each ancestor x of the inserted node,

Update its height by If AVL property is violated at node x

If h(x.left) = h(x)-1 (i.e. h(x.right) = h(x)-3) If h(x.left.left) = h(x)-2 Single Right Rotation(x) [case 1] Else If h(x.left.right) = h(x)-2 Double Left Rotation(x) [case 2]

If h(right(x)) = h(x)-1 If h(x.right.left) = h(x)-2 Double Right Rotation(x) [case 3] Else If h(x.right.right) = h(x)-2 Single Left Rotation(x) [case 4]

Finish // early termination Caution: don’t forget

Rotation may change x, so remember to connect the resulting tree to x.parent

Update the height of nodes involved in rotations

Page 26: avl trees

Review: Deletion in BST

There are 3 cases Deleted node is a leaf Deleted node has one child Deleted node has two children

target

x

T1

...

target

x

T2

...

T1

target

x

T3

...

T1 T2min

Page 27: avl trees

Deletion in AVL Tree

Perform BST deletion Similar to insertion, we have to restore the

AVL property after deletion

Page 28: avl trees

Which nodes may violate AVL property? Recall the three cases

Deleted node is a leaf Height of x may be modified

Deleted node has one child Height of x may be modified

Deleted node has two children Height of the parent of the minimum node of right

subtree of x may be modified And the height of all ancestors of the altered node

may need to be update As before, consider node x is the lowest node that violate

the AVL property after deletion

Page 29: avl trees

Restore AVL property (Case 0) A node is deleted in T2

AVL property cannot be violated at node x The height of T2 can be decreased by

at most 1 after deletion AVL property can only be violated if

the height of T2 is less than that of T1 by 1 originally, and a node is deleted from T2 to make T2 even shorter by 1 Symmetrically, similar case for T1

h + 1

x

T2

h + 1o r

h

...

T1

h + 2

x

T1 T2 h + 1

...

Page 30: avl trees

Restore AVL property (Case 1) A node is deleted from T3 and AVL property

is violated in node x

deletex

y

T2

T3 h + 1

h + 1

h + 2

...

T1

ho r

h + 1

deletex

y

T2

T3 h

h + 1

h + 2

...

T1

ho r

h + 1

Page 31: avl trees

Restore AVL property (Case 1) Swap node x and node y

After swapping, the subtrees T1, T2 and T3 must be in their final position From BST property, we have T1 < y < T2 < x < T3 < …

x

y

T1

T3 h

ho r

h + 1

h + 1

h + 1o r

h + 2

...

T2

Single Right Rotationx

y

T2

T3 h

ho r

h + 1

h + 1

h + 2

...

T1

Page 32: avl trees

Restore AVL property (Case 4) Symmetric case as case 1

Occurs if we delete a node from T1

x

y

T3

T2T1h

h + 1

h + 1o r

h + 2

...

ho r

h + 1

x

y

T3T2

T1h

ho r

h + 1

h + 1

h + 2

...

Single Left Rotation

Page 33: avl trees

Rotating Nodes

M(1)

J(1) S(1)

U(0)Q(0)

T(0) V(0)

L(0)

delete(L)

S(0)

M(0) U(0)

T(0) V(0)Q(0)J(0)

Deleting a node in the left sub-tree (M’s balance becomes 2).Need to rotate M with it’s right sub-tree.S’s balance factor is 1 before rotate.Just do a rotate left of node S.Notice that the height of the tree does change in this case.

tT1

Page 34: avl trees

Rotating Nodes

M(1)

J(0) S(0)

U(0)Q(0)

delete(J)

S(-1)

M(1) U(0)

Deleting a node in the left sub-tree (M’s balance becomes 2).Need to rotate M with it’s right sub-tree.S’s balance factor is 0 before rotate.Just do a rotate left of node S.Notice that the height of the tree does not change in this case.

Q(0)

Page 35: avl trees

Rotating Nodes

M(1)

J(1) S(-1)

U(0)Q(-1)

N(0)

L(0)

delete(L)

Q(0)

M(0) S(1)

U(0)N(0)J(0)

Deleting a node in the left sub-tree (M’s balance becomes 2).Need to rotate M with it’s right sub-tree.S’s balance factor is -1 before rotate.Need to do a right rotate of Q with S and then a left rotate of Q with M.Notice that the height of the tree changes.

Page 36: avl trees

Rotating Nodes

M(1)

J(1) S(-1)

U(0)Q(1)L(0)

delete(L)

Q(0)

M(-1) S(0)

U(0)J(0)

Deleting a node in the left sub-tree (M’s balance becomes 2).Need to rotate M with it’s right sub-tree.S’s balance factor is -1 before rotate.Need to do a right rotate of Q with S and then a left rotate of Q with M.Notice that the height of the tree changes.

R(0)

R0)

Page 37: avl trees

Restore AVL property (Case 2) Single rotation may also fail as in deletion

In this case, we need double rotation

x

y

T1

T2 T3 hh + 1

h h + 2

...

Single Right Rotationx

y

T2

T3 h

h + 1h

h + 2

...

T1

Page 38: avl trees

Restore AVL property (Case 2) Further examine the subtree T2

The height of T2a and T2b can either be h or h-1

x

y

T1

T2a

T3 h

h + 1

h

h + 2

...

T2b

z

x

y

T2

T3 h

h + 1h

h + 2

...

T1

Page 39: avl trees

Restore AVL property (Case 2) Swap the order of x, y, z, so that two of them

are children of the other one

h o r

h - 1

y

z

T1 T2a T3 hh + 1

h

...

T2b

x

h o r

h - 1

h + 1

Double Left Rotationx

y

T1

T2a

T3 h

h + 1

h

h + 2

...

T2b

z

Page 40: avl trees

Restore AVL property (Case 3) Symmetric case as case 2

Occurs if we delete a node from T1

h o r

h - 1

x

z

T1 T2a T3 hh + 1

h

...

T2b

y

h o r

h - 1

h + 1

x

y

T1

T2a

T3

h + 1

h

h + 2

...

T2b

z

h

Double Right Rotation

Page 41: avl trees

Summary: Restore AVL property After deletion, you may need to check the

AVL property for all ancestors of the last deleted node

If AVL property violated, you may need to perform either single rotation or double rotation to fix it

Page 42: avl trees

Deletion in AVL Tree – Time Complexity Time complexity

Find the location to delete O(log n)

Checking and rotation (if needed) for a node O(1)

Checking and rotation for all ancestors of the last deleted node O(log n)

Overall, time complexity of deletion is O(log n)

Page 43: avl trees

Continue to examine its ancestors? After a rotation, it is easy to see that the AVL

property is fixed at the violated node x Could we stop checking after one rotation, as

in insertion? No

Page 44: avl trees

Continue to examine its ancestors? Let’s consider the following counter-example

A node is deleted from T3

x

y

T2 T3 hh

h + 1h + 2

...

T1

v

T4 h + 4

x

y

T2

T3 h

hh + 1

h + 2

...

T1

h + 3

v

T4 h + 4

Single Right Rotation(x)

Page 45: avl trees

Continue examine its ancestors? Indeed, all kinds of rotations also need further

examination until we reach the root But, anyway, the time complexity of deletion

is still O(log n)

Page 46: avl trees

Summary: Deletion in AVL tree Perform BST deletion For each ancestor x of the last deleted node,

Update its height by If AVL property is violated at node x

If h(x.left) = h(x)-1 (i.e. h(x.right) = h(x)-3) If h(x.left.left) = h(x)-2 Single Right Rotation(x) [case 1] Else If h(x.left.right) = h(x)-2 Double Left Rotation(x) [case 2]

If h(right(x)) = h(x)-1 If h(x.right.left) = h(x)-2 Double Right Rotation(x) [case 3] Else If h(x.right.right) = h(x)-2 Single Left Rotation(x) [case 4]

// cannot early termination, until we reach the root Caution: don’t forget

Rotation may change x, so remember to connect the resulting tree to x.parent

Update the height of nodes involved in rotations