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-maxs, successor and predecessor nodes in BSTs is of O(h) while those of AVLs makes O(logn).
I don’t exactly understand what the difference is. With h=[logn]+1 in mind, so why do we say O(h) and somewhere else O(logn)?
I have a question on time complex in trees operations. It’s said that (Data
Share
his the height of the tree. It is alwaysOmega(logn)[not asymptotically smaller thenlogn]. It can be very close tolognin complete tree (then you really geth=logn+1, but in a tree that decayed to a chain (each node has only one son) it isO(n).For balanced trees,
h=O(logn)(and in fact it isTheta(logn)), so anyO(h)algorithm on those is actuallyO(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):
Both are valid Binary search trees, and in both searching for an element (say
1) will beO(h). But in the first,O(h)is actuallyO(n), while in the second it isO(logn)