H
H
hoxnox2012-01-28 11:56:36
Algorithms
hoxnox, 2012-01-28 11:56:36

What is the most efficient algorithm for adding an element to a tree?

There is an incoming increasing sequence (each next element is strictly greater than the previous one). It is necessary to add sequence elements to the tree in such a way that it remains balanced. What is the most efficient algorithm?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vgrichina, 2012-01-28
@hoxnox

In theory, in this case, it simply does not make sense to use a tree; a dynamically growing array with a binary search on it will be more efficient.
Also, if you first save the sequence into an array, then you can then build a balanced binary tree from it in O (N), simply by going around the array recursively in the correct order.

A
Artur Khashaev, 2012-01-28
@Invizory

Well, for example, you can use Cartesian trees , if the keys are sorted, then we can stuff them all in linear time. But, as noted above, there is little sense in this - a binary search will save everyone.

S
sgzmd, 2012-01-28
@sgzmd

Either way you get O(N log N) so AVL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question