You are on page 1of 14

Deletion algorithm

Analysis

Lecture 11: AVL-tree 3


CSC2100 Data Structure
Yufei Tao
CSE department, CUHK

June 23, 2011

Summary

Deletion algorithm

Analysis

Summary

We have learned how to perform insertions in a binary search tree.


In this lecture, we will make the structure fully dynamic by
supporting deletions as well.

Deletion algorithm

Deletion algorithm
BST principle
Balance principle

Analysis

Analysis

Summary

Deletion algorithm

Principles

BST.
Balanced.

Analysis

Summary

Deletion algorithm

Analysis

Summary

Finding the node to be deleted

Same as answering a point-search query. Cost = O(log n), where n


is the number of nodes in the tree.
Example
Assume that we want to delete 60:
30

15

10

40

20

35

36

73

60

Deletion algorithm

Analysis

Summary

BST principle

Maintaining the BST principle

Let u be the node to be deleted. If u is a leaf node, simply remove


it.
Example
30

30

15

15

40

40

10

20

35

36

10

73

60

20

35

36

73

Deletion algorithm

Analysis

Summary

BST principle

Maintaining the BST principle (cont.)


If u is a non-leaf node and has a right subtree, replace u with its
successor.
Example
Delete 40:
30

30

15

15

40

60

10

20

35

36

10

73

60

20

35

73

36

The successor can be found in O(log n) time, as discussed in the


tutorial.

Deletion algorithm

Analysis

Summary

BST principle

Maintaining the BST principle (cont.)


Otherwise, u must be a non-leaf node with a left subtree. In this case,
replace u with its predecessor.
Example
Delete 73:
30

30

15

15

40

40

10

20

35

36

10

73

60

20

35

60

36

Note that in any case the deletion algorithm so far accesses a single
root-to-leaf path. Let us call it the deletion path.

Deletion algorithm
Balance principle

Balance maintenance

Same as in insertions.

Analysis

Summary

Deletion algorithm

Analysis

Summary

Balance principle

Example 1
Delete 30:
30

35

15

15

40

40

10

20

10

35

20

Node 35 now becomes unbalanced. This is a left-left case, which should


be handled by a rotation:
35

15

15
40

10

35

10

20

20

40

Deletion algorithm

Analysis

Summary

Balance principle

Example 2
Delete 40:
30

30

15

15

40

73

10

20

35

10

73

35

20

36

36

Node 73 becomes unbalanced. This is a left-right case, which should be


handled by a double rotation:
30

30

15

73

15

36

10

20

35

36

10

20

35

73

Deletion algorithm

Analysis

Summary

Balance principle

Example 3

Delete 40:
30

35

15

15

40

40

10

20

25

10

35

20

25

Node 35 becomes unbalanced. Both subtrees of 15 are equally tall. In


this case, regard it as a left-left case (handle by a single rotation)

Deletion algorithm

Analysis

Summary

Deletion cost

...

Only the nodes along the deletion path may become


unbalanced.
Since fixing each unbalanced node takes O(1) time, the total
cost is O(log n).

Deletion algorithm

Analysis

Summary

Playback of this lecture:


Each deletion can be done in O(log n) time.
So far we have completed our discussion of the binary search tree.
We know that this structure consumes O(n) space, and support
each insertion/deletion in O(log n) time, where n is the number of
nodes in the tree.

You might also like