开发者

Why O(N Log N) to build Binary search tree?

Getting ready for exam. This is not the homework question.

I figured that the worst case O(N^2) 开发者_StackOverflowto build BST. (each insert req N-1 comparison, you sum all the comparisons 0 + 1 + ... + N-1 ~ N^2). This is the case for skewed BST.

The insertion for (balanced) BST is O(log N), so why the best case is O(N logN) to construct the tree ?

My guess best guess - since single insertion is log N, than summing all the insertions somehow gives us N log.

Thanks !


As you wrote :) Single insertion is O(log N).Because the weighted tree height of N element is log N you need up to log N comparsions to insert single element. You need to do N of these insertions. So N*logN.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜