Consider an unbalanced binary search tree and an ordered vector list. Which of the two structures is best suited to search for any element.
Consider an unbalanced binary search tree and an ordered vector list. Which of the two structures is best suited to search for any element.
The most effective algorithm to find any element in either case is the same: binary search. It has average complexity O (log n).
The problem is that in unbalanced binary trees, the worst-case scenario is O (n). For example, suppose I'm looking for 5
in the tree below:
1
\
2
\
3
\
4
\
5
The algorithm will go through all the elements of the tree to find the 5. Already with ordered lists, the worst case remains O (log n). Having the same items as an example:
[1,2,3,4,5]
The algorithm will scroll through only 3, 4, and 5 until you find the result. This way it is preferable to use an ordered list.
ABB Tree. The complexity of the tree is O (log n) and the complexity of the ordered list is O (n), so the search in an ABB tree is more efficient than the search in the list.