Quote:The answer turned out to be O(N), but I put O(N*log(N)) on the test. If each insertion into the BST takes O(log(N)) time, then inserting the N keys will take O(N*log(N)), right? Laying the process out, it would look like O(log(N) + log(N-1) + log(N-2) + . . . + log(N - (N -1))) = O(log(N*(N-1)(N-2). . .(N-(N-1)))) = O(log(N^N + . . .)) = O(N*log(N)). The first part, log(N), is the insertion of the last key; I reversed the addends to make it easier to manipulate and see. Moreover, another question that I got correct asks
"What is the complexity to take the keys from the sorted array of size N and create a perfectly balanced BST?"
Quote:Each removal of a key from the heap takes O(log(N)), removing them in sorted order; using the same argument I answered that the complexity is O(N*log(N)). And this is the answer on the solutions! The first question seems to be the same as the second question but in reverse; the answer to the first--according to the solutions--is O(N), but the answer to the second--according to the solutions--is O(N*log(N)). I emailed the professor, and he replied with "it doesn't matter that suboptimal algorithms exist which have O(N*log(N)) or even O(N^2) complexity because the single best answer is the one with O(N) complexity." He is talking about the first question. Am I missing an algorithm that takes only O(N) time? Sorry for the long question, thanks.
"What is the complexity to place the keys from a heap into an array in sorted order?"