I have a question on time complex in trees operations.
It\'s said that (Data Structures, Horowitz et al) time complexity for insertion, deletion, search, finding mins-ma
O(h)
means complexity linear dependent on tree height. If tree is balanced this asymptotic becomes O(logn)
(n - number of elements). But it is not true for all trees. Imagine very unbalanced binary tree where each node has only left child, this tree become similar to list and number of elements in that tree equal to height of tree. Complexity for described operation will be O(n)
instead of O(logn)
h
is the height of the tree. It is always Omega(logn)
[not asymptotically smaller then logn
]. It can be very close to logn
in complete tree (then you really get h=logn+1
, but in a tree that decayed to a chain (each node has only one son) it is O(n)
.
For balanced trees, h=O(logn)
(and in fact it is Theta(logn)
), so any O(h)
algorithm on those is actually O(logn)
.
The idea of self balancing search trees (and AVL is one of them) is to prevent the cases where the tree decays to a chain (or somewhere close to it), and its (the balanced tree) features ensures us O(logn)
height.
EDIT:
To understand this issue better consider the next two trees (and forgive me for being terrible ascii artist):
tree 1 tree 2
7
/
6
/
5 4
/ / \
4 2 6
/ / \ / \
3 1 3 5 7
/
2
/
1
Both are valid Binary search trees, and in both searching for an element (say 1
) will be O(h)
. But in the first, O(h)
is actually O(n)
, while in the second it is O(logn)