2

Prove that given a number we can find whether there're 2 elements in a red/black tree that their sum equals that number in $\Theta(n)$ time and constant space.

The original problem appears here, however the solution uses $\lg n$ space. A problem in my course required adjusting the algorithm such that we use constant space.

I thought of the following algorithm:

MyAlgorithm(root, num)
    min<-findMin(root)
    max<-findMax(root)
    while(min.key <= max.key && min != max)
        if((min.key + max.key)=num) return true
        if((min.key + max.key) < num)
            min<-successor(min)
        else
            max<-predeccessor(max)
    return false

According the CLRS book both predeccessor() and successor() functions time complexity depends on tree height which is $O(\lg n)$ in our case since red-black tree is balanced.

However, we have the while loop which theoretically can run almost n times.

For example, because the algorithm above is equivalent to searching for the number num in a sorted array, say we have this array and num<-3: $$ 1,2,3,4,5,6,7,8,9,10 $$ In this case we'll call predecessor(max) $8$ times which is almost $\Theta(n)$ or $\Theta((n-2)\lg n)=\Theta(n\lg n)$ asymptotically.

Am I wrong in my conclusions? How can I prove that the time complexity is indeed $\Theta(n)$?

D.W.
  • 159,275
  • 20
  • 227
  • 470
Yos
  • 527
  • 1
  • 5
  • 18
  • 1
  • Please credit the original source of the problem. 2. Do you allow modifying the tree? 3. If only we had a way to do in-order traversal in constant space, we could apply https://cs.stackexchange.com/q/13585/755. Hmm. It's just possible it might be worth your time to try searching on that topic.
  • – D.W. Jun 30 '17 at 19:46
  • @D.W. we cannot modify the tree. The post https://cs.stackexchange.com/questions/13585/how-to-develop-an-on-algorithm-solve-the-2-sum-problem doesn't nearly have the restrictions that I have in the post, I don't really see the connection. – Yos Jun 30 '17 at 19:52
  • Can you clarify precisely what constant space means? Do you mean a constant number of memory cells, where each memory cell can hold a $\lg n$-bit number or a pointer into the tree? Do you mean a constant number of bits of memory? The former is probably the natural meaning. The latter is unlikely to be what you mean -- that isn't even enough to store a pointer to a single node in the tree. If the former, then you have $O(\lg n)$ bits -- can you think of any way to do an in-order traversal, if you were able to store $O(\lg n)$ bits? – D.W. Jun 30 '17 at 20:41
  • @D.W. such details were not mentioned in the original problem, all that was said was "constant space". – Yos Jun 30 '17 at 20:46
  • 1
    You said you saw the "original problem" somewhere else. Where? Was it in a book? A course? A programming contest? I encourage you to edit the question to tell us where you encountered the problem and provide credit to the place where you originally saw the problem. I'm not sure what post you are referring to. I'm talking about the statement of the problem, not your algorithm, not your code. – D.W. Jul 01 '17 at 01:39
  • What makes you think it can be solved in constant space? You said that the original problem said constant space, but the link you provided in the question doesn't say anything about constant space. – D.W. Jul 01 '17 at 07:42
  • A problem in my course required adjusting the algorithm such that we use constant space. – Yos Jul 01 '17 at 08:10