Getting ready for exam. This is not the homework question.
I figured that the worst case O(N^2) to 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With