8

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jordan
  • 4,928
  • 4
  • 26
  • 39
  • 1
    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 Mar 02 '13 at 00:00
  • 1
    I came across this paper: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.1458 – sukunrt Mar 03 '13 at 10:21
  • Could you clarify `half of the total range to be inserted`? Just curious, and couldn't figure it out – keyser Jul 31 '13 at 17:36
  • The worst case is rather good. [Wikipedia](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) 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 Apr 10 '19 at 07:39

2 Answers2

2

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

Leigh
  • 28,765
  • 10
  • 55
  • 103
0

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
Jonathan E. Landrum
  • 2,748
  • 4
  • 30
  • 46
  • just because the tree balances itself, does not mean there is no worst case. in the rb tree instance, the worst case probably involves a sequence of insertions that the self balancing rules operate poorly around – Jordan May 17 '13 at 11:49
  • That's true, there is a worst case, but it won't be a linear array as you've posted. Did I misunderstand your question? Edit: there's a worst case for a given collection of elements. – Jonathan E. Landrum May 17 '13 at 20:09
  • The numbers separated by commas are meant to represent order of insertion not an array. Does that clarify things? – Jordan May 20 '13 at 12:14
  • Yes, and that makes the problem quite interesting. Would be a good thesis topic. – Jonathan E. Landrum May 20 '13 at 13:43