Lecture-24-CS210-2012.pptx

23
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 24 Height Balanced Binary Search Trees 10 types of operations, each executed in O(log n) time ! 1

Transcript of Lecture-24-CS210-2012.pptx

Page 1: Lecture-24-CS210-2012.pptx

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 24 Height Balanced Binary Search Trees

• 10 types of operations, each executed in O(log n) time !

1

Page 2: Lecture-24-CS210-2012.pptx

Height Balanced BST(Red-black tree, AVL tree)

Operations you already know1. Search(T,x)2. Insert(T,x)3. Delete(T,x)4. Min(T)5. Max(T)6. Predecessor(T,x)7. Successor(T,x)

A NOTATIONT < T’ :every element of T is smaller than every element of T’.

New operations8. SpecialUnion(T, T’): Given T and T’ such that T < T’, compute T*=TUT’. NOTE: T and T’ don’t exist after the union.

9. Split(T, x): Split T into T’ and T’’ such that T’ < x < T’’.

10. FindRank(T, x): return the total number of elements in T that are smaller than x.

2Every operation in O(log n) time.

Page 3: Lecture-24-CS210-2012.pptx

SpecialUnion(T,T’)

3

Recall: every element of T is smaller than every element of T’

Page 4: Lecture-24-CS210-2012.pptx

T’

A trivial algorithm that does not work

Time complexity: O(log n)

4

T

<Height balance lost

Page 5: Lecture-24-CS210-2012.pptx

Towards an O(log n) time for SpecialUnion(T,T’) …

• Simplifying the problemCan we solve some special cases easily ?

• Solving the simpler version efficiently

• Extending the solution to generic version

5

Page 6: Lecture-24-CS210-2012.pptx

Simplifying the problem

Simplified problem: Given two trees T, T’ of same black height and a key x, such that T<x<T’, transform them into a tree T*=TU{x}UT’

6

T T’

< <

x

Page 7: Lecture-24-CS210-2012.pptx

Solving the simplified problem

Simplified problem: Given two trees T, T’ of same black height and a key x, such that T<x<T’, transform them into a tree T*=TU{x}UT’

7

T

O (1) time

T’

< <

x

Page 8: Lecture-24-CS210-2012.pptx

T’

Extending the algorithm to the generic problem

8

T

Page 9: Lecture-24-CS210-2012.pptx

T’

Extending the algorithm to the generic problem

9

T

< <x

Page 10: Lecture-24-CS210-2012.pptx

T’

Extending the algorithm to the generic problem

10

T

x

Page 11: Lecture-24-CS210-2012.pptx

T’

Extending the algorithm to the generic problem

11

T

x

Page 12: Lecture-24-CS210-2012.pptx

T’’

Extending the algorithm to the generic problem

12

T

xv

Page 13: Lecture-24-CS210-2012.pptx

Extending the algorithm to the generic problem

13

T

x

T’’

v

Page 14: Lecture-24-CS210-2012.pptx

Extending the algorithm to the generic problem

Algorithm for SpecialUnion(T,T’):Let black height of T ≤ black height of T’1. Let x be the node storing smallest element of T’. 2. Delete the node x from T’.3. Keep following left pointer of T’ until we reach a node v such that

1. left(v) is black2. The subtree T” rooted at Left(v) has black height same as that of T

4. left(x) T; 5. right(x) T”;6. Color(x) red;7. Left(v) x; 8. parent(x) v;9. If color(v) is red, remove the color imbalance (like in the usual procedure of insertion in a red-black tree)

14

Page 15: Lecture-24-CS210-2012.pptx

Split(T,x)

15

Page 16: Lecture-24-CS210-2012.pptx

Achieving O(log n) time for Split(T,x)

• Take a scissor and cut T into trees starting from x• Make use of SpecialUnion algorithm.

16

Tx

Page 17: Lecture-24-CS210-2012.pptx

Find-rank(T,x)

17

Page 18: Lecture-24-CS210-2012.pptx

A trivial algorithm for Find-rank(T,x)

Repeatedly find predecessors of xTime complexity: O (k log n) (where k is rank of x in T)

18

T

2

28

46

67

25

5

31 41

35 49

5348

Page 19: Lecture-24-CS210-2012.pptx

Achieving O(log n) time for FindRank(T,x)

19

x

T

View the entire tree T from perspective of the path from x

to the root. How will T look like ?

Page 20: Lecture-24-CS210-2012.pptx

Achieving O(log n) time for FindRank(T,x)

20

T

x

Page 21: Lecture-24-CS210-2012.pptx

An elegant solution

Fields associated with a node v in T: 1. Value(v) 2. Left(v) 3. right (v) 4. parent(v)

Keep one more field

Size(v): The number of nodes stored in the subtree rooted at v.

21

Page 22: Lecture-24-CS210-2012.pptx

Algorithm for FindRank(T,x)

High level description

Modify the algorithm for Search(T,x):• rank 0;• whenever we follow a right link of some node v, increment rank by size(left(v)) + 1

Time complexity of the algorithm : O(log n)

Are we done ? No …(We need to efficiently maintain this field under deletion/insertion)

22

Page 23: Lecture-24-CS210-2012.pptx

Maintaining size field T under insertion/deletion

High level description

• Modify the algorithm for Insert(T,x) and Delete(T,x)

• While performing rotations, show that it takes O(1) time to update the size field of the corresponding nodes.

Time complexity of the modified Insert/Delete operation : … … O(log n) still.

23