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
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.
SpecialUnion(T,T’)
3
Recall: every element of T is smaller than every element of T’
T’
A trivial algorithm that does not work
Time complexity: O(log n)
4
T
<Height balance lost
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
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
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
T’
Extending the algorithm to the generic problem
8
T
T’
Extending the algorithm to the generic problem
9
T
< <x
T’
Extending the algorithm to the generic problem
10
T
x
T’
Extending the algorithm to the generic problem
11
T
x
T’’
Extending the algorithm to the generic problem
12
T
xv
Extending the algorithm to the generic problem
13
T
x
T’’
v
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
Split(T,x)
15
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
Find-rank(T,x)
17
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
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 ?
Achieving O(log n) time for FindRank(T,x)
20
T
x
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
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
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
Recommended