What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a diffe
Your analysis is wrong, both insertion and deletion is O(n) for a sorted array, because you have to physically move the data to make space for the insertion or compress it to cover up the deleted item.
Oh and the worst case for completely unbalanced binary search trees is O(n), not O(logn).
There's not much of a benefit in querying either one.
But constructing a sorted tree is a lot faster than constructing a sorted array, when you're adding elements one at a time. So there's no point in converting it to an array when you're done.
Note also that there are standard algorithms for maintaining balanced binary search trees. They get rid of the deficiencies in binary trees and maintain all of the other strengths. They are complicated, though, so you should learn about binary trees first.
Beyond that, the big-O may be the same, but the constants aren't always. With binary trees if you store the data correctly, you can get very good use of caching at multiple levels. The result is that if you are doing a lot of querying, most of your work stays inside of CPU cache which greatly speeds things up. This is particularly true if you are careful in how you structure your tree. See http://blogs.msdn.com/b/devdev/archive/2007/06/12/cache-oblivious-data-structures.aspx for an example of how clever layout of the tree can improve performance greatly. An array that you do a binary search of does not permit any such tricks to be used.
Adding to @Blindy , I would say the insertion in sorted array takes more of memory operation O(n) std::rotate()
than CPU instruction O(logn), refer to insertion sort.
std::vector<MYINTTYPE> sorted_array;
// ... ...
// insert x at the end
sorted_array.push_back(x);
auto& begin = sorted_array.begin();
// O(log n) CPU operation
auto& insertion_point = std::lower_bound(begin()
, begin()+sorted_array().size()-1, x);
// O(n) memory operation
std::rotate(begin, insertion_point, sorted_array.end());
I guess Left child right sibling tree combines the essence of binary tree and sorted array.
data structure | operation | CPU cost | Memory operation cost | |
---|---|---|---|---|
sorted array | insert | O(logn) (benefits from pipelining) | O(n) memory operation, refer to insertion-sort using std::rotate() |
|
search | O(logn) | benefits from inline implementation | ||
delete | O(logn) (when pipelining with memory operation) | O(n) memory operation, refer to std::vector::erase() |
||
balanced binary tree | insert | O(logn) (drawback of branch-prediction affecting pipelining, also added cost of tree rotation) | Additional cost of pointers that exhaust the cache. | |
search | O(logn) | |||
delete | O(logn) (same as insert) | |||
Left child right sibling tree (combines sorted array and binary tree) | insert | O(logn) on average | No need std::rotate() when inserting on left child if kept unbalanced |
|
search | O(logn) (in worst case O(n) when unbalanced) | takes advantage of cache locality in right sibling search , refer to std::vector::lower_bound() | ||
delete | O(logn) (when hyperthreading/pipelining) | O(n) memory operation refer to std::vector::erase() |