Sie sind auf Seite 1von 1

Data Structures and Algorithms : CS210A, ESO207A, ESO211

Practice-sheet 5
Date: 17 October, 2012.
1. Given two binary search trees T
1
, T
2
storing n
1
and n
2
elements, design an O(n
1
+n
2
)
time algorithm to merge the two trees T
1
and T
2
.(Note that T
1
and T
2
lose their
identities after merging).
2. Recall the operation rank(x, T) dened for any element x and binary search tree T.
This operation returns the number of elements in T less than x. We used additional
eld size at each node to design an O(log n) time algorithm for rank(x,T).
(a) Write Pseudo-code for Left-Rotate(v) which updates the size eld as well suitably.
(b) Suppose, instead of storing size eld, you store rank of each node explicitly in
the tree. What will be the problem with this solution ?
3. Write pseudo-code for Decrease-key operation on a Binary heap.
4. Given k sorted lists L
1
, ..., L
k
storing n
1
, ...n
k
elements respectively. Let n =

i
n
i
Design an O(nlog k) time algorithm to merge them into a single sorted list.
5. Given an array A storing n numbers. Design an O(n + k log n) time algorithm to
compute k-smallest elements from A. You may modify the array A if you wish.
6. You are given a sequence x
0
, . . . , x
n1
of n numbers. Design a suitable data structure
which can perform each of the following operations in O(log n) time for any 0 i
j < n.
Report sum(i, j): Report the sum of all numbers {x
i
, ..., x
j
}.
Update(i, ): Add to the current value of x
i
.
7. You are given a sequence S = b
0
, . . . , b
n1
of n bits. Design a suitable data structure
which can perform each of the following operations in O(log n) time for any 0 i < n.
Report longest sequence: Report the length of the longest contiguous subsequence
of 1s in the sequence S.
Flip bit(i): Flip bit b
i
.
1 Slightly dicult problem
1. You are given a height balanced tree T storing n elements. Design an algorithm which
takes O(k + log n) time to compute k predecessors of any given element x in tree T.
2. You are given a binary heap H of size n, a number x, and a positive integer k. Design
an O(k) time algorithm to determine if x is smaller than kth smallest element in H.
You may use O(k) extra space.
Hint: If you are not able to solve this problem, most likely it is because you are
interpreting this problem in a wrong way.
1

Das könnte Ihnen auch gefallen