M
Matt Long
Hi
I would like to check this result:
I have tested binary tree search API from Linux (search.h) by
searching into a binary tree generated from an unsorted dictionary a
given word. The second program uses the same dictionary, from which an
array is initialized which is sorted with qsort() then the same word
is searched with binary search fct (bsearch()).
Running both programs shows that the binary search (the second
program) is almost twice as fast as the binary tree search.
I have used directly the sorted distionary (so qsort() should have
the highest complexity), but still the second program is the fastest.
Is this the correct result ? What advantage has binary tree search
wrt qsort() followed by binary search ?
thanks
Bogdan
Chances are that the binary tree search generated a degenerate tree --
that is, the tree is not balanced. Each lookup _could_ take as long
as a linear search in the worst case.
Quicksort will take O(n log n)to sort, but the lookup is a guaranteed
O(log n) operation.
This is the reason for self-balancing trees, such as red-black trees.
Matt