7
votes

Lets say we are dealing with the keys 1-15. To get the worst case performance of a regular BST, you would insert the keys in ascending or descending order as follows:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

Then the BST would essentially become a linked list.

For best case of a BST you would insert the keys in the following order, they are arranged in such a way that the next key inserted is half of the total range to be inserted, so the first is 15/2 = 8, then 8/2 = 4 etc...

8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15

Then the BST would be a well balanced tree with optimal height 3.

The best case for a red black tree can also be constructed with the best case from a BST. But how do we construct the worst case for a red black tree? Is it the same as the worst case for a BST? Is there a specific pattern that will yield the worst case?

2
Hey this is a great question, I think. And I am particularly interested in knowing the answer. Perhaps people at cstheory stackexchange can help here. So if you can post it there?sukunrt
Could you clarify half of the total range to be inserted? Just curious, and couldn't figure it outkeyser
The worst case is rather good. Wikipedia says: "The balancing of the tree is not perfect, but it is good enough to allow it to guarantee searching in O(log n) time, where n is the total number of elements in the tree." and: "[...] valuable in [...] real-time applications [...] and the Completely Fair Scheduler used in current Linux kernels and epoll system call implementation uses red–black trees."nalply

2 Answers

2
votes

You are looking for a skinny tree, right? This can be produced by inserting [1 ... , 2^(n+1)-2] in reverse order.

0
votes

You won't be able to. A Red-Black Tree keeps itself "bushy", so it would rotate to fix the imbalance. The length of your above worst case for a Red-Black Tree is limited to two elements, but that's still not a "bad" case; it's what's expected, as lg(2) = 1, and you have 1 layer past the root with two elements. As soon as you add the third element, you get this:

B                   B
 \                 / \
  R       =>      R   R
   \
    R