For a balanced search tree, it is O(log(N)) for all cases. For unbalanced search trees, the worst case is O(N), e.g., insert 1, 2, 3, 4,.. and best case complexity is when it is balanced, e.g., insert 6, 4, 8, 3, 5 7. How do we define the average case complexity for unbalanced search tree?
Asked
Active
Viewed 1,449 times
3
-
I'd expect about `O(n^.5)` due to similarity to random walks, but I'm not posting an answer as I can't quite prove this. BTW, if this is homework, could you tag it as such? – pavpanchekha Feb 25 '10 at 03:42
-
1this is not homework. this is out of curiosity as most search trees in standard libraries in C++, Java are implemented as unbalanced. – user236215 Feb 25 '10 at 03:46
-
This is too tough to be homework. – Feb 25 '10 at 05:26
-
1It is not true that search trees in C++ and Java's libraries are unbalanced. Both use red-black trees. – jkff Feb 25 '10 at 05:27
-
"Unbalanced search tree" is a large category. Some math appears to be solving avg/max depth for "a random tree picked from the set of all possible trees with N nodes", and some solve for "a tree generated by inserting N nodes in random locations", and the two appear to have vastly different results :( – Mooing Duck Dec 07 '13 at 17:52
1 Answers
4
The average height of Binary Trees is Theta(sqrt(n)). This has been shown (or referenced, not very sure) in the following paper: http://www.dtc.umn.edu/~odlyzko/doc/arch/extreme.heights.pdf.
But perhaps you are more interested in the average depth of a random node and this is Theta(log n), as can be seen here: http://www.toves.org/books/data/ch05-trees/index.html (Section 5.2.4).