Sei sulla pagina 1di 7

Exercise No.

7
Red-Black Trees RBT Property RBT is a BST with a color bit in each node (red or black). 1. Each node is either red or black 2. Each leaf is a Nil node and colored black 3. If node is red then both it's children are black 4. All the simple paths from a node x to a leaf in the subtree rooted at x have the same number of black nodes Time Complexity The RBT interface supports the following operations in O(log n), for RBT of n nodes: Maximum, Minimum, Search, Predecessor, Successor.

Black Height(x)

For a node x, bh(x) = the number of black nodes in any path from x to a leaf in the subtree rooted at x, not including x itself For a RBT T, bh(T) = the number of nodes in any path from the roo to a leaf, not including the root itself. Lema 1: The subtree rooted at x has at least 2bh(x) -1 inner nodes Lema 1: The height of a RBT of n nodes 2log(n+1)

Rotation

Example
Inserting the keys 41; 38; 31; 12; 19; 8 into an initially empty red-black tree

Question 1
Suggest a data structure based on RBT that supports the following operation and given time complexities: Operation Init Init the DS with n real numbers (unordered) Insert(x) Insert x to the DS findMin Return the value of the minimal element findMax Return the value of the maximal element findMed Return the value of the median element DelMin Remove the minimal element DelMax Remove the maximal element DelMed Remove the median element Solution: 2 Red-Black trees: T1 - All the numbers median T2 - All the numbers median variables: min - minimal value max - maximal value n1 - number of elements in T1 n2 - number of elements in T2 Complexity O(nlogn) O(logn) O(1) O(1) O(logn) O(logn) O(logn) O(logn)

Init:
Sort the numbers (merge sort) O(nlogn) Init min, max, n1, n2 O(1) Insert first half to T1 and second half to T2 2 * (n/2) * log(n/2) = nlog(n/2) = = n(logn - 1) = O(log n) Total: O(nlogn)

Insert(x):
If x med insert x to T1, else to T2 O(log n) Update n1 or n2 correspondingly O(1) Update min/max if necessary O(1) If |n1 - n2| become larger than 1, move element from one tree to another: max(T1) to T2 , or min(T2) to T1 O(log n) Total: O(log n)

findMin/findMax:
return min/max O(1)

findMed:
|T| = number of elements in T If |T1| > |T2| return max(T1), otherwise return min(T2) O(1)

DelMin/DelMax:
Remove max(T2) or min(T1) O(log n) Update n1 or n2 correspondingly O(1) Update min/max O(log n) If |n1 - n2| become larger than 1, move element from one tree to another max(T1) to T2 or min(T2) to T1 O(log n) Total: O(log n)

Del med:
if n1 > n2 then Delete max(T1) if n2 > n1 then Delete min(T2) Total: O(log n) O(log n) O(log n)

Question 2
Suppose that a node x is inserted into a red-black tree with RB-INSERT and then immediately deleted with RB-DELETE. Is the resulting red-black tree the same as the initial red-black tree? Solution: Inserting and immidiately deleting need not yield the same tree

3 Question :
)1. Insert(X) - O(log n )2. Delete(X) - O(log n )1(3. Predecessor(X) O )1(4. Succesor(X) O :Solution

- 2 : , . 1. - X P:X.pred = P ; P.succ.pred = X ; X.succ = P.succ ; P.succ = X )Total time : O(log n 2. - X X X X)Total time : O(log n 3. X )1(Total time : O

4 Question
)2Join(T1, x, T .T1 and T2 are Red-Black trees 2x is an element and T1 < x < T .Build a RBT from the elements in T1, T2 and x

Solution: find bh(T1) and bh(T2) in O(log n), n = |T1| + |T2| 1. If bh(T1) = bh(T2) return the tree:

2. If bh(T1) > bh(T2): o Find in O(log n) a black node y in T1 with the largest key such that bh(y) = bh(T2) o Let's Ty be the tree rooted at y o Replace Ty in Ty U {x} U T2, without breaking the BST property o Color x in red, without breaking the RBT properties

If x and v are red, fix it like in insert Complexity of finding bh(T1) and bh(T2) = O(logn) Complexity of searching node y = O(|bh(T1) - bh(T2)|) = O(log n) Complexity of the merge itself = O(|bh(T1) - bh(T2)|)

Question 5
Split(T, x) T is a Red-Black tree which does not include a node with the key x. Find an algorithm to split T into 2 Red-Black trees T1 and T2 so that T1 < x < T2. Solution:

N is a Nil leaf. 1. Find the search path when searching for x in T. 2. T1 will include: o All the nodes where the right son was taken when searching for x (ai) o All the left sub-trees of these nodes (Ai) 3. T2 will include: o All the nodes where the left son was taken when searching for x (bi) o All the right sub-trees of these nodes (Bi) 4. Find bh(Ai) and bh(Bi) in O(logn) 5. Use the Join(T1, x, T2) with the sub-trees required for building T1 and T2 Complexity of all the joinings:

Potrebbero piacerti anche