I am reading a book on trees. here is the text snippet.
There are quite a few general algorithms to implement balanced trees.
Most are quite a bit more complicated than a standard binary search
tree, and all take longer on average. They do, however, provide
protection against the embarrassingly simple cases.A newer, method is to forego the balance condition and allow the tree
to be arbitrarily deep, but after every operation, a restructuring
rule is applied that tends to make future operations efficient. These
types of data structures are generally classified as self-adjusting.
In the case of a binary search tree, we can no longer guarantee an
O(log n) bound on any single operation, but can show that any sequence
of m operations takes total time O(m log n) in the worst case.
Questions on above text snippet
-
How author came to conclusion in first paragraph what does author means embarrassingly simple cases how general algorithms of balanced trees provide
protection against this? -
What does author mean “in last paragraph any sequence of m operations take total time O(mlogn) how we came to this conclusion, request to explain with
example.
Thanks!
For a typical, simple implementation of a binary search tree, merely inserting the sequence
1, 2, 3, ..., nwill produce a tree with n levels. (Inserting each element traverses the tree all the way down the right side, then adds a new element on that side, resulting in a maximally unbalanced tree.) I believe this is what they mean by “embarrassingly simple”.They are talking about splay trees (as opposed to AVL or red/black trees). AVL and red/black trees guarantee O(log n) worst-case for every insert/delete/lookup operation, but at the cost of complex code and a somewhat large constant factor. Splay trees do not guarantee O(log n) for every single operation, but they do guarantee O(log n) per operation on average for any long sequence of operations. So in the long run, they perform as well as the more complex trees, but with a simpler implementation and smaller constant factor.